Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

Hosting the service in IIS using the TCP protocol

Save for later
  • 8 min read
  • 30 Oct 2014

article-image

In this article by Mike Liu, the author of WCF Multi-layer Services Development with Entity Framework, Fourth Edtion, we will learn how to create and host a service in IIS using the TCP protocol.

(For more resources related to this topic, see here.)

Hosting WCF services in IIS using the HTTP protocol gives the best interoperability to the service, because the HTTP protocol is supported everywhere today. However, sometimes interoperability might not be an issue. For example, the service may be invoked only within your network with all Microsoft clients only. In this case, hosting the service by using the TCP protocol might be a better solution.

Benefits of hosting a WCF service using the TCP protocol

Compared to HTTP, there are a few benefits in hosting a WCF service using the TCP protocol:

  • It supports connection-based, stream-oriented delivery services with end-to-end error detection and correction
  • It is the fastest WCF binding for scenarios that involve communication between different machines
  • It supports duplex communication, so it can be used to implement duplex contracts
  • It has a reliable data delivery capability (this is applied between two TCP/IP nodes and is not the same thing as WS-ReliableMessaging, which applies between endpoints)

Preparing the folders and files

First, we need to prepare the folders and files for the host application, just as we did for hosting the service using the HTTP protocol. We will use the previous HTTP hosting application as the base to create the new TCP hosting application:

  1. Create the folders:

    In Windows Explorer, create a new folder called HostIISTcp under C:SOAwithWCFandEFProjectsHelloWorld and a new subfolder called bin under the HostIISTcp folder. You should now have the following new folders: C:SOAwithWCFandEFProjectsHelloWorld HostIISTcp and a bin folder inside the HostIISTcp folder.

  2. Copy the files:

    Now, copy all the files from the HostIIS hosting application folder at C:SOAwithWCFandEFProjectsHelloWorldHostIIS to the new folder that we created at C:SOAwithWCFandEFProjectsHelloWorldHostIISTcp.

  3. Create the Visual Studio solution folder:

    To make it easier to be viewed and managed from the Visual Studio Solution Explorer, you can add a new solution folder, HostIISTcp, to the solution and add the Web.config file to this folder. Add another new solution folder, bin, under HostIISTcp and add the HelloWorldService.dll and HelloWorldService.pdb files under this bin folder.

  4. Add the following post-build events to the HelloWorldService project, so next time, all the files will be copied automatically when the service project is built:
    xcopy "$(AssemblyName).dll" "C:SOAwithWCFandEFProjectsHelloWorldHostIISTcpbin" /Y
    xcopy "$(AssemblyName).pdb" "C:SOAwithWCFandEFProjectsHelloWorldHostIISTcpbin" /Y

  5. Modify the Web.config file:

    The Web.config file that we have copied from HostIIS is using the default basicHttpBinding as the service binding. To make our service use the TCP binding, we need to change the binding to TCP and add a TCP base address. Open the Web.config file and add the following node to it under the <system.serviceModel> node:

    <services>
    <service name="HelloWorldService.HelloWorldService">
       <endpoint address="" binding="netTcpBinding"
       contract="HelloWorldService.IHelloWorldService"/>
       <host>
         <baseAddresses>
           <add baseAddress=
           "net.tcp://localhost/HelloWorldServiceTcp/"/>
         </baseAddresses>
       </host>
    </service>
    </services>

In this new services node, we have defined one service called HelloWorldService.HelloWorldService. The base address of this service is net.tcp://localhost/HelloWorldServiceTcp/. Remember, we have defined the host activation relative address as ./HelloWorldService.svc, so we can invoke this service from the client application with the following URL: http://localhost/HelloWorldServiceTcp/HelloWorldService.svc.

For the file-less WCF activation, if no endpoint is defined explicitly, HTTP and HTTPS endpoints will be defined by default. In this example, we would like to expose only one TCP endpoint, so we have added an endpoint explicitly (as soon as this endpoint is added explicitly, the default endpoints will not be added). If you don't add this TCP endpoint explicitly here, the TCP client that we will create in the next section will still work, but on the client config file you will see three endpoints instead of one and you will have to specify which endpoint you are using in the client program.

The following is the full content of the Web.config file:

<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET 
application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web>    <compilation debug="true" targetFramework="4.5"/>    <httpRuntime targetFramework="4.5" /> </system.web>   <system.serviceModel>    <serviceHostingEnvironment >      <serviceActivations>        <add factory="System.ServiceModel.Activation.ServiceHostFactory"          relativeAddress="./HelloWorldService.svc"          service="HelloWorldService.HelloWorldService"/>      </serviceActivations>    </serviceHostingEnvironment>      <behaviors>      <serviceBehaviors>        <behavior>          <serviceMetadata httpGetEnabled="true"/>        </behavior>      </serviceBehaviors>    </behaviors>    <services>      <service name="HelloWorldService.HelloWorldService">        <endpoint address="" binding="netTcpBinding"         contract="HelloWorldService.IHelloWorldService"/>        <host>          <baseAddresses>            <add baseAddress=            "net.tcp://localhost/HelloWorldServiceTcp/"/>          </baseAddresses>        </host>      </service>    </services> </system.serviceModel>   </configuration>

Enabling the TCP WCF activation for the host machine

By default, the TCP WCF activation service is not enabled on your machine. This means your IIS server won't be able to host a WCF service with the TCP protocol. You can follow these steps to enable the TCP activation for WCF services:

  1. Go to Control Panel | Programs | Turn Windows features on or off.
  2. Expand the Microsoft .Net Framework 3.5.1 node on Windows 7 or .Net Framework 4.5 Advanced Services on Windows 8.
  3. Check the checkbox for Windows Communication Foundation Non-HTTP Activation on Windows 7 or TCP Activation on Windows 8.

    The following screenshot depicts the options required to enable WCF activation on Windows 7:

    hosting-service-iis-using-tcp-protocol-img-0

    Unlock access to the largest independent learning library in Tech for FREE!
    Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
    Renews at €18.99/month. Cancel anytime

    The following screenshot depicts the options required to enable TCP WCF activation on Windows 8:

    hosting-service-iis-using-tcp-protocol-img-1

  4. Repair the .NET Framework:

    After you have turned on the TCP WCF activation, you have to repair .NET. Just go to Control Panel, click on Uninstall a Program, select Microsoft .NET Framework 4.5.1, and then click on Repair.

Creating the IIS application

Next, we need to create an IIS application named HelloWorldServiceTcp to host the WCF service, using the TCP protocol. Follow these steps to create this application in IIS:

  1. Open IIS Manager.
  2. Add a new IIS application, HelloWorldServiceTcp, pointing to the HostIISTcp physical folder under your project's folder.
  3. Choose DefaultAppPool as the application pool for the new application. Again, make sure your default app pool is a .NET 4.0.30319 application pool.
  4. Enable the TCP protocol for the application. Right-click on HelloWorldServiceTcp, select Manage Application | Advanced Settings, and then add net.tcp to Enabled Protocols. Make sure you use all lowercase letters and separate it from the existing HTTP protocol with a comma.

    hosting-service-iis-using-tcp-protocol-img-2

Now the service is hosted in IIS using the TCP protocol. To view the WSDL of the service, browse to http://localhost/HelloWorldServiceTcp/HelloWorldService.svc and you should see the service description and a link to the WSDL of the service.

Testing the WCF service hosted in IIS using the TCP protocol

Now, we have the service hosted in IIS using the TCP protocol; let's create a new test client to test it:

  1. Add a new console application project to the solution, named HelloWorldClientTcp.
  2. Add a reference to System.ServiceModel in the new project.
  3. Add a service reference to the WCF service in the new project, naming the reference HelloWorldServiceRef and use the URL http://localhost/HelloWorldServiceTcp/HelloWorldService.svc?wsdl.

    hosting-service-iis-using-tcp-protocol-img-3

  4. You can still use the SvcUtil.exe command-line tool to generate the proxy and config files for the service hosted with TCP, just as we did in previous sections. Actually, behind the scenes Visual Studio is also calling SvcUtil.exe to generate the proxy and config files.
  5. Add the following code to the Main method of the new project:
    var client = new HelloWorldServiceRef.HelloWorldServiceClient ();
    Console.WriteLine(client.GetMessage("Mike Liu"));

  6. Finally, set the new project as the startup project.

Now, if you run the program, you will get the same result as before; however, this time the service is hosted in IIS using the TCP protocol.

Summary

In this article, we created and tested an IIS application to host the service with the TCP protocol.

Resources for Article:


Further resources on this subject: