Creating Routing Service using WCF 4.0, .NET Framework 4.0 and Visual Studio 2010 RC

Posted by: Mahesh Sabnis , on 2/26/2010, in Category Windows Communication Foundation (WCF)
Views: 90559
Abstract: The Routing mechanism WCF 4.0 provides an excellent feature for developing SOA based application. Now the client application is free to send request to any WCF service based upon filters.
I hope most of you must have already started exploring WCF 4.0. Microsoft recently released Visual Studio 2010 RC and you can download it from here. With WCF 4.0 on .NET 4.0, new features have been introduced for application development. Routing provides the mechanism of isolating one or more WCF services directly accessible from the client application. Client application have knowledge of only one WCF service which further routes call from the client to the specific WCF service, based upon filters. The architecture can be described as shown below:
Architecture
In the setup shown above, the client connects to WCF Routing services hosted on ‘172.100.10.30’, and then based upon filters passed by the client, the request is navigated to the concerned web service. I have already posted an article earlier on this topic which is based on WCF 4.0 Beta 1.
In this article I will be posting only the App.Config file of the routing service which is as shown below:
<?xmlversion="1.0"encoding="utf-8" ?>
<configuration>
      <system.serviceModel>
            <services>
                  <servicename="System.ServiceModel.Routing.RoutingService"behaviorConfiguration="MyRoutedServBehave">
                        <host>
                              <baseAddresses>
                                    <add baseAddress="http://localhost:5643/routingservice/router"/>
                              </baseAddresses>
                        </host>
                        <endpoint
                               address="twoway-ws"
                               binding="wsHttpBinding"
                               name="MyRoutingEndpoint"
                               contract="System.ServiceModel.Routing.IRequestReplyRouter"/>
                  </service>
            </services>
            <behaviors>
                  <serviceBehaviors>
                        <behaviorname="MyRoutedServBehave">
                              <serviceMetadatahttpGetEnabled="True"/>
                              <serviceDebugincludeExceptionDetailInFaults="True"/>
                              <routing filterTableName="ServiceRouterTable"/>
                              <!--The Router Table Contains Entries for services-->
                        </behavior>
                  </serviceBehaviors>
            </behaviors>
 
            <!--Define Services Here-->
            <client>
                  <endpoint
                         name="WCF_QtrwiseSalesService"binding="wsHttpBinding"
                         address="http://localhost/WCF40_QtrwiseSalesVD/CQtrwiseSales.svc"
                         contract="*">
                  </endpoint>
 
                  <endpointname="WCF_ServiceSalesData"binding="wsHttpBinding"
                                 address="http://localhost/WCF40_SalesDataVD/CSalesData.svc"
                                 contract="*"></endpoint>
             
            </client>
 
            <!--Routing Defination-->
            <routing>
     
                  <!--Filter For Detecting Messages Headers to redirect-->
                  <filters>
                        <filtername="GetSalesDetsils_Filter_1" filterType="Action"filterData="http://tempuri.org/IQtrwiseSales/GetSalesDetsils"/>
                        <filtername="GetSalesDetsils_Filter_2"filterType="Action"filterData="http://tempuri.org/ISalesData/GetSalesDetsils"/>
                  </filters>
 
     
                  <!--Define Routing Table, This will Map the service with Filter-->
 
      <filterTables>
        <filterTablename="ServiceRouterTable">
          <addfilterName ="GetSalesDetsils_Filter_1"endpointName="WCF_QtrwiseSalesService"/>
          <addfilterName="GetSalesDetsils_Filter_2"endpointName="WCF_ServiceSalesData"/>
        </filterTable>
      </filterTables>
     
             
            </routing>
      </system.serviceModel>
</configuration
 
The Address provided under the <client> tag, are WCF services, where the routing service will route request from the client application.
Code in the host application is as below:
C#
static void Main(string[] args)
{
    ServiceHost Host = new ServiceHost(typeof(RoutingService));
    try
    {
        Host.Open();
        Console.WriteLine("Routing Service is Started...............");
        Console.ReadLine();
        Host.Close();
 
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    Console.ReadLine();
}
VB.NET
Shared Sub Main(ByVal args() As String)
      Dim Host As New ServiceHost(GetType(RoutingService))
      Try
            Host.Open()
            Console.WriteLine("Routing Service is Started...............")
            Console.ReadLine()
            Host.Close()
 
      Catch ex As Exception
            Console.WriteLine(ex.Message)
      End Try
      Console.ReadLine()
End Sub
On the client application side, write the following configuration:
<?xmlversion="1.0"encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <client>
 
            <endpointaddress="http://localhost:5643/routingservice/router/twoway-ws"
              binding="wsHttpBinding"
                    contract="MyRefQtrwiseSales.IQtrwiseSales"
              name="clientEdpQtrwiseSales" />
            <endpointaddress="http://localhost:5643/routingservice/router/twoway-ws"
                binding="wsHttpBinding"
                      contract="MyRefSalesData.ISalesData"
                name="clientEdpSalesData" />
 
        </client>
    </system.serviceModel>
</configuration>
Note: The client application must have the knowledge of WCF services to which it needs to send requests. The entire source code of this article can be downloaded over here

Conclusion: The Routing mechanism WCF 4.0 provides an excellent feature for developing SOA based application. Now the client application is free to send request to any WCF service based upon filters.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Mahesh Sabnis is a DotNetCurry author and a Microsoft MVP having over two decades of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions), and Front-end technologies like Angular and React. Follow him on twitter @maheshdotnet or connect with him on LinkedIn


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Shivaji varma on Saturday, January 1, 2011 8:01 AM
Sir,
I was writing code for chatbox.i hope you can help me out.
Here is the problem:

I need to route to a mesg chatbox behind router on internet.

I was able to write code for chat inside a network(ip required). how can i extend it behind router.
Comment posted by Miguel Merayo on Friday, March 18, 2011 2:43 AM
Hi,
Interesting article. I have been trying to use the service router to route a WCF claims-aware service but I think this is impossible, could you please tell me your opinion?, do you know any way to make the scenario work? and, How could I configure the RoutingService to route one WIF STS service?
Thanks in advance
Comment posted by Vijaya Anand on Tuesday, August 30, 2011 6:17 AM
Thanks for sharing the article. The Routing service that ships with WCF 4.0 is a nice one, I too wrote a small article about that subject if you are interested you can find at here http://www.prideparrot.com/blog/archive/2011/8/routing_service_and_content_based_routing
Comment posted by Anish Jain on Monday, December 31, 2012 6:57 AM
Hi,
   Can you send project that route service.
   Thank you
Comment posted by Leela kumar on Tuesday, July 9, 2013 6:59 AM
I want to work Router with Http and net.tcp protocal ,I was develop Router with Http and Net.Tcp Protocal,but this Router process only one request ,2 end request it will hang the router ...,pls suggest me...  
Comment posted by Leela kumar on Tuesday, July 9, 2013 7:00 AM
I want to work Router with Http and net.tcp protocal ,I was develop Router with Http and Net.Tcp Protocal,but this Router process only one request ,2 end request it will hang the router ...,pls suggest me...  
Comment posted by arun Tripathy on Friday, March 27, 2015 12:34 AM
In case of normal way of consuming WCF service Service Contract, dataContract, FaultContract etc available to client. But in case of WCF routing service on IReuestReplyRouter is available. How to access DataContract, ServiceContract etc.