Question

How many ServiceContracts can a WCF service have?

Specifically, since a ServiceContract is an attribute to an interface, how many interfaces can I code into one WCF web service? Is it a one-to-one?

Does it make sense to separate the contracts across multiple web services?

Was it helpful?

Solution

You can have a service implement all the service contracts you want. I mean, I don't know if there is a limit, but I don't think there is.

That's a neat way to separate operations that will be implemented by the same service in several conceptually different service contract interfaces.

OTHER TIPS

WCF services can have multiple endpoints, each of which can implement a different service contract.

For example, you could have a service declared as follows:

[ServiceBehavior(Namespace = "DemoService")]
public class DemoService : IDemoService, IDoNothingService

Which would have configuration along these lines:

<service name="DemoService" behaviorConfiguration="Debugging">
  <host>
    <baseAddresses>
      <add baseAddress = "http://localhost/DemoService.svc" />
    </baseAddresses>
  </host>
  <endpoint 
    address =""
    binding="customBinding"
    bindingConfiguration="InsecureCustom"
    bindingNamespace="http://schemas.com/Demo" contract="IDemoService"/>
  <endpoint 
    address =""
    binding="customBinding"
    bindingConfiguration="InsecureCustom"
    bindingNamespace="http://schemas.com/Demo" contract="IDoNothingService"/>
</service>      

Hope that helps, but if you were after the theoretical maximum interfaces you can have for a service I suspect it's some crazily large multiple of 2.

@jdiaz

Of course you should strive to have very different business matters in different services, but consider the case in which you want that, for example, all your services implement a GetVersion() operation. You could have a service contract just for that operation and have every service implement it, instead of adding the GetVersion() operation to the contract of all your services.

A service can theoretically have any number of Endpoints, and each Endpoint is bound to a particular contract, or interface, so it is possible for a single conceptual (and configured) service to host multiple interfaces via multiple endpoints or alternatively for several endpoints to host the same interface.

If you are using the ServiceHost class to host your service, though, instead of IIS, you can only associate a single interface per ServiceHost. I'm not sure why this is the case, but it is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top