Reputation: 20346
I was trying to understand what goes on in the background when I write the following piece of code to host a REST webservice in WCF:
RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(),
typeof(MyConcreteService)));
Also, the class definition is something like this:
public class MyConcreteService : IService
where IService defines the ServiceContract and OperationContract with WebGET etc
In setting up the routes, I never mention IService
anywhere. How does the server find out what contracts to use ? Can someone spell out the secret that goes on in the background which makes the webservice work ?
Upvotes: 0
Views: 86
Reputation: 87228
The WebServiceHostFactory
returns an instance of WebServiceHost
. That class will see if the service class itself is decorated with [ServiceContract]
- if so, it will use it as the contract type. Otherwise it will look for the interfaces implemented by the service class to see which ones are decorated with [ServiceContract]
. If it finds only one (which is the case in your example), then it will be the one used as the contract type. If it finds 0 or more than 1, it should throw an exception.
Having the contract type, WebServiceHost
will add an endpoint with that contract type, the appropriate binding (WebHttpBinding
) and an empty relative address. It will also add the appropriate behavior for web endpoints (WebHttpBehavior
). That's essentially it; it may do other things such as setting up a content type mapper if needed, etc, but the beauty of the WebServiceHost (and WebServiceHostFactory) class is that you don't need to know about it (in most of the cases).
Upvotes: 3