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