Two WCF services with different contracts but same business objects

断了今生、忘了曾经 提交于 2019-12-03 14:28:16

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.

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.

It's a semantic issue. If you want to define one UserService.Customer and DeviceService.Customer as semantically equal, then you ought to physically re-factor that data contract into a separate assembly. Alternatively, if you want to define UserService.Customer and DeviceService.Customer as semantically different, then keep them as separate types and write a utility function to translate from one to the other.

Simian

I thought I'd attempt to answer my own question with details on how I'd over come this solution. It was based on an article on a blog article by Dan Meineck.

Is summary, I think my concept of having multiple services for each of my root business entities was misguided.

Instead I exposed a single service which implemented several DataContracts eg

public partial class DeviceService : IDeviceService, IUserService
{
}

Because device service is created as a partial class this allowed me to separate the services, well I say separate, they are still the same service but it allowed me to separate them into separate files and give some structural organisation to the service.

The last piece of the implementation was to declare two end points in the service definition eg

<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 experienced in WCF enought to say if this is "correct" solution or not, but it's working a treat for my requirements. If anyone else has a better solution I'd love to hear it!

Cheers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!