Host and consume a WCF service over TCP

In a previous post we have seen how to build and deploy a WCF service in IIS and later consume that service, via a console client application. This service though, was build under the default configurations, that is to use the basic settings for the service to function. The default binding configuration for WCF services is the basicHttpBinding, which allows a service to be called over the internet, using http requests. Sometimes though, the service isn’t supposed to serve clients over the internet but serve clients within an organization for example. At these cases there are several different configurations can be applied that would make the service response more efficient. We are going to show now, how to configure a WCF service to function under the TCP protocol. I am going to configure the project from the previous post mentioned.

First of all, we need to extend IIS functionality so it can handle and support TCP protocol requests. To do that, Windows Process Activation service (WAS) must be enabled. I am using Windows 7 and WAS by default isn’t even installed. To activate go to control panel and find “Turn Features On and Off”. Select Windows Process Activation service and it’s sub-features, select Microsoft .NET Framework 3.51 and its sub-features, and then finally click OK. Ensure that you have the correct version of ASP.NET installed by running the following command in the “Developer Command Prompt for VS2012” which is under the developer tools folder of the Visual Studio 2012 folder.

aspnet_regiis -iru 

Open Internet Information Services as administrator, right click the Default Web Site and select Edit Bindings. If WAS was installed correctly, you should see a net.tcp entry in the window like this.


Now you need to configure the website that we have hosted in the IIS in previous post to listen request over the TCP too. Do that by right click the website from IIS Default Web Site, choose from Actions menu Advance Settings and add “net.tcp” to the “Enabled Protocols” entry. I had named the website “AdventureWorksService”.  The enabled protocols entry should look like this now.

http, net.tcp

Now the WCF service hosted in IIS can listen both HTTP and TCP requests (in different ports, TCP listens right now at :808 port). Back in the client project, note that in the App.config there is only one endpoint defined, which specifies the HTTP address of the hosted service. Also, in the Program.cs file, when we create the proxy, the default constractor is used. This is because only one endpoint is defined. Now, we are going to add another endpoint entry for TCP requests and also we ‘ll change the proxy contractor in the main method, passing as parameter the name of the new endpoint. That way, the requests will be sent over the TCP. The App.config file should change as follow:

<?xml version="1.0" encoding="utf-8" ?>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    <binding name="BasicHttpBinding_AdventureWorksService" />
  <endpoint address="http://localhost/AdventureWorksService/Service.svc"
   binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_AdventureWorksService"
   contract="AdventureWorksService.AdventureWorksService" name="BasicHttpBinding_AdventureWorksService" />
    <endpoint address="net.tcp://localhost/AdventureWorksService/Service.svc"
     binding="netTcpBinding" contract="AdventureWorksService.AdventureWorksService"
     name="NetTcpBinding_AdventureWorksService" />

Change the main method in the Program.cs file like this.

static void Main(string[] args)
// Create a proxy object and connect to the service
AdventureWorksService.AdventureWorksServiceClient proxy = new AdventureWorksService.AdventureWorksServiceClient("NetTcpBinding_AdventureWorksService");

// Obtain a list of all products
Console.WriteLine("Test 1: List all products");
string[] productNumbers = proxy.ListProducts();
foreach (string productNumber in productNumbers)
    Console.WriteLine("Number: {0}", productNumber);
Console.WriteLine("Test 2: Display the details of a product");
ProductData product = proxy.GetProduct("SA-M198");
Console.WriteLine("Number: {0}", product.ProductNumber);
Console.WriteLine("Name: {0}", product.Name);
Console.WriteLine("Color: {0}", product.Color);
Console.WriteLine("Price: {0}", product.ListPrice);
// Disconnect from the service
Console.WriteLine("Press ENTER to finish");

Build and run the program. You just consumed your service over TCP!

Categories: WCF

3 replies

  1. Note – be careful up whitespace in the “Enabled Protocols” in IIS. If there is whitespace between them, I have found it to fail. In your example, you specify “http, net.tcp”. It should be, “http,net.tcp”


  1. Throwing and catching Strongly Types SOAP Faults in WCF | chsakell's Blog
  2. Secure a TCP – WCF service at the Message level | chsakell's Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: