423,832 Members | 1,902 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,832 IT Pros & Developers. It's quick & easy.

WCF with two services sharing DataContract

CroCrew
Expert 100+
P: 563
Hello All,

Please let me say thank you to anyone that provides help to my issue. "Thank You"!

Let me explain my problem first then I will show the code.

I have a WCF solution (Example) that has two projects (Service and Service2). Within Service2 there is a "DataContract" called "xPerson". Both services has a method that returns "xPerson".

I would think that because both projects are in the same solution and are using the same entity within the datacontract that "xPerson" would be the same entity regardless if "xPerson" was returned from Service or Service2.

When I consume both service in an application I would assume that I could do something like this...

Expand|Select|Wrap|Line Numbers
  1. Service.xPerson = Service2.xPerson
What i get is "Cannot implicitly convert type.... Error"

Can anyone show me an example on how to do this? See my code below.

WCF solution (Example)

Service.cs
Expand|Select|Wrap|Line Numbers
  1. using System;
  2.  
  3. public class Service : IService
  4. {
  5.     public xPerson BuildPerson(string FirstName, string LastName)
  6.     {
  7.         xPerson ThisPerson = new xPerson();
  8.  
  9.         ThisPerson.FirstName = "[1]" + FirstName;
  10.         ThisPerson.LastName = "[1]" + LastName;
  11.  
  12.         return ThisPerson;
  13.     }
  14. }
  15.  
IService.cs
Expand|Select|Wrap|Line Numbers
  1. using System.Runtime.Serialization;
  2. using System.ServiceModel;
  3.  
  4. [ServiceContract]
  5. public interface IService
  6. {
  7.     [OperationContract]
  8.     xPerson BuildPerson(string FirstName, string LastName);
  9. }
  10.  
Service2.cs
Expand|Select|Wrap|Line Numbers
  1. using System;
  2.  
  3. public class Service2 : IService2
  4. {
  5.     public xPerson BuildPerson(string FirstName, string LastName)
  6.     {
  7.         xPerson ThisPerson = new xPerson();
  8.  
  9.         ThisPerson.FirstName = "[2]" + FirstName;
  10.         ThisPerson.LastName = "[2]" + LastName;
  11.  
  12.         return ThisPerson;
  13.     }
  14. }
  15.  
IService2.cs
Expand|Select|Wrap|Line Numbers
  1. using System.Runtime.Serialization;
  2. using System.ServiceModel;
  3.  
  4. [ServiceContract]
  5. public interface IService2
  6. {
  7.     [OperationContract]
  8.     xPerson BuildPerson(string FirstName, string LastName);
  9. }
  10.  
  11. [DataContract]
  12. public class xPerson
  13. {
  14.     string _FirstName;
  15.     string _LastName;
  16.  
  17.     [DataMember]
  18.     public string FirstName
  19.     {
  20.         get { return _FirstName; }
  21.         set { _FirstName = value; }
  22.     }
  23.  
  24.     [DataMember]
  25.     public string LastName
  26.     {
  27.         get { return _LastName; }
  28.         set { _LastName = value; }
  29.     }
  30. }
  31.  

Web Application

Default.aspx
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4.     <head runat="server">
  5.         <title>WCF Example</title>
  6.     </head>
  7.     <body>
  8.         <form id="form1" runat="server">
  9.             <div>
  10.                 Enter First & Last Name!<br />
  11.                 <asp:TextBox ID="tb1" runat="server" />&nbsp;&nbsp;<asp:TextBox ID="tb2" runat="server" />&nbsp;&nbsp;
  12.                 <asp:Button ID="btn1" runat="server" Text="Show" OnClick="btn1_Click" />
  13.                 <div id="AnswerDiv1" runat="server"></div>
  14.                 <div id="AnswerDiv2" runat="server"></div>
  15.             </div>
  16.         </form>
  17.     </body>
  18. </html>
  19.  
Default.aspx.cs
Expand|Select|Wrap|Line Numbers
  1. using System;
  2.  
  3. public partial class _Default : System.Web.UI.Page
  4. {
  5.     protected void Page_Load(object sender, EventArgs e)
  6.     {
  7.  
  8.     }
  9.  
  10.     protected void btn1_Click(object sender, EventArgs e)
  11.     {
  12.         ServiceReference1.ServiceClient client1 = new ServiceReference1.ServiceClient();
  13.         ServiceReference2.Service2Client client2 = new ServiceReference2.Service2Client();
  14.  
  15.         ServiceReference1.xPerson MyPerson1 = new ServiceReference1.xPerson();
  16.         ServiceReference2.xPerson MyPerson2 = new ServiceReference2.xPerson();
  17.  
  18.         MyPerson1 = client1.BuildPerson(tb1.Text, tb2.Text);
  19.         MyPerson2 = client2.BuildPerson(tb1.Text, tb2.Text);
  20.  
  21.         AnswerDiv1.InnerHtml = MyPerson1.FirstName + " " + MyPerson1.LastName;
  22.         AnswerDiv2.InnerHtml = MyPerson2.FirstName + " " + MyPerson2.LastName;
  23.  
  24.         // Here is here the error is.
  25.         //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  26.         MyPerson1 = client2.BuildPerson(tb1.Text, tb2.Text);
  27.         //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28.  
  29.  
  30.         client1.Close();
  31.         client2.Close();
  32.     }
  33. }
  34.  
Apr 28 '17 #1
Share this Question
Share on Google+
2 Replies


CroCrew
Expert 100+
P: 563
If anyone knows of a solution please let me know. Still looking....
May 31 '17 #2

Frinavale
Expert Mod 5K+
P: 9,731
Ok I'm not sure Why you are doing what you are doing but it comes down to what the Service is expecting.

Think about it for a second, your first WCF service Service generates WSDL for the methods that it provides. This includes details about the types that the service expects and what the service returns.

Likewise, your second WCF service Service2 generates its own WSDL for the methods it provides and types it expects and returns.

This means that, while both WCF projects are returning the same underlying class, the services are completely separate and running their own operations.

The compiler of your web application (that references the two services) doesn't know that the services are returning the same class because it has no way of knowing that...it got the information about what the services return from the WSDLs.

In other words, even though the two WCF services are using the same underlying types in their processes, the consuming application (your website) doesn't know that because it retrieves information about the services through WSDL which is simply XML that describes what is provided by the services. It has no idea what is going on at the server level with regards to what is being referenced etc.

Really don't know why you are attempting to do this anyways?

-Frinny
Jun 26 '17 #3

Post your reply

Sign in to post your reply or Sign up for a free account.