WCF 4.0: Protocol Bridging using Routing to Protect Internal Business Services

Posted by: Mahesh Sabnis , on 6/9/2011, in Category Windows Communication Foundation (WCF)
Views: 43767
Abstract: In this article, we are going to discuss protocol bridging mechanism using WCF 4.0 routing features.

In this article, we are going to discuss protocol bridging mechanism using WCF 4.0 routing features. In a previous article, I had written on introduction on Routing in WCF 4.0. Using this Routing feature, an organization can keep their core business service inaccessible from direct client application access. Routing services offer the following mechanisms:

  • Protocol Bridging.
  • Content based routing.
  • Backup list

In this article,I am going to demo Protocol Bridging feature using WCF routing. WCF 4.0 provides default support using the following service class:

System.ServiceModel.Routing.RoutingService

This service class implements the IRequestReplyRouter contract. Protocol Bridging can be explained with a simple diagram shown below:

WCF Protocol Bridging

Typically this scenario is implemented when your organization having various internal business services, wants to protect from getting accessed directly from a remote client. The routing service act as a bridge between client application and the actual service. The routing service accepts request message from the remote client using HTTP protocol (most of the times). The routing service has knowledge of the actual business services to which the communication takes place, using protocol like Tcp.

Creating Internal WCF Business Service

Step 1: Open VS2010 and create a blank solution, name it as ‘WCF40_RoutingServices_Protocol_Bridging’. In this solution, add a new console application and name it as ‘WCF_Service_DataAccess’. This console application will define a self-hosted WCF service. In this project add references to the following namespaces:

  • System.ServiceModel
  • System.Runtime.Serialization

Step 2: In the Program.cs file, add the following classes:

wcf-service-iservice

The above code defines DataContract, ServiceContract and the Service class.

Note: You can also write code for DataAccess from an actual database. For demo purposes, I have decided to keep it simple.

Step 3: Write the following hosting code in the Main method:

wcf-hosting-code

Step 4: In the above project, add an App.Config file with NetTcpBinding as shown below:

wcf-nettcp-binding

In the config file shown above, observe that the security mode is defined as None. If you select any specific type of security, then in the configuration of the router service and the client application, you need to specify the specific security settings.

Step 5: Run the application and test the host. The result should be as below:

clip_image002

 

Creating WCF Routing service

Step 1: In the solution created above, add a new WCF service application project. Make sure that the framework version selected as .NET 4. Name this project as ‘WCF_Routing_Service’. From this project, remove Service1.svc.cs (the code behind file) and Iservice1.cs.

Step 2: Rename Service1.svc to RoutingService.svc. Right click on it and select View Markup and write the following code in it:

<%@ ServiceHost

Service="System.ServiceModel.Routing.RoutingService, System.ServiceModel.Routing, version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>

Step 3: In the Web.Config file, write the following configuration as shown below. This file contains the following configuration information:

  • The configuration information for the actual internal WCF service. This defines the actual endpoint of the internal service.
  • The configuration for the routing filter table which defines the client endpoint name in the filtertable.

The configuration is as shown below:

wcf-service-configurationpng

Step 4: Build the application and right click on the .svc file and view in browser. Make sure the result is similar to the following:

clip_image002[8]

 

Creating the WPF Client application

Step 1: In the solution, add a new WPF client application and name it as ‘WPF_Client_Routing_Service’. In this project, add Service reference to the actual internal service. Name the reference namespace as ‘MyRef’. This is required so that the client application has knowledge of the contract exposed by the application. In the client project, the App.Config file will contain the endpoint with netTcpBinding. However we won’t be using it, so delete the endpoint from the App.Config file and add the following endpoint which contains the address of the routing service:

wcf-routing-endpoint

Step 2: Open the MainWindow1.xaml and Drag-Drop a DataGrid. The Xaml code is as shown below:

wpf-client-app-wcf

Step 3: Open MainWindow1.Xaml.cs and add the following code in the click event of the ‘Get Data’ button:

wcf-clientproxy-wpfapp

Step 4: Right on the solution and set the multiple startups object as shown below:

WCF Property Page Multiple Startup

Step 5: Run the application and click on the ‘Get Data’ button. The result will be as shown below:

clip_image002[10]

Note: Performance for the first response is slow.

Conclusion: Using the Protocol Bridging feature of WCF service, an organization can protect their proprietary services from the direct access from remote clients.

The entire source code of this article can be downloaded over here

Give me a +1 if you think it was a good article. Thanks!
Recommended Articles
Mahesh is having 10 years 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). Follow him on twitter @maheshdotnet


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Leela on Saturday, July 20, 2013 7:33 AM
Sir,
Am working router concept basic Http to NetTcp binding,Router working on Basic Http and Services working NetTcp protocal, whem i host This services ,its not working ,please suggest me.....
Comment posted by pragnesh on Wednesday, January 29, 2014 12:23 AM
great article. help me a lot.
but how can I implement client in other than ASP.net
I can't change endpoint

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel