WCF Self-Hosting multiple bindings

When building WCF Services there is this point when you have to make a critical decision: Which binding is the best for you service? Windows Communication Foundation comes with several different bindings, each of those created for specific situations and requirements. Here are some of the most used bindings with some of their characteristics as well:

Binding

Characteristics

BasicHttpBinding

Use the HTTP and HTTPS transport protocols and encodes messages as XML text.

WS2007HttpBinding

Support distributed transactions and secure, reliable sessions.

WSDualHttpBinding

Suitable for handling duplex communications. Duplex messaging allows a client and service to perform two-way communication without requiring any form of synchronization

WebHttpBinding

Suitable for building WCF REST Web services

NetTcpBinding

Uses the TCP transport protocol to transmit messages using a binary encoding. Great advantage it’s higher performace over the HTTP protocol

You can see that there are a lot of options but this post isn’t a tutorial about WCF bindings. Instead what I wanna show you is how to Self-Host the same or a even a different service over different WCF bindings. Let me explain in detail. Suppose that you have to host two different services in a console application. Why? Because the first service is supposed to be consumed over the internet, for example by customers of your company and the second one, is a service for your employees. So ideally, you would expose the first public service using a binding such as the BasicHttpBinding and the second service which is private and consumed under the local area network of your organization, would be exposed using the NetTcpBinding. To demonstrate this situation, we will build a project where the same service will be exposed under two different bindings using a single ServiceHost instance. More specifically, we will use the Chinook database which is one of my favorite for testing and you can download it for free in codeplex.com. Those who use MS SQL Server can use the sql script they will find inside project’s folder we will build (check download link at the bottom). We will create a WCF Service exposing two Operation Contracts, one to retrieve all artists and another to get all artists with their albums. Later we will host this same service in a console application, exposing it in two different bindings, BasicHttpBinding and NetTcpBinding. The client will be an ASP.NET Web Site which will consume the two different endpoints. Here is the diagram of our project.
Let’s start. You don’t have to type all the code by yourself, instead you can download the project at the bottom of the page. Make sure you change all the connections strings respectively. Create a solution named “ChinookWCFApplication” and add the first C# class library project named “ChinookModel”. Then add a new ADO.NET Entity Data Model item, named “ChinookEntities.edmx” and adding to it only the Artist and Album tables from the Chinook database.
Now let’s create the WCF service. Add a new C# class library project named “ChinookWcfService”. Add references to the ChinookModel project, to the System.ServiceModel and the System.Runtime.Serilization libraries as well. Last but not least, install the Entity Framework from the Nuget Packages... Add a new interface C# file, named IChinookService and paste the following code.

Our service is ready to be hosted. Add a new Console Application project to the solution, named “ChinookHostConsole” and add references both to the ChinookWcfService project and the System.ServiceModel library as well. Add the following code inside the Main method of your Program.cs file.

static void Main(string[] args)
{
//Create two different URIs to serve as the base address
// One for http requests and another for net.tcp
Uri httpUrl = new Uri("http://localhost:8090/ChinookHttpService");
Uri netTcpUrl = new Uri("net.tcp://localhost:8080/ChinookHttpService");
//Create ServiceHost to host the service in the console application
ServiceHost host = new ServiceHost(typeof(ChinookWcfService.ChinookService), httpUrl,netTcpUrl);
//Enable metadata exchange - you need this so others can create proxies
//to consume your WCF service
ServiceMetadataBehavior serviceMetaBehavior = new ServiceMetadataBehavior();
serviceMetaBehavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(serviceMetaBehavior);
//Start the Service
host.Open();
Console.WriteLine("Service is host at " + DateTime.Now.ToString());
Console.WriteLine("Host is running... Press <Enter> key to stop");
Console.ReadKey();
host.Close();
}

The code is quite self-explanatory I think. Notice the constructor we used for the ServiceHost object. We passed the service implementation class and the two different URIs we created before. Believe it or not, this is the only thing you have to do in order to host this service over HTTP and TCP protocols. Before testing if the WCF service is hosted correctly, copy and paste the connection string from the Model project to the console application as well. Set the console application as the Start up project and run it.
The last step to test is consuming the two different WCF service endpoints. Add a new ASP.NET Empty web site named “ChinookWebClient”. We need to create a proxy class in order to consume the service. We have two options, let Visual Studio create it for us or use the Svcutil.exe. We will make it through the Visual Studio this time so make sure you start the console application so it is running when we try to add a service reference. While the service is hosted, right click the Client project and select Add Service Reference... Paste the HTTP Uri and press Go. Name the service “ChinookServiceRef”.
We need a Web Form to test the service so create one naming it “Defaut.aspx”. This form will have just two buttons and a gridview. The first button will consume the GetArtists() OperationContract over the basicHttpBinding resulting in filling the gridview. The second button will consume the second operation contract GetArtistsAlbums resulting in writing the results in the page. Here is the Default’s html code.

Notice that two different endpoints have been created, one for the basicHttpBinding and another for the netTcpBinding. We will use the name of those endpoints when we create WCF clients. Here is the code for the code behind file for the Default Web Form.

As we have already mentioned, the gridview control will be filled consuming the “GetArtists()” method under the BasicHttpBinding_IChinookService and all albums will be displayed consuming the GetArtistAlbums() under the NetTcpBinding_IChinookService binding. Set as start up projects both the ChinookHostConsole (it should start first) and the ChinookWebClient. Build and run your solution.
Let’s try something interesting to ensure that these actions are indeed consumed under different protocols. In the console application remove the httpUrl parameter some that the service is hosted only under the TCP protocol. Also comment out all the ServiceMetadataBehavior code we wrote so we can create the proxy class.

Build and run your application again. Press the button to print all artist albums, using the TCP protocol. It should work just fine. Now press the button to consume the service using the BasicHttpBinding.
Obviously, you got an error since the service is hosted only under TCP protocol. That’s it, we saw how to self-host a WCF service in a console application under different bindings. I hope you enjoyed the post. You can download the project we created from here . Make sure you follow this blog to get notified for newly posts!

In WCF you set the Security mode in the Binding level (not endpoint). Hence in our case, you could set different security modes in the configuration file, one for BasicHttpBinding and another one for NetTcpBinding. This link, will guide you how to do it.

The purpose of this blog is to broaden my education, promote experimentation and enhance my professional development. Albert Einstein once said that “If you can’t explain it simply, you don’t understand it well enough” and I strongly believe him!