Click here to Skip to main content
Click here to Skip to main content

Router Service in WCF 4.0

, 28 Sep 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

<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:

<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).

<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.

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)

Share

About the Author

No Biography provided

Comments and Discussions

 
GeneralSource code ??? PinmemberPardhac20-Dec-12 21:36 
BugImage link is broken PinmemberThiago_Lima4-Sep-12 7:16 
GeneralMy vote of 2 PinmemberAnders Lybecker3-Oct-11 20:17 
GeneralMy vote of 4 PinmemberWooters28-Sep-11 6:35 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 28 Sep 2011
Article Copyright 2011 by {var name = Amit}
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid