Routing In WCF

In this article we will discuss about the basics of Routing in WCF services. This concept aims at hiding the actual services from a client applications and making them sending the requests to a single common service. Based on the request being sent to the common service, it is routed to the appropriate, one or more services, which were actually intended to receive the request from the client application. So such a scenario looks like the following: 
 
Some of the major advantages of this concept include:
  • Service versioning:

    This means, create multiple versions of a service, one after another. Old clients will keep using the older version and new clients can be routed to the new service, using the router service. Once the old clients are ready to use the new service, pass appropriate version request in the request message.

  • Protocol bridging:

    This includes the scenario, where the client supports different protocols and the services support different protocols. In such a case, create a service which can communicate with the client application as per their protocol type support and route the request to the appropriate service, based on the request where it is intended to be.

  • Error handling:

    This allows creation of back-up list for the service that should receive the requests, in case there is any failure in processing the request by a service.
Refer to the following msdn link, for some more features that this routing service can provide: https://msdn.microsoft.com/en-us/library/ee517423%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

How the router service works ?
 
Now the question arises, how we can create a router service, which can perform these tasks. For this, the major task is to define the configuration for the routing service, based on which the request will be routed to the appropriate service. This configuration include following major steps:
 
1. Configure the router service with it's end point and behavior configuration: This includes defining the endpoint where it can be accessed and protocol that can be used for this service and it's contract type. So it would look like the following:
  1. <services>  
  2.   
  3.      <service name="System.ServiceModel.Routing.RoutingService"  
  4.                  behaviorConfiguration="RoutingSvcBehavior">  
  5.        <host>
             <baseAddresses>
                <add baseAddress="http://localhost:5643/RoutingSvc"/>
             </baseAddresses>
          </host>

  6.        <endpoint  
  7.                  address=""  
  8.                   binding="wsHttpBinding"  
  9.                   name="RoutingEndpoint"  
  10.                   contract="System.ServiceModel.Routing.IRequestReplyRouter"/>  
  11.      </service>  
  12.    </services>  
  13.   
  14.    <behaviors>  
  15.      <serviceBehaviors>  
  16.        <behavior name="RoutingSvcBehavior">  
  17.          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>  
  18.          <serviceDebug includeExceptionDetailInFaults="false"/>  
  19.           <routing filterTableName="SvcRouterTable"/>
  20.        </behavior>  
  21.      </serviceBehaviors>  
  22.    </behaviors>  
Notice the contract type defined for the routing service. This can be from one of the following 4 types only and type to be used depends on the message exchange pattern to be used between the services i.e. request-reply or duplex patterns etc. These are: 
  • IDuplexSessionRouter
  • IRequestReplyRouter
  • ISimplexDatagramRouter
  • ISimplexSessionRouter
In our case, we are going to use the request-reply mode, so we are using the IRequestReplyRouter. Further there is the a tag named routing, which acts as the core logic of this routing concept. We will discuss about these further.
 
Next we add list of endpoints in the client tag, which is nothing but the endpoints of the services, which are intended to receive the request from the client application. It will look like the following: 
  1. <client>  
  2.       <endpoint  
  3.               name="Service_1" binding="wsHttpBinding"  
  4.               address="http://localhost:12354/Service1.svc"  
  5.               contract="*">  
  6.       </endpoint>  
  7.       <endpoint  
  8.               name="Service_2" binding="wsHttpBinding"  
  9.               address="http://localhost:34343/Service_2.svc"  
  10.               contract="*">  
  11.       </endpoint>   
  12.     </client>  
So in the above code, we have defined endpoints of two services Service_1 and Service_2, for which the router service will work.
 
Next, we will define the routing table, with filters, based on which a request is redirected to an appropriate service, for which it is actually intended to be. This section will look like the following:  

  1. <routing>   
  2.      <filters>  
  3.        <filter name="MyFilter_1"  filterType="Action" filterData="http://localhost:64832/IMyService/GetData"/>  
  4.        <filter name="MyFilter_2" filterType="Action" filterData="http://localhost:64832/IMyService/GetData2"/>
  5.      </filters>   
  6.      <filterTables>  
  7.        <filterTable name="ServiceRouterTable">   
  8.          <add filterName="MyFilter_1" endpointName="Service_1"/>    
  9.          <add filterName="MyFilter_2" endpointName="Service_2"/>
  10.        </filterTable>  
  11.      </filterTables>  
  12. </routing>  

Here, the filters tag contains the type of filter which can be applied to the incoming request. Based on the filter which matches the incoming request, it's corresponding endpoint address is picked from the filterTables tag and the endpoint is selected from the client endpoints, defined in client section we discussed above, and request is routed to that service. The filterType can be one of the following: 
  • Action
  • EndpointAddress
  • EndpointAddressPrefix 
  • And
  • Custom
  • EndpointName
  • MatchAll
  • XPath 
So this was the basic configuration for the routing service. Next we simply define the client services and the client application. Client application will be configured to make the requests to the router service. In next article, we will practically implement the concept of  routing service. Hope you enjoyed reading it. Happy coding...!!! 
 
Read more articles on WCF:


Similar Articles