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
- Service.xPerson = Service2.xPerson
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
- using System;
- public class Service : IService
- {
- public xPerson BuildPerson(string FirstName, string LastName)
- {
- xPerson ThisPerson = new xPerson();
- ThisPerson.FirstName = "[1]" + FirstName;
- ThisPerson.LastName = "[1]" + LastName;
- return ThisPerson;
- }
- }
Expand|Select|Wrap|Line Numbers
- using System.Runtime.Serialization;
- using System.ServiceModel;
- [ServiceContract]
- public interface IService
- {
- [OperationContract]
- xPerson BuildPerson(string FirstName, string LastName);
- }
Expand|Select|Wrap|Line Numbers
- using System;
- public class Service2 : IService2
- {
- public xPerson BuildPerson(string FirstName, string LastName)
- {
- xPerson ThisPerson = new xPerson();
- ThisPerson.FirstName = "[2]" + FirstName;
- ThisPerson.LastName = "[2]" + LastName;
- return ThisPerson;
- }
- }
Expand|Select|Wrap|Line Numbers
- using System.Runtime.Serialization;
- using System.ServiceModel;
- [ServiceContract]
- public interface IService2
- {
- [OperationContract]
- xPerson BuildPerson(string FirstName, string LastName);
- }
- [DataContract]
- public class xPerson
- {
- string _FirstName;
- string _LastName;
- [DataMember]
- public string FirstName
- {
- get { return _FirstName; }
- set { _FirstName = value; }
- }
- [DataMember]
- public string LastName
- {
- get { return _LastName; }
- set { _LastName = value; }
- }
- }
Web Application
Default.aspx
Expand|Select|Wrap|Line Numbers
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>WCF Example</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- Enter First & Last Name!<br />
- <asp:TextBox ID="tb1" runat="server" /> <asp:TextBox ID="tb2" runat="server" />
- <asp:Button ID="btn1" runat="server" Text="Show" OnClick="btn1_Click" />
- <div id="AnswerDiv1" runat="server"></div>
- <div id="AnswerDiv2" runat="server"></div>
- </div>
- </form>
- </body>
- </html>
Expand|Select|Wrap|Line Numbers
- using System;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void btn1_Click(object sender, EventArgs e)
- {
- ServiceReference1.ServiceClient client1 = new ServiceReference1.ServiceClient();
- ServiceReference2.Service2Client client2 = new ServiceReference2.Service2Client();
- ServiceReference1.xPerson MyPerson1 = new ServiceReference1.xPerson();
- ServiceReference2.xPerson MyPerson2 = new ServiceReference2.xPerson();
- MyPerson1 = client1.BuildPerson(tb1.Text, tb2.Text);
- MyPerson2 = client2.BuildPerson(tb1.Text, tb2.Text);
- AnswerDiv1.InnerHtml = MyPerson1.FirstName + " " + MyPerson1.LastName;
- AnswerDiv2.InnerHtml = MyPerson2.FirstName + " " + MyPerson2.LastName;
- // Here is here the error is.
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- MyPerson1 = client2.BuildPerson(tb1.Text, tb2.Text);
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- client1.Close();
- client2.Close();
- }
- }