简体   繁体   中英

what happens in the background of hosting a rest webservice using wcf

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 ?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM