In continuation of previous post regarding WCF, here goes another. In this post I will do step step explanation for hosting service using config file (remember this is the beauty of WCF, we don’t need to recompile to add multiple endpoints).
The assumtion is readers have already implemented the service contract. I will use a dummy class named IService for my explanation
- Create a instance of ServiceHost.
- ServiceHost servicehost = new ServiceHost(typeof(Examples.ServiceImplementation));
- Use the Open to start listening.
- Now put the following in the App.Config file
Let me explain each of the element
The host element declares the base address for the endpoint. In this case its declaring two base address the http address and another one is the tcp address. The host address is used by the ServiceHost to listen on the respective port and address.
There are three endpoints
Each endpoint basically is collection of address, contract and binding. Here in the above case the address is not specified as we have already sepcified in the base address. The contract is Examples.IService and the binding is HTTP.
- The second endpoint
is the TCP endpoint for the same service. Smilar to the previous one this definess the endpoint as collection of Examples.IService as contract and binding as TCP and address is not mentioned and taken from the base address.
The last endpoint
is used by the client to generate the meta information about the Service. This is standard and ideally should be there for each service.I hope the above helps in understanding this beautiful technology.
The author has 10+ years experience with Enterprise Poduct Development. He is also the Co-Founder of PCVITA. He have expertise in Enterprise Architecture, Enterprise Messaging Platform Migration, Email Archiving.