Thursday, December 27, 2012

WCF 4.0 Features - Three

So in WCF 4.0 Features - Two we discussed Dynamic Service and 
End Point discovery. Now let's move to next.

Routing service


This new feature introduces routing service between client and actual business service. This intermediate service Act as broker or gateways to the actual business services and provides features for content based routing, protocol bridging and error handling

 In the above figure,the Client application have knowledge of WCF Routing Service which further routes request to the specific services WCF Service1 or WCF Service2 or WCF Service3.

Understanding the Routing Service

 The Routing Service is implemented as a Windows Communication Foundation (WCF) service in the System.ServiceModel.Routing namespace.In order to implement the WCF Routing Service .NET framework 4.0 offers a few in-build contracts as follows
  1. ISimplexDatagramRouter: It is a one way model for which session is not mandatory. Ideal for performing tasks like logging, message queuing, etc.
  2. ISimplexSessionRouter: It is one way but works on a session.
  3. IRequestReplyRouter: It is the usual client message request and service response model.
  4. IDuplexSessionRouter: It works over session and supports performing callbacks to the client
Message Filters are used by the WCF routing service to consider which part of the message has to be matched in order to perform the routing to the respective service. Below are some of the available message filters.

  1. Action 
  2. EndpointName
  3. EndpointAddress 
  4. XPATH
  5. Even a Custom one can be created

Sample Code …Create a WCF service project, delete all the .cs files and add the reference to the library System.ServiceModel.Routing and add following configuration file :

<system.serviceModel>
    <services>
        <service name="DemoRoutingService" behaviorConfiguration="MyRoutingServiceBehavior">
        <host>
            <baseAddresses>
                <add baseAddress="http://localhost:5555/RoutingService/DemoRouter"/>
            </baseAddresses>
        </host>
        <endpoint name="RequestReplyBindingEP" address="http://localhost:5555/RoutingService/DemoRouter" binding="wsHttpBinding" contract="System.ServiceModel.Routing.IRequestReplyRouter"></endpoint>
       </service>
    </services>

    <behaviors>
        <serviceBehaviors>
            <behavior name="MyRoutingServiceBehavior">
            <serviceMetadata httpsGetEnabled="True"/>
            <routing filterTableName="routingFilterTable"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <client>
        <endpoint name="GreetingService" address="http://localhost:6666/GreetingService/Greeting" binding="wsHttpBinding" contract="IGreetingService"></endpoint>
        <endpoint name="CalculatorService" address="net.tcp://localhost:6666/CalculatorService/Calculator" binding="netTcpBinding" contract="ICalculatorService"></endpoint>
    </client>
    <routing>
        <filters>
         <filter name="EPFilter" filterType="EndpointName" filterData="RequestReplyBindingEP"/>
          <filter name="EPAddressFilter" filterType="EndpointAddress" filterData="http://localhost:5555/RoutingService/DemoRouter"/>
        </filters>
        <filterTables>
            <filterTable name="routingFilterTable">
            <add filterName="EPFilter" endpointName="GreetingService"/>
            <add filterName="EPAddressFilter" endpointName="CalculatorService"/>
            </filterTable>
        </filterTables>
    </routing>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>

Below is detailed the sequence for the routing service configuration

1.Add routing service endpoints.

2.Add the routing service behavior along with a filter table name.

3.Add client service endpoints

4.Add the MessageFilters with filterType and filterData

5.Link the message filter with respective client endpoints through filterTable.

There is also an interesting concept called back up endpoints. The messages are routed to these back up endpoints when the main client WCF service endpoint routing fails for some reason. There can be multiple backup endpoints specified and the fail over will happen in the provided order. This is same what is mentioned in WCF 4.0 Features.

In this article, We discussed routing service.In later articles, I will cover in detail other features as well. Till then, thanks for reading...



Courtesy: Random Web Images, MSDN, Microsoft .NET 4.0 WCF Book, Several online resources 
 

No comments:

Post a Comment