Search code examples
c#wcfservice-reference

Error using 2 wcf services with methods that require the same class as param


The error is referenced in the comments of Client Demo Code

Service 1 Demo Code

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        Boolean AddPerson1(Person p);
    }

Service 2 Demo Code

   [ServiceContract]
   public interface IService2
   {
       [OperationContract]
       Boolean AddPerson2(Person p);
   }

Client Demo Code

Person p=ServiceReference1.Person{Name="Peter"};
new Service2Client().AddPerson2(p); //Error right here because the method requires ServiceReference2.Person

Error Message

enter image description here

I need to be able to use Person from ServiceReference1. Maybe Class cast would solve the problem but i am not allowed to do it

ServiceReference1 and ServiceReference2 Settings

enter image description here


Solution

  • The error message is clear:

    Argument 1: cannot convert from WpfApplicatoin1.ServiceReference1.Person to WpfApplication1.ServiceReference2.Person

    The objects are not the same, even if the code is the same, because of namespaces.

    You'll need to map from WpfApplication1.ServiceReference1.Person to WcfApplication1.ServiceReference2.Person, or put the Person object in a separate assembly that is shared by the services and the client.

    For example, if you were to map between the two you might have something like this:

    Person p = ServiceReference1.Person{Name="Peter"};
    ServiceReference2.Person p2 = new ServiceReference2.Person() {
        Property1 = p.Property1,
        Property2 = p.Property2,
        // and so on
    };
    new Service2Client().AddPerson2(p2);