Hosting in WCF – Part 1

Hosting is the key ingredient to give life to your service. Without a host, service holds no meaning. Host is the application, which is responsible for listening the calls and then instantiating the service. WCF offers two types of hosting; one is called Web-Hosting which is there since web service days. Few artifacts about this is easy to setup, dependency on IIS and only supported protocol is HTTP. There is a catch involved in this. We will cover the same in coming section. Now, another hosting scenario is Self-Hosting. Self-Hosting covers any application. Any application can be host. It requires little bit of coding but gives very deeper control over your hosting. With this, you can setup custom hosting, which will feature rich. So, without wasting time let us get started.


Self-Hosting depends on ServiceHost class. Hence, it requires at least one instance of the same, which is in the System.ServiceModel namespace and assembly off course. One point to note here that ServiceHost class used in all scenarios including Web-Hosting. Nevertheless, in case of Web-Hosting IIS instantiates for us. Now, when the host (instance of ServiceHost) is open, it requires Endpoint information. Endpoints are nothing but a collection of Address, Binding and Contract. We will come to this later. Nevertheless, briefly, Endpoints tell the host how and where to listen for calls from consumer. Now, this information can be provided either from configs file or directly from code. We will see that in a moment. Once, host opened successfully, host will be listening and waiting for calls from consumer. Lastly, once opened the host, it needs to be closed as well. Either it can closed gracefully or it can be aborted. Close will wait for any in progress calls and Abort will simply terminate any calls in the pipeline.


In this section, 1st I am going to cover Console Host which is nothing but an example of self-host. As you can see in the Hosting folder, I have already created one Console App with the name Console Host.


This is the basic skeleton of any console app. Let me show you the references I have to make the same self-hosted application.


As you can see in the above screen shot, it has two basic dependencies for any WCF application and it has all the dependencies, which my service is going to need. Moreover, my program class looks like shown below.

As I said for any self-hosted app we are going to need minimum one instance of ServiceHost; so the very first thing I have instantiated the same with the reference of my MovieManager class. Then I have opened the host. As soon as host is opened, it is going to require Endpoint configuration, which I will supply in a moment. Then, at the end of the call, I have just closed the connection gracefully.  

Service Configuration:-

In this section, we will provide Endpoint information via config file. WCF relies heavily on configuration. One point to note here, that anything in configuration can be done procedurally as well, but we will look at the same later. There has been much noise around WCF community regarding WCF configuration. It has been a subject of criticism. In fact, Microsoft in .NET 4.0 has given an option of config-less services. It means that some of the basic stuffs which configuration was providing can be assumed based on other elements. Nevertheless, what I like about configurations is its explicitness and it is very readable. It often gives me an overview of entire WCF system. Hence, I stick to the configuration basics of WCF. In case of config-less, every time any problem happens, you need to dig in code to understand the problem. This is one of the pain points of config-less WCF. The basic elements of Service Configurations are endpoints. This is the primary thing which host is going to need in order to host the services. Optionally, you can set variety of features to services, which we will cover, in coming section. However, for now Endpoint is ok.   Now, let us go ahead and open the App.Config file. Here, in this file first I have mentioned my SQL Server connection string as it is going to be needed, when host will make the service call.

Apart from that I have also installed, Entity Framework here, hence those entries are also present here. Now, I am going to provide my endpoint details. Below in the snippet, I have provided the complete details. Now, if you are wondering that from where, this connection string is coming, so it’s coming from the Data project MovieReviewDbContext class as shown below.

And also,

Let me explain the code a bit, in WCF configuration, whether it is on client side or server side everything goes inside tag. Since, we are hosting here, hence the next tag comes is . One point to note here that you need one service tag for every service which you are hosting just like one ServiceHost instance for every service which you are hosting. Then, I have provided the service name and this is fully qualified type name. Now, inside the service tag, you need one or more Endpoint. The service tag is one to one with actual service itself means there is one to one relationship between service tag and actual service. The endpoint is at service contract level. Now, endpoints are going to have lot of additional properties, but the most important are

• Address: – The address of the Endpoint is the URL which client can discover it.
• Binding: – The binding is going to define transport medium or communication medium.
• Contract: – The contract is nothing but the service contracts.
While defining these values you will get some sort of intellisense support to help you incase if you done some typo. Binding here, I have specified is the netTcpBinding for a good reason as I am sitting inside the firewall, hence I will pick the one, which is fastest. Later on, I will cover whole suite of different bindings. Nevertheless, for now TCP is fine. Since, I have used netTcpBinding, hence, my URL has to be a TCP one as specified in Address. Here, I am done with all kind of configuration. So, if I simply run the same, then it should run fine.

When I run the same for the first time, it will give Firewall popup as shown below in the screen shot.


When, you click on Allow access, it will take you to the output window.


I hope you would have liked this discussion. Thanks for joining me.

Rahul Sahay
Happy Coding

Thanks, Rahul Happy Coding