Nice presentation on WCF
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.
Windows Communication Foundation(WCF) that is part of .NET Framework 3.0 is in the line of Microsoft(MS) classic work of abstraction. MS have been innovator in providing abstraction over various technologies from many years. In the same tradition I do believe WCF is another achievement. Undoubtedly they understand the difficulties in any technologies being faced by technology implementors and then they use their experience to nake the technology simple by intoroducing the layer of abstraction.
Let me talk about WCf in particular now
It was always challenge from many years to talk to remote objects.
Many technologies have come and gone or become extinct as dinosors.
To name a few will be CORBA or DCOM.
But there was always problem with the technology itself and its implementation.
Web Service was the most pouplar of all the technology and it still one of the best for such communication.
The problem comes when we want to expose a remote object interface with multiple protocol interface such as HTTP, TCP etc.
The problem with existing technology is that we have create multiple interface for the remote object i.e. each specific to each protocol.
This really become a stumbling block while deciding on the protocol to be used as creating multiple interface results in maintainence overhead.
So if we want a complete interoperable solution we need a web service based interface but if its standalone system the same Web Service interface becomes a performance bottleneck.
WCF really becomes a great help for such scenario. It allows developer to create interface irrespective of the protocol to use.
Developer can then configure the so called end points to decide on which protoocl to used for communication.
The developer will create the interface and implement it. It will then expose the interface using multiple end points i.e. HTTP, TCP, Named Pipes.
The client using the interface then can use the appropriate end point based their need.
WCf as infrastructure takes care all overheads related to messaging etc.
Of late I have been reading about WCF. Though I have many things to share but thought of sharing this link about how to expose the WCF Service as multiple end points i.e. suppose we wrote a WCF service to calculate home loan. The same service can be exposed as
- Named Pipe
If the service is to be used by non-.NET based app then HTTP is the best option and if both service and client are in same network and both the app are .NET based tcp can be used and if both the service and client areon same machine amed pipe can be used.
The question then comes do a developer have to expose multiple interface, the answer is NO. The developer have to use the configuration files to expose the service with 3 end points.
This is one of the cool feature that I liked about WCF. the below link provides the similar stuff with an example