Share via


Exercise 8: Protocol Bridging

WCF4 includes a new routing service found in the System.ServiceModel.Routing namespace. The Routing Service is designed to act as a generic, configurable SOAP intermediary. It allows you to configure Content Based Routing, set up Protocol Bridging, and handle communication errors that you encounter. The Routing Service also makes it possible for you to update your Routing Configuration while the Routing Service is running without restarting the service.

Figure 28

The routing service bridging http and net.tcp

Imagine that you had a client application that can only communicate using basic http and at the same time, you have a service that communicates using net.tcp. There are many ways to solve this problem. For example, with WCF you can have more than one endpoint for a service so it would be possible to expose an endpoint using basicHttpBinding for the client.

With WCF4 System.ServiceModel.Routing you now have another way to solve this problem. In this exercise, you will use the routing service to bride the http and net.tcp protocol by simply configuring the routing service.

Task 0 – Opening the Solution

This exercise uses a new begin solution

  1. Open the starting solution for Exercise 8 located under the Source\Ex8-ProtocolBridging\Begin (choosing the folder that matches the language of your preference.) Use it as the starting point for this exercise.
  2. Press CTRL+SHIFT+B to build the solution.

Task 1 – Trying the Calculator

In this task, you will explore the calculator solution.

  1. The solution contains two projects a CalculatorService and CalculatorClient project.
  2. Open the app.config file from the CalculatorService project. Note the base address of the CalculatorService is using a net.tcp address.

    App.config

    <service name="CalculatorService.CalculatorService"> <!-- ... --> <host> <baseAddresses> <add baseAddress="net.tcp://localhost:9090/CalculatorService" /> </baseAddresses> </host>

  3. The CalculatorClient project has a Service Reference to the CalculatorService project. Open the app.config file for the CalculatorClient project, and note the client section is also using net.tcp to communicate with the calculator service.

    App.config

    <client> <endpoint address="net.tcp://localhost:9090/CalculatorService" binding="netTcpBinding" bindingConfiguration="CalculatorService" contract="CalculatorServiceReference.ICalculatorService" name="CalculatorService" /> </client>

  4. The solution is set to use multiple startup projects. If this is not the case, right-click the solution node in Solution Explorer, and select Set StartUp Projects to configure them. Press CTRL+F5 to start the solution. This will launch the CalculatorService and CalculatorClient applications.

    Note:
    Windows Firewall may prompt you to allow the service access to use the network. It is safe to allow this.

  5. The CalculatorClient will be pre-loaded with random values. Press Invoke Service to invoke the service over net.tcp. You will see something similar to the following.

    Figure 29

    The Calculator Service and Client Application connected over TCP

Task 2 – Adding the Router Service

Your calculator works great if you are able to connect with net.tcp, but what if you had a requirement that the calculator application could access the calculator service over the Internet?

In this task, you will change the CalculatorClient to connect to a router service bridging the http and net.tcp protocols.

  1. Right-click the Ex8-ProtocolBriding solution, point to Add and select Existing Project. Browse to Source\Assets\RouterService (Choosing the folder that matches the language of your preference.) and add the RouterService project.
  2. The routing service has to be configured Open the Web.config file from the RouterService project.
  3. The routing service is both a service and a client. The first thing you will do is add the client endpoint and binding configuration to the Web.config file. Add the following configuration as shown.

    (Code Snippet - What is new in WCF4 Lab – RoutingService calc endpoint XML)

    Web.config

    <system.serviceModel>
    <bindings> <netTcpBinding> <binding name="CalculatorService" > <security mode="None" /> </binding> </netTcpBinding> </bindings> <client> <endpoint name="regularCalculatorEndpoint" address="net.tcp://localhost:9090/CalculatorService" binding="netTcpBinding" bindingConfiguration="CalculatorService" contract="*" /> </client>FakePre-f809f48008194bd2b3f81425e738afff-29e3f4f27af8406e9bbcb031f04b7bdb

  4. The routing service uses a filter table to map incoming messages to client endpoints. The next thing you need to do is to create the filter table and service behavior that will apply it in the configuration. To do this, add the following portion after the client section replacing the original behaviors node.

    (Code Snippet - What is new in WCF4 Lab – Routing service behavior XML)

    Web.config

      </client>
    <behaviors> <serviceBehaviors> <behavior name="routingConfiguration"> <routing filterTableName="filterTable1" /> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> <behavior name=""> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> <routing> <filters> <filter name="MatchAllFilter" filterType="MatchAll" /> </filters> <filterTables> <filterTable name="filterTable1"> <add filterName="MatchAllFilter" endpointName="regularCalculatorEndpoint" priority="0"/> </filterTable> </filterTables> </routing>FakePre-c8f325e226984a2f9cfb9435ba0c3173-764be79856a9463794d9e8f54fba733c

    Note:
    What does the filter table do?

    This filter table will match all requests and route them to the regularCalculatorEndpoint listening at net.tcp://localhost:9090/CalculatorService.

  5. The last thing you need to do is to add a service definition for the router service. Your router service will be listening on http and routing requests to the calculator service, which is listening on net.tcp – this effectively bridges the protocols. Add the following configuration after the routing section.

    (Code Snippet - What is new in WCF4 Lab – Routing service XML)

    Web.config

      </routing>
    <services> <service behaviorConfiguration="routingConfiguration" name="System.ServiceModel.Routing.RoutingService"> <endpoint address="general" binding="basicHttpBinding" name="routerEndpoint1" contract="System.ServiceModel.Routing.IRequestReplyRouter" /> </service> </services>FakePre-c3d00ec57394449eafe2abd411f169ab-1d2e4be6db9246838134120109904ff3

Task 3 – Modifying the Client to Enable Routed HTTP

  1. Your client application will need a client endpoint definition to use with the router service. Open the app.config file from the CalculatorClient project, and add the following configuration after the existing endpoint.

    (Code Snippet - What is new in WCF4 Lab – CalcClient endpoint XML)

    XML

    <endpoint address="https://localhost:8000/Router.svc/general" binding="basicHttpBinding" contract="CalculatorServiceReference.ICalculatorService" name="RouterService" />
    </client>

    Note:
    Named endpoints in configuration

    If your client section contains more than one endpoint definition, you will have to provide an endpoint name when creating a client proxy. Look at the CalculateResults method of MainWindow, which has been written to pass the endpoint name to the proxy constructor.

Next Step

Exercise 8: Verification