Duplex WCF Services Hosted in IIS Using Net.Tcp
(
Oct 11 2008 - 07:18:04 PM by
Timothy Khouri) - [
print article]
In the previous article (Client Server Programming with WCF), we saw how easy it was to create a client/server architecture with our server hosted in a Windows application (whether a console app, win-forms app or a Windows service). This article will build on top of that solution to host our server in IIS. You can download the source code at the end of the article.
In order to keep this project simple, we are only going to implement a very minimalistic client/server application that does authenticating, gets the time on the server and then disconnects. Because this is a full duplex connection (meaning, communication can go both ways), the server will also send a message to the connected client.
In the previous article, the server automatically sent "Hello there!!!" when the client connected. But, because this is a web application, we're going to go a step further. We'll make a simple page that allows us to send whatever message we want to all of the connected clients!
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:
- Enable "Windows Communication Foundation Non-HTTP Activation": Control Panel -> Programs and Features -> Turn Windows features on or off

- Enable the "Net.Tcp Protocol" in your web site: IIS (7.0) Admin -> Right Click on your Web Site -> Manage Website -> Advanced Settings...

- Configure the "Site Bindings" in IIS:

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:
<service name="WCF_Server.SingingEelsServer">
<endpoint
address="net.tcp://localhost:12345/EelsServer"
binding="netTcpBinding"
bindingConfiguration="InsecureTcp"
contract="WCF_Interface.ISingingEelsServer" />
</service>
<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:
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