Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two WCF services with different contracts but same business objects

Tags:

wcf

wcf-client

For example I have two services hosted in IIS.

[ServiceContract]
public interface IDeviceService
{
    [OperationContract]
    DeviceCollection GetAllDevices(Customer customer);

}

[ServiceContract]
public interface IUserService
{
    [OperationContract]
    User Authenticate(string username, string password);

}

Both the User object that is returned from the Authenticate operation in the UserService and the DeviceCollection that is returned from the GetAllDevices operation in the DeviceService have a child object definition of Customer. Customer is a business object is in the same assembly as the User and Device objects.

My problem is on the client - when I call the device operation

userProxy.GetAllDevices(user.Customer);

The compiler complains with the following message:

Argument 1 - Cannot convert from UserService.Customer to DeviceService.Customer

I can connect to both services fine, it's the object definition of Customer that is the problem. I don't really want to put the Operations in the same service as they seem to live naturally in their own services. I guess what I'm asking is that how to other programmers deal with such a problem?

Cheers, Stuart

like image 261
Simian Avatar asked Jun 23 '09 15:06

Simian


People also ask

How would you expose different methods of a WCF service to different clients?

Simply create two services (interface) each for each client and deploy them on different endpoint. Give different URL to different client. 2. If you have only one service and want to restrict client to methods, then use Custom Authorization and restrict client.

Can WCF service have multiple endpoints?

Sometimes in our mind the question arise; can we implement multiple service contract in WCF service? And the answer is, Yes we can. Service class implement multiple service interfaces, and then expose each service using a different endpoint.

How do you create multiple endpoints in WCF?

The first endpoint is defined at the base address using a basicHttpBinding binding, which does not have security enabled. The second endpoint is defined at {baseaddress}/secure using a wsHttpBinding binding, which is secure by default, using WS-Security with Windows authentication.


2 Answers

If you want to share a data contract across multiple services then you will have to compile the data contract in question into its own assembly and then distribute that assembly to the client.

Even though the type appears to be the same, it is in fact a two separate types and that is why you are seeing the error that you are seeing. Your only other choice (other than a separate, shared assembly) is to combine the two services into one so that they can share the data contract.

like image 196
Andrew Hare Avatar answered Oct 27 '22 01:10

Andrew Hare


One option would be to use AutoMapper on the client to convert seamlessly from one type to another. As they have the same properties mappings would be straightforward.

like image 34
Darin Dimitrov Avatar answered Oct 27 '22 00:10

Darin Dimitrov