Apart from other bindings supported by WCF, I found netTcpBinding as more complex to configure than its other peers and less (rather scattered) information about the same over internet. I thought of putting it all together from the very first step at one place in order to make it easier for everyone including myself to use it as reference in future. So lets get started.
Remember : netTcpBinding hosting on IIS is only supported by IIS7 and later versions. I’m using Microsoft Visual Studio 2010 and IIS7.5 for this demo.
By default, IIS only supports HTTP or HTTPS. In order to let IIS support netTcpBinding, we first need to enable/activate WCF non-HTTP activation. To do that, Go to control panel > Program and Features > Turn Windows features on or off and then look for ‘Microsoft .NET Framework 3.5.1’ . You will see something like below:
Make sure you select both options. This will enable IIS to support HTTP, HTTPS, net.tcp, net.pipe, net.msmq, msmq.formatname.
Remember : After windows finish enabling these features, it will unregisters ASP.NET from the machine.
As this has unregisters the ASP.NET from the machine. So, next step is to register ASP.NET with IIS. To do this,go to Visual Studio Command Prompt and then type ‘aspnet_regiis -i’. This will register the ASP.NET with the IIS and you will be able to host your web application again.
Now next step is to check if you have required windows service running. Go to Administrative tools > Services and check the following service if they running, if not start them.
These three service ensures that IIS is able to listen to TCP request and also enable the port sharing.
As you machine is now configured to support netTcp, next step is to configure IIS to enable a website to use net.tcp port which is 808 by default. You can also change it any other port you want. Make sure you don’t configure net.tcp to port 80 because it is being used by HTTP and you cannot have two protocols running on the same port. Alternative, either change HTTP port to something else other than 80 (only if you want to use net.tcp on port 80, BUT it is not a recommended approach).
Now time to configure IIS. Open IIS and then go to your WebSite (‘Default Web Site’) and right click on it and then go to ‘Edit Bindings’. You will see the following:
Did you notice that you got whole list of protocols other than HTTP and HTTPS. This is because we have enable WCF non-HTTP activation in step 1.
Now select net.tcp from the drop down and you will have something like following:
In the ‘Binding information’ text box, you need to specify which port you want to use for netTCP connection. As I mentioned earlier, 808 is default but you can have any other port as well. Format is ‘port:ipaddress’ so I entered ‘808:*’ and then click ok.
Now you website is configured to listen TCP connection request on port 808. Remember if you have multiple website where you need to setup TCP then you have to do the same for all the websites.
Next step is to enable net.tcp protocol for your application in the IIS. To do that right click on your application in the IIS and then click on ‘Advanced Settings’ and look for property ‘Enabled Protocols’ and there enter ‘net.tcp’. List of enabled protocol is a comma-separated list so you can enable multiple protocols by separating them with a comma.
Now your website is fully configured to use netTcp binding. Next step is to configure your project to have WCF service on TCP port 808.
Open Visual Studio Project where you have your WCF service and then right click on the project and then go to project ‘Properties’. You should have something like below. Select ‘Web’ tab from left and change servers to be local IIS web server instead of Visual Studio Development Server and change Project URL and point it to your IIS application.
The reason we have changed Server to be Local IIS Web Server instead of Visual Studio Development Server is that you can configure IIS Web Server to support net.tcp but you cannot configure the same for Visual Studio Development Server. This is because, Visual Studio Development Server only supports HTTP.
Now the next step is to configure your web.config of the project to support netTcpBinding for the WCF Service.
Our last step is to configure web.config to use netTcpBinding for the WCF Service. Following the sample of web.config on how it should for a WCF service using netTcpBinding.
Now there are few points to note:
– ‘httpGetEnabled’ attribute of ‘serviceMetadata’ must be set to ‘false’. This will allow to discover WCF service metadata over non-HTTP protocol.
– Note we are using ‘mexTcpBinding’ for the mex endpoint. You can also use ‘mexHttpBinding’ but then you have to set ‘httpGetEnabled’ attribute to ‘True’ and you should be able to locate your WCF Service using http address instead of net.tcp.
– Its important that you specify the baseAddress for your endpoint. This will help IIS to let find your WCF Service. It will then know where to look for.
Next and final step is try and find your WCF Service in the browser.
Please note: you will only be able to discover service metadata in browser if you have set httpGetEnabled to True and have used mexHttpBinding instead of mexTcpBinding in the web.config. This is because, web browser only supports HTTP and HTTPs. Oterwise it will only be discoverable from within visual studio itself (see screenshot one after the below).
And now try using Add Service Reference option where you want to add the reference to this WCF Service.
Thats it and your ready to go.
Following are the errors you can encounter if you skipped or didn’t do any step properly.
– Check if you have correctly enabled the protocols for the application in the IIS (Step 5).
– Check if you have mentioned proper baseAddress in the web.config (Step 7).
– Perform Step 2.