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)
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:
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.
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!
Was this article worth reading? Share it with fellow developers too. Thanks!
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