Friday, March 16, 2012

WCF : Enable the net.tcp protocol on IIS.7

Before we can get up and running, however, there are a few issues that need to be resolved. Those issues are as follows:
  • We have to enable and start the "Net.Tcp Listener Adapter" if it's not already done.
  • IIS has to be configured to allow the "net.tcp" protocol.
  • Our "Site" in IIS needs to have binding configuration setup for the net.tcp protocol.
This article assumes that you already have the .Net framework 3.5 SP1 installed (really only .Net 3.0 is needed). Also, I'm using Windows Server 2008/Vista (IIS 7.0) for this article, but the same can be achieved with IIS 6.0 on Windows Server 2003 (or even XP for that matter).

Net.Tcp - Enabling the Service

To use full duplex functionality of WCF in IIS, with all of the strong session control that we desire, we need to enable the net.tcp protocol. You only have to go through these steps if you want to host your net.tcp service in IIS. So here are the steps:
  1. Enable "Windows Communication Foundation Non-HTTP Activation": Control Panel -> Programs and Features -> Turn Windows features on or off Windows Features - Enabling .NET 3.0 (WCF Services)
  2. Enable the "Net.Tcp Protocol" in your web site: IIS (7.0) Admin -> Right Click on your Web Site -> Manage Website -> Advanced Settings... Enabling the 'net.tcp' protocol in a web site
  3. Configure the "Site Bindings" in IIS: Editing 'Site Bindings' in IIS7 to configure the 'net.tcp' protocol
Now that those basic steps have been taken, we can move forward with making our WCF server application. We're going to copy much of the code in the previous article, with the exception that when a user connects, we'll add them to a collection of "connected users" so that we can broadcast a message to them later from a web page.
In the previous article, we configured our WCF server in the App.config file. The configuration sections are going to be almost exactly the same, but because this is a web application we'll put them in the Web.config.

Configuring our WCF Service

One key difference between hosting the WCF service ourselves and hosting in IIS is that we don't have to specify the endpoint address in the web.config file. Notice the slight difference between the config files:
<!-- Hosting in a Windows app: -->
<service name="WCF_Server.SingingEelsServer">
   <endpoint
       address="net.tcp://localhost:12345/EelsServer"
       binding="netTcpBinding"
       bindingConfiguration="InsecureTcp"
       contract="WCF_Interface.ISingingEelsServer" />
</service>

<!-- Hosting in a IIS: -->
<service name="WCF_WebServer.SingingEelsServer">
   <endpoint
       address=""
       binding="netTcpBinding"
       bindingConfiguration="InsecureTcp"
       contract="WCF_Interface.ISingingEelsServer" />
</service>
A final difference is in our client app config which will now point to the ".svc" file in our web application. The port "12345" was configured in IIS above, and the path "/SingingEelsServer.svc" is the file name in our web application.
<client>
   <endpoint
       name="SingingEelsWcfServer"
       address="net.tcp://localhost:12345/SingingEelsServer.svc"
       binding="netTcpBinding"
       contract="WCF_Interface.ISingingEelsServer"
       bindingConfiguration="InsecureTcp" />
</client>
Now that our initial setup is done and our configuration is done, the rest of the code is exactly as in the previous article when it comes to how we communicate between the client and the server. One final difference is that we don't have to "start" our listener manually because IIS will do this for us. Essentially this code is obsolete:
// This code is no longer needed in our server because IIS will handle
// starting the service for us (and hosting it).

using (ServiceHost host = new ServiceHost(Program.serviceInstance))
{
   host.Open();

   // ...

}
That's really all that can be said without actually digging into the code yourself, so here is the complete solution including the WCF client app, a shared class library and the WCF server web app. Check out the "broadcast message" functionality in the web site: SingingEels_WCF_WebServer.zip
Reference:

No comments:

Post a Comment