Friday, February 24, 2012

WCF NetTcp in IIS

Today I will make a short demo about hosting wcf services with nettcpbinding in iis.
There are cases when is more suited for your service to use a nettcpbinding for different reasons: speed, reliability, etc.
But in this case you are limitted to choose the hosts of your service from console app, windows service. So what if you want to host your service in iis and still expose nettcpbindings ?

So here are a few stept that must be done before we can use our service. For a more in-depth description check this post IIS 7.0 and WAS

Step 1. Enable Non-Http protocols
Go to Control Panel->Programs->Turn Windows features on/off and under .NetFramework 3.5.1 be sure to tick "Non-Http activation" (remember that iis supports only http)

Step 2. Enable WAS and PortSharing.
The previous step will make available some services (wcf services by the way) that will help us. So go now in your services list: Task Manager->Services tab and be sure that the following services are running:

WAS - is the Windows process activation service
NetTcpPortSharing -  is the WCF TCP port sharing service. It implements a centralized TCP listener so that multiple processes can listen on the same TCP port. This service is available even if IIS 7.0 is not installed.
NetTcpActivator - is the WCF TCP Activation Service. It communicates TCP activation requests to WAS.
Step 3. Create a WCF Service with a NetTcpBinding and create the endpoints.

Create the binding in the bindings tag, and then create 2 endpoints. One for your contract and one mex.
Note: If you don't create the mex endpoint, you won't be able to add a reference in your project.
Step 4. Publish the service in iis and configure it

Right click on the service->Publish. Be sure to tick 'Mark as IIS application'
In IIS, expand your newly created application, go in content view and browse your service. You will get this error:
This tells us that currently we don't have support for tcp protocol so here is what you have to do in order to eneable it: in iis, right click on application->Manage Application->Advanced Settings and add "net.tcp" next to http and then click ok.
 Now the error should be gone
Step 5. Add a reference to your project
And that's it :).