Understanding WCF Hosting

 WCF is a flagship product from  Microsoft for developing distributed application using SOA. Prior to WCF   traditional ASMX Web services were hosted only on Internet Information Services (IIS). The hosting options for WCF services are significantly enhanced from Microsoft .NET Framework 3.0 onwards. In WCF there are three main parts: Service, Endpoints and Hosting Environment. Multiple services can be hosted using a single host process and also the same service type can be hosted in multiple host processes. We can host a WCF service in two ways –  Self Hosting and IIS Hosting. Here we will examine  Self Hosting along with a comparative discussion of  different hosting environments. For an introduction to WCF please read my first WCF article at  http://www.sql-server performance.com/articles/dev/windows_communication_foundation_wcf_intro_p1.aspx

Self Hosting Vs IIS Hosting

Let’s look at  the differences between the two hosting types available for a WCF service.


IIS Hosting

Needs to add code to host the process

Automatic hosting

Host process should be running before client makes a call to the service.

IIS host runs automatically when a client makes a call to the service.

Automatic process recycling not possible

Automatic process recycling not possible

Can   controlled the service lifetime using Open and Close methods

Lifetime cannot be controlled manually.

Easy to deploy

More difficult deployment than  Self-Hosting

Protocols Supported


http, wshttp



Windows console and form application


Windows service




Hosting using a Windows Console Application

Here we will walk through how we can host the service using a windows console application:

Step I: Select the Windows project and then  the Console Application Template.

Step II: Open the App.Config file.

Step III: Add a reference of the Service project to the console application project.


Step IV:                                                                                                          

Add   namespace: using System.ServiceModel;

Add namespace: using WCFSample; 

Step V:

In the main function add the following code.

   using (ServiceHost host = new ServiceHost(typeof(Service1)))



                Console.WriteLine(“Service is running”);



Step VI:

Run the application and you should see the below  output:

Other Self-Hosting options

We can also use Windows Forms ,WPF forms or Windows Services for self Hosting. Windows services can start when the system starts, without requiring a user to log in to the machine and this unique advantage makes a  Windows Service  a great way for self hosting.

In case of windows Form or WPF Form in the Form Page Load we can write the following code:

  using (ServiceHost host = new ServiceHost(typeof(Service1)))



                MessageBox.Show(“Service is running”); 


In the case of a  windows service, we have to open and close the host in the OnStart and OnStop methods respectively. The code as follows:

  ServiceHost _host = null;

        protected override void OnStart(string[] args)


            _host = new ServiceHost(typeof(Service1));



        protected override void OnStop()


            if (_host != null)



                _host = null;



Hosting in WAS:

Windows Activation Service (WAS) was introduced with Windows Vista. WAS is part of IIS7 but can be installed and configured separately. WAS builds on the existing IIS 6.0 process and hosting models, but is much more powerful because it provides support for other protocols besides HTTP. Now, all the great features of IIS (on-demand activation, process health monitoring, enterprise-class manageability, rapid failure protection), are also available for  non-HTTP-based applications (TCP and Named Pipes)like HTTP based applications. Like IIS Hosting, WAS also requires a .svc file.

Optimal Hosting Environment:

Each and hosting environment has its own merits and demerits. In the case of self-hosting(or hosting a WCF service in a managed application) we have already seen that it needs less infrastructure to deploy and takes only two lines of code to create the service host and open the service to expose it to clients keeping all the endpoints definitions (addresses and bindings) in the config file. In self hosting we can use a console application as well as rich form based applications such as windows form applications or WPF applications. So the  question arises should we go with a console application or with a rich client based application in case of self hosting of WCF services.A console application provides a quick and simple environment under which we can develop,test, and debug our service and host but provides very little in terms of user interface functionality. We can accept this limited UI functionality and once the service is ready for production,a rich client application(windows form application or windows presentation foundation application), can then be used to put the application into production to get the benefit of communication with external sources such as in a peer to peer environment.

Hosting WCF service under windows service is more or less equal to hosting a WCF service in other managed code applications.We have already seen the advantages of a windows service for hosting a WCF service and it is ideal in such situations where a WCF service is long running and requires no user interaction. Unlike a windows service, an IIS hosting environment can only communicate over the HTTP protocol and has  no explicit control ofver the service lifetime. However,  WAS gives us a solution after almost combining the benefits of both IIS and windows service by extending the WCF services beyond HTTP.


WCF hosting allows services to be hosted in different environments such as Windows NT,Windows Forms,console applications, IIS(Internet Information Services) and Windows Activation Services(WAS). The choice will depend on the version of windows and the  transport protocols i.e, how the service will send the messages.Both hosted and self-hosted having their advantages and disadvantages.The goal is to recognise and determine the best hosting environment for WCF service.


Leave a comment

Your email address will not be published.