Tags

Introduction

Routing Service is an intermediary between the client and the back-end services. It has the ability to receive messages from clients and route them to the appropriate service, based on the message content. The client needs not to have the multiple endpoints, whereas the routing service has the aggregation of various service endpoints, and the messages will be routed based on the message content. If you are not familiar with the Routing basics, you can refer to my last article “Basics of WCF Routing”. This topic will help you understand the Routing Service configuration. Moreover, it also tells how filters are applied to route the back-end service, based on the message content.

Architecture

To configure the Routing, one has to configure the following components –

  • Routing Service
  • Contracts
  • Service Behaviour
  • Filters
  • Endpoint

Before you know how to configure these components, let’s first understand what exactly the purpose is of configuring each component.

Routing Service

Routing service is implemented as a WCF service in the System.ServiceModel.Routing assembly. It’s already available in the WCF to route messages over any protocol, using a different message exchanging patterns like one way, two way, Request-Response and duplex messaging. We only need to define the name, address, binding, and contract according to our requirements.

Contracts

Service contract of Routing service supports different Message Exchange Patterns (MEPs), like request-reply, duplex session, and simplex session channels. These Interfaces are required to process the messages from channels.

Contract Description
IDuplexSessionRouter Defines the interface required to process messages from duplex session channels
IRequestReplyRouter Defines the interface required to process messages from request-reply channels.
ISimplexDatagramRouter Defines the interface required for processing messages from simplex datagram.
ISimplexSessionRouter Defines the interface required to process messages from simplex session channels

Filter

The Job of the Message Filter is to inspect specific sections of a message, such as – address, endpoint. To send the message to the desired endpoint, the incoming message should match the filters applied.

FilterTable

Once the filters are matched with filter data in the earlier step, here we are associating the filter with the endpoint name.

Endpoint

Endpoints are used for associating the address and binding with the endpoint name. The Routing service refers the address with the associated endpoint and routes the messages accordingly.

Routing Configuration

So far, we have seen what are all the components required and why these are required to configure Routing . Now, I am going to explain to you in detail, the steps involved in configuring the simple routing.

Routing Configuration

In the above diagram, you can find that Client always connects to the Front-end service. Then, front-end service will inspect the message content and route the messages to the Food Inventory service or Materials inventory service, based on the message content it receives from the client. To accomplish this configuration, let me explain to you in detail-

Step 1 – Let’s start with configuring back-end services first. I would recommend you guys to configure and host the back-end services in IIS. As soon as the IIS gets started, these services will also get started automatically, unlike the self-hosting where we have to start it manually. First, create the Food Inventory Service Library and do the IIS hosting. If you need assistance in creating IIS hosting, please refer my old article (http://www.c-sharpcorner.com/article/iis-hosting-in-wcf/). We don’t have any complex configuration in backend service.  In the below snapshot, you can see the snapshot of my Food Inventory configuration for your reference

Routing Configuration

Hope you have done the IIS hosting for Food Inventory. Once you are done with it, we will go for creating a Materials Inventory Service.

Step 2 – In step 2, you have to repeat the same steps (what you have done for Food Inventory) to the Material Inventory. Create the Material Inventory Service and host it in the IIS. In the below snapshot, you can see the Materials Inventory Service configuration, for your reference. So far, you are not able to find a single configuration in Routing. All the routing configuration will happen only on the next step i.e on Front-end service or Main Service.

Routing Configuration

Step 3 – Step 3 is the most important configuration in the Routing. As I mentioned in the architecture section here, we need to configure the following in the Front-end service

  • Contract
  • Service Contract
  • Filters
  • Endpoint
  • Routing Service

As a first step, create WCF Service Library project (New Project ->WCF->WCF Service Library) and open the App.config file. As mentioned in the below snapshot, update the service name as “System.ServiceModel.Routing.RoutingService” and update the ServiceBehaviourConfiguration. In our example, I have named the service behavior as “Routing Service Behaviour”.

Routing Configuration

Then, as usual, give the address for your service. In our case, since this service is a front-end service for the client, though we have a couple of back-end services (Food-Inventory & Materials Inventory), the URL to access the services remains same for the client. And, that URL is none other than Front End Service URL (http://localhost:8555/MainRoutingService/Router). After that, did you find the contract name in the endpoint element? As I already mentioned, Routing Service is already available in WCF and the name of the contract we decided for our case is “System.ServiceModel.Routing.IRequestReplyRouter”. As all my services, MEP (Message Exchange Pattern) is Request Reply Pattern. I thought of selecting this option as a contract with my front-end service.

Step 4 – This step is all about defining the Filters and Filter Tables. The first thing you need to do here declares the name of a routing filter table in the Service Behavior.

Routing Configuration
In the above snapshot, you can find that name of a filter table is a ServiceRouterInfoTable. After that, you have to write the routing logic in the form of Filters and Filter Table. A Filter has 3 elements- name, filter type, and filter data. You have to define these 3 as mentioned in the below snapshot

Routing Configuration

The FilterData will be used to inspect the specific section of an incoming message. Once the message is matched, the associated endpoint entry name will be taken to forward the message. In the above snapshot, filterData, “GetFoodInventory” gets matched with the incoming message. So, the associated endpoint name “ept_FoodInventory” will be taken as mentioned in the the below snapshot

Routing Configuration
As the last step, you need to define the endpoint names ept_FoodInventory and ept_MaterialsInventory in the endpoint element. Once the filter is matched with the filter data and the incoming message, endpoint name will be referred to the filter name. Then, the address and binding information against the endpoint name will be taken to route the messages accordingly. The following snapshot shows how to define the endpoint in the Front- end service

Routing Configuration

Finally, we need to host the Routing service or Front-end Service. Place the below code and start the project. The Routing Service will be launched to receive the requests from the client. Route it to back-end services based on the message content.
Routing Configuration

Guys, we are done on the service side. So far, we have configured back-end services and hosted them in IIS. Now, that we created a front end service and created Service name, Contract and Service behaviour. Then  we do Filters, FilterTable, endpoint definition and hosting the Router Service

Step 5 – As a Final step, let me tell you what you need to do on the client side. The simpler option would be adding the service reference by right clicking the project -> Add Service Reference. Provide the Routing Service URL and click Go (Refer the below snapshot). Before that, make sure, your back-end service is up and running in IIS. The same is applicable to Materials Inventory Service as well. A couple of times, you have to create Service reference for both the services.

Routing Configuration

As soon as you add the service reference, App.config in the client project will be populated automatically with the endpoint, binding and contracts. As the client needs to connect only Routing service, just update the Routing URL http://localhost:8555/MainRoutingService/Router over both the endpoints in the client. Now, run the project and make a service call, it will automatically route to the desired service.

Please refer to the attached source code for the details. The attachment includes back-end services FoodInventory and MaterialInventory, front-end services RoutingService and the client application. Hope you enjoyed learning! If anything strikes you, please feel free to update your comments over this article.

Source-Code

Please use this link to download the source-code

Summary

Routing Service in the System.ServiceModel.Routing Namespace. Front end service will get all the messages from the client and route it to the appropriate service based on the message content.

Advertisements