WCF: Hosting non-http protocols in IIS 7.0

The new IIS 7.0 allows hosting of multiple protocols. I experimented with hosting a WCF service with Tcp and Http endpoints in IIS 7.0.

I started with creating a new Service Web Project in VS.2005. The project template gives you a Service with one method that echo’s the string back prefixed by “Hello:”. I added the <serviceMetadata httpGetEnabled=”true”/> to the web.config to allow meta data exchange using a http get (for instance a browser).

 Then I created a WinForms client application and referenced the Service url to create the proxy. The client has one textbox and a button. The button-click handler creates a service proxy using the default ctor and calls the web service with the string entered in the textbox. The result returned by the service is displayed in a MessageBox.

So, now I have a simple, plain vanilla, out-of-the-box service and client. I can test if everything is working and should see a message box popup after I pushed the button on the form of the client.

Now we need to configure IIS 7.0 to handle the Tcp protocol as well. It turns out there is no UI in Vista to do this (there should be in Windows Server 2008). But luckily there is a command line tool you can use to get this done. Here’s the command line:

%windir%system32inetsrvappcmd.exe set site “Default Web Site” /+bindings.[protocol=’net.tcp’,bindingInformation=’8080:*’]

This command line adds a net.tcp protocol handler to IIS 7.0 (called a binding, not to be mistaken for a WCF binding, which is a different thing altogether) that listens on port 8080. The ‘*’ is a wildcard for the host name: so this handler will handle all tcp traffic on port 8080 no matter the host name specified.

This is a global setting to the “Default Web Site”. Our web application that runs in IIS still has to be told to use that IIS-binding: the tcp handler for port 8080. Here’s the command line to do that:

%windir%system32inetsrvappcmd.exe set app “Default Web Site/<MyAppName>” /enabledProtocols:http,net.tcp

 This will enable the specified protocols for the specified application. Note: replace <MyAppName> with your actual web application name of the service project you created in the beginning.

Important: You have to have admin rights to successfully run these command lines or you’ll get an access denied error.

If you try to run the client again it should still work. But realize that it is still connecting using the http protocol. Now add a new endpoint to the web.config that uses a netTcpBinding (I just copied the existing endpoint and replaced the binding).

Now we have added the tcp endpoint to the service, it is time to update the client. The easiest way to get the tcp config settings into your client’s app.config file is to update the Service Reference in VS.NET.

Dont try to run the client now: you’ll get an exception. The reason is that there are now two endpoint elements (under client element) and you have to tell the service proxy what endpoint configuration to use. So enter the bindingConfiguration value in the ctor of the service proxy (starting with “WsHttpBinding” or “NetTcpBinding”). The client should work on either endpoint.

If you need more information on the subject check out this msdn magazine article:

Comments are closed.