Introduction to WCF - Part 4 - Non-HTTP Bindings

04 May 2011

As stated in part 1 of this series of posts, one of the key goals for WCF is loose-coupling from underlying service bindings. So far we've used HTTP bindings exclusively. The following short example shows how easy to it is configure (and re-configure) service bindings. We'll create an AppFabric-hosted service that works over TCP. We'll then re-configure it to use named pipes.

TCP Service Hosted in IIS

  1. Create a new WCF Service Application project:
  2. Delete the auto-generated service and create a new service called HelloTcpService which implements IHelloTcpService:
  3. namespace WcfTCPService
    {
        public class HelloTcpService : IHelloTcpService
        {
            public string Hello()
            {
                return "Hello TCP";
            }
        }
    }
    

  4. Modifying the web.config file (either manually or using the WCF Configuration Editor). Notice that we're using the netTcpBinding and have specified a TCP baseAddress:
  5. <system.serviceModel>
      <services>
        <service name="WcfTCPService.HelloTcpService">
          <endpoint address="" 
                    binding="netTcpBinding" 
                    contract="WcfTCPService.IHelloTcpService" />
          <host>
            <baseAddresses>
              <add baseAddress="net.tcp://localhost:9004" />
            </baseAddresses>
          </host>
        </service>
      </services>
    

  6. Build the project then start the IIS manager console app
  7. Create a new Application called WcfTcpService and point the physical path to C:\...\Projects\WcfTCPService\WcfTCPService
  8. As described previously, edit the permissions for the new service
  9. Now for the "magic" bit! Make sure WcfTcpService is selected in the left-hand Connections pane, then click Advanced Settings (under Actions).

    The Enabled Protocols property will already be set to htpp.

    Edit the field so that it reads net.tcp,htpp:
  10. The reason we need http as well as net.tcp is that we’ll be doing MEX queries over http when we setup the client application
  11. Click OK to close the Advanced Settings dialog, then confirm access to the service over HTTP at: http://localhost/WcfTcpService/HelloTcpService.svc

TCP Service Client

  1. Create a simple WPF client app containing a button and listbox and add a service reference (http://localhost/WcfTcpService/HelloTcpService.svc) as we’ve done previously
  2. Add the following code to the button's click event handler:
  3. private void button1_Click(object sender, RoutedEventArgs e)
    {
      ServiceReference1.HelloTcpServiceClient proxy = new 
        ServiceReference1.HelloTcpServiceClient();
      
      listBox1.Items.Add("Address: " + proxy.Endpoint.Address);
      listBox1.Items.Add("Binding: " + proxy.Endpoint.Binding.ToString());
      listBox1.Items.Add("URI: "     + proxy.Endpoint.ListenUri.ToString());
      listBox1.Items.Add("Output: "  + proxy.Hello());
    }
    

  4. Run the application - you should see something similar to the following:

Re-Configuring Bindings

We'll now see how easy it is to re-configure the service and client to use different bindings.

  1. Modify the service’s web.config as follows:
  2. <service name="WcfTCPService.HelloTcpService">
      <endpoint address="" 
                binding="netNamedPipeBinding" 
                contract="WcfTCPService.IHelloTcpService" />
      <host>
        <baseAddresses>
          <add baseAddress="net.pipe://localhost:9004" />
        </baseAddresses>
      </host>
    </service>
    

  3. Notice that we modified the endpoint binding from netTcpBinding to netNamedPipeBinding and changed the base address from net.tcp to net.pipe
  4. In the client app, update the service reference by right-clicking the reference and selecting Update Service Reference - no other modifications are necessary (it is necessary to update the service reference so that the auto-generated proxy code uses the named pipe binding)
  5. Finally, modify the Enabled Protocols in IIS Advanced Settings for the service - remove net.tcp and substitute net.pipe:
  6. Re-run the client app - you should see:

This concludes part four of our introduction to WCF. In part five we'll take a look at exposing and consuming multiple endpoints.