Two WCF services with different contracts, but with the same business objects

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 User objects that return from the Authenticate operation in the UserService and DeviceCollection that return from the GetAllDevices operation in the DeviceService have a child child definition. The client is a business object in the same assembly as the User and Device objects.

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

 userProxy.GetAllDevices(user.Customer); 

The compiler complains about the following message:

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

I can connect to both services normally, this is the problem of defining the Customer object. I really do not want to put Operations in the same service that they seem to live naturally in their own services. I assume that I am asking how other programmers deal with such a problem?

Cheers, Stuart

+6
wcf wcf-client
source share
4 answers

If you want to share a data contract with several services, you will have to compile the corresponding data contract into your own assembly, and then distribute this assembly with the client.

Despite the fact that the type seems to be the same, in fact they are two separate types, and that is why you see the error you see. Your only other choice (apart from a separate general assembly) is to combine the two services into one so that they can share the contract with the data.

+3
source share

One option is to use AutoMapper on the client to seamlessly convert from one type to another. Since they have the same properties, matching would be simple.

+2
source share

I thought that I would try to answer my question with details about how I came up with this solution. It was based on an article in a Dan Minek blog article.

Is a summary, I think that my concept of having multiple services for each of my root business objects was wrong.

Instead, I discovered one service that implemented several DataContracts, for example

 public partial class DeviceService : IDeviceService, IUserService { } 

Since the device service is created as a partial class, this allowed me to separate the services, and I say separately, they are still the same service, but this allowed me to separate them into separate files and provide some structural organization to the service.

The last part of the implementation was to declare two endpoints in the service definition, for example

 <service behaviorConfiguration="GPSCloudHost.DeviceServiceBehavior" name="BusinessService.DeviceService"> <endpoint address="Device" binding="wsHttpBinding" contract="BusinessService.DataContracts.IDeviceService"></endpoint> <endpoint address="User" binding="wsHttpBinding" contract="BusinessService.DataContracts.IUserService"></endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 

I'm not sure about WCF to say if this is the β€œright” solution or not, but it works with my requirements. If anyone else has a better solution, I would love to hear it!

Greetings

0
source share

This is a semantic problem. If you want to define one UserService.Customer and DeviceService.Customer as semantically equal, then you must physically refactor this data contract into a separate assembly. Alternatively, if you want to define UserService.Customer and DeviceService.Customer as semantically different, save them as separate types and write a utility function to translate from one to another.

0
source share

All Articles