473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

WCF with two services sharing DataContract

CroCrew
564 Expert 512MB
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
2 2352
CroCrew
564 Expert 512MB
If anyone knows of a solution please let me know. Still looking....
May 31 '17 #2
Frinavale
9,735 Expert Mod 8TB
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

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

Similar topics

3
by: Chris Dunaway | last post by:
How can a class be shared between a web service and a client that consumes the web service? Suppose I have a Class Libraray with the following simple class: Public Class SimpleClass Private...
5
by: Jeff | last post by:
Hi - I'm creating an installation program that will install a VB.NET program and an instance of MSDE on an unknown desktop (commercial application). The install will include the MSDE...
6
by: Moshe Kravchik | last post by:
Hi all! I have 2 web services, one writtenin C++ (ATL) and another one in C#. Is there a way to define data stuctures in a single place both services could use? The structures are the same, but if...
1
by: Kris | last post by:
I read a column on sharing types between web services at http://msdn.microsoft.com/library/en-us/dnservice/html/service07162002.asp Sharing types can be acheived, similar to what described here...
1
by: bruce.1 | last post by:
I'm trying to figure a way for a remote user -- using ASP.NET -- to share files on a local office LAN. The remote user would go to an ASP.NET website (on the LAN), mark the files they want, and...
0
by: Daniel P. | last post by:
http://danutp.blogspot.com/ Web Services - sharing data between client and server Dealing a lot with web services a friend of mine (Ehsan Samani) and I ran into another issue: when we move...
0
by: Emily | last post by:
Imagine a world where everybody shares and has faith in each other. We have all struggled at one time or another with our jobs or careers and have wondered if there was a better way to make a...
4
by: Dave Burns | last post by:
Hello, I am trying to specify a logical default value for a in a WCF Web Service using basicHttpBinding. I realize that the language defaults are: int - 0 string - null bool - false
1
by: Padu | last post by:
Hi All, In WCF, up to now, I've been using the default pattern of creating an IService.cs that holds both and for the composite types that must be returned by the service operations. It...
0
by: Marcel Overweel | last post by:
Hi, I'm developing a set of services for a software solution. It will be broken down in several application including a few windows services. It is very likely that most of these services...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.