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)
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:
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:
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:
Step 4: In the above project, add an App.Config file with NetTcpBinding as shown below:
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:
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:
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:
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:
Step 2: Open the MainWindow1.xaml and Drag-Drop a DataGrid. The Xaml code is as shown below:
Step 3: Open MainWindow1.Xaml.cs and add the following code in the click event of the ‘Get Data’ button:
Step 4: Right on the solution and set the multiple startups object as shown below:
Step 5: Run the application and click on the ‘Get Data’ button. The result will be as shown below:
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
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