Click here to Skip to main content
15,860,861 members
Articles / .NET

Router Service in WCF 4.0

Rate me:
Please Sign up or sign in to vote.
4.64/5 (4 votes)
28 Sep 2011CPOL2 min read 23.7K   8   4
Router represents a logical connection between inbound and outbound endpoints

Introduction

MS.NET 4.0 Technology represents a foundation technology for metadata driven model applications. This article focuses on one of the unique components from the WCF 4 model for logical connectivity called Routing Service. We will try to extend its usage for enterprise application driven by metadata stored in the Runtime Repository. The basic idea behind routing service is to route messages based on message content. It is also a good tool to design a load balancer in your enterprise application architecture. I am going to cover a simple "how to design a simple routing service".

WCF Routing Service allows the following Message Exchange Pattern (MEP) with contract options:

  • OneWay (SessionMode.Required, SessionMode.Allowed, TrasactionFlowOption.Allowed)
  • Duplex (OneWay, SessionMode.Required, CallbackContract, TrasactionFlowOption.Allowed)
  • RequestResponse (SessionMode.Allowed, TrasactionFlowOption.Allowed)

In addition to the standard MEPs, the Routing Service has a built-in pattern for Multicast messaging and Error handling. The following image describes the complete in & out flow.

The router process is very straightforward, where the message received by inbound endpoint is forwarding to the outbound endpoint or multiple endpoints based on the prioritized rules represented by message filter types. The evaluation rules are started by highest priority. The router complexity depends on the number of inbound and outbound endpoints, type of message filters, MEPs and priorities.

Note that the MEP pattern between the routed inbound and outbound endpoints must be the same, for example: the OneWay message can be routed only to the OneWay endpoint, therefore for routing Request/Response message to the OneWay endpoint, the service mediator must be invoked to change the MEP and then back to the router.

Using the Code

The following code snippet describes how to configure router service endpoint.

XML
<service name="System.ServiceModel.Routing.RoutingService" 
	behaviorConfiguration="RoutingService_Behavior">
        <host>
           <baseAddresses>
              <add baseAddress="http://[your service address]/RoutingService/Router" />
           </baseAddresses>
        </host>

        <endpoint
           name="PatientManagement_Router"
           address="PatientManagement"
           binding="basicHttpBinding"
           contract="System.ServiceModel.Routing.IRequestReplyRouter"
        />
        <endpoint
           name="ClinicalsPatientManagement_Router"
           address="ClinicalsPatientManagement"
           binding="basicHttpBinding"
           contract="System.ServiceModel.Routing.IRequestReplyRouter"
        />
        <endpoint
           address="mex"
           binding="mexHttpBinding"
           contract="IMetadataExchange"
        />
     </service> 

Here, we will configure the service which will listen to the inbound call to router:

XML
<client>
     <clear/>
     <endpoint 
        name="PatientManagement_Client" 
        address="http://[your service address]/patientmanagementservice.svc" 
        binding="basicHttpBinding"
        contract="*"
     />
     <endpoint
        name="ClinicalsPatientManagement_Client"
        address="http://[your service address]/clinicalspatientmanagementservice.svc"
        binding="basicHttpBinding"
        contract="*"
     />
  </client> 

In the below config section, first we will declare the filter in <filters> section. In this, we will define the filter type and filter data.

In <filterTables> section, we will create filter table and here we will map the above created filter to the actual service (which is going to handle this type of incoming request).

XML
<routing>
     <filters>
        <filter name="PatientManagement_Filter" filterType="EndpointName" 
			filterData="PatientManagement_Router" />
        <filter name="ClinicalsPatientManagement_Filter" 
	    filterType="EndpointName" filterData="ClinicalsPatientManagement_Router"/>
     </filters>

     <filterTables>
        <filterTable name="RoutingService_FilterTable">
           <add filterName="PatientManagement_Filter" 
			endpointName="PatientManagement_Client" />
           <add filterName="ClinicalsPatientManagement_Filter" 
			endpointName="ClinicalsPatientManagement_Client"/>
        </filterTable>
      </filterTables>
  </routing>

Finally we will host the service, here I am using a sample console based application for hosting this service.

C#
using(var serviceHost = new ServiceHost
			(typeof(System.ServiceModel.Routing.RoutingService)))
{
    var endpointCounter = 0;
    Console.WriteLine("Routing Service configured, opening........");
    serviceHost.Open();
    Console.WriteLine("\r\nRouting Service is now running.....
    	and listening at following addresses");
    foreach (var endPoint in serviceHost.Description.Endpoints)
    {
        endpointCounter++;
        Console.WriteLine("\r\n" + endpointCounter + ".) 
        	" + endPoint.Address.ToString());
    }
    Console.WriteLine("\r\nPress <ENTER> to terminate router.");
    Console.ReadLine();
};

Disclaimer

The information in this article is provided “AS IS” with no warranties, and confers no rights. This article does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion.

All data and information provided in this article is for informational purposes only. I makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSource code ??? Pin
Pardhac20-Dec-12 20:36
Pardhac20-Dec-12 20:36 
BugImage link is broken Pin
Thiago_Lima4-Sep-12 6:16
Thiago_Lima4-Sep-12 6:16 
GeneralMy vote of 2 Pin
Anders Lybecker3-Oct-11 19:17
Anders Lybecker3-Oct-11 19:17 
GeneralMy vote of 4 Pin
Wooters28-Sep-11 5:35
Wooters28-Sep-11 5:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.