Multiple WCF Contracts

May I get some clarification on the complex WCF service that provides my business objects. Say I have 4 objects: contact , organisation , project and letter .

The best way to create my service:

  • Make 4 contracts as “service objects” and pass the object and the intended operation as values ​​/ parameters in the “service object”? or
  • Create contracts for all objects and their functions (which can be many)

Thank you very much,

Chris

+4
source share
2 answers

In accordance with the principle of separation of segments , you can think about how to separate these things.

One typical approach is to have one interface (for example, one “service”) for each type of object - for example. one interface for Contact with all necessary operations and useful for contacts, etc.

Of course, you can also have methods related to several different types of objects - it is somewhat difficult to place in a specific service contract.

In addition, with WCF, you can easily have one service implementation class, which then, in turn, implements several of these interfaces at once - for example. Use common code or common templates.

But I think it would be nice to rethink your service contract and convert it into smaller and more manageable fragments.

Update:

if your service implementation class implements four service contracts, you need to configure it as follows:

 <services> <service name="YourNamespace.YourServiceImplementation"> <host> <baseAddresses> <add baseAddress="http://YourServer/MyServices/" /> </baseAddresses> </host> <endpoint name="Contact" address="Contact" binding="basicHttpBinding" contract="YourNamespace.IContactService" /> <endpoint name="Letter" address="Letter" binding="basicHttpBinding" contract="YourNamespace.ILetterService" /> <endpoint name="Organisation" address="Organisation" binding="basicHttpBinding" contract="YourNamespace.IOrganisationService" /> <endpoint name="Project" address="Project" binding="basicHttpBinding" contract="YourNamespace.IProjectService" /> <endpoint name="mex" address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> 

Now each of your services is available at a specific endpoint:

  • your IContractService is available at http://YourServer/MyServices/Contact
  • your ILetterService is available at http://YourServer/MyServices/Letter

etc....

For each of these addresses you can now add links to services from the client - add only those that you really need. One application may require only one of these services, the other may require two or three, etc.

+8
source

To add to Mark's very useful answer, the interfaces should be designed as shown below, and only then will we get the opportunity to add an independent link to each contract implemented separately by the service.

 [ServiceContract(Name="Contact", Namespace="YourNamespace.IContactService")] public interface IContractService { ... } 

Similarly for other interfaces

 [ServiceContract(Name="Letter", Namespace="YourNamespace.ILetterService")] public interface ILetterService { ... } 

Without adding these attributes, I was unable to add a separate refernce service for each of the contracts implemented by the shared service.

+1
source

All Articles