Click here to Skip to main content
13,402,312 members (55,340 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


49 bookmarked
Posted 9 Feb 2011

Service Bus Architecture based on WCF (3): Build your ESB

, 10 Feb 2011
Rate this:
Please Sign up or sign in to vote.
Service Bus Architecture based on WCF (3): build your ESB


It was almost one year ago that I finished the last chapter. One year is kind of too long to remember everything, I have to spend a couple of hours to review what I have done one year ago. The good thing is I still remember how to bring up each application and connect them together, the better thing is they are still working great. The best thing is I can go on writing the article.

In the last chapter, we have successfully abstracted WCF service out of business logic. Based on what we have done, I’m going to create our service bus. Let's go back to the FuctionService and MessageService. FunctionService is responsible for providing service to client call. MessageService is responsible for subscripting message to clients.

For example, there are HubBeijing Service managing Beijing product inventory and HubChina Service managing China product inventory. They open FunctionService and MessageService respectively. And they manage their own inventory respectively. I put a simple rule that each Hub will send out Out of Stock message if client requests a product number that is more than product inventory. Let’s assume these two systems are independent. Beijing is a part of China, when Beijing hub is out of stock, China Hub will provide the number requested from client. Since Beijing hub and China hub are independent, they will not communicate directly, we need a third party to connect them together. This third party is what we said Service Bus. We have abstracted service out of business logic, now we are going to abstract the Service Bus out of business logic to be an independent component.


Please check How to abstract a WCF Service out of business logic and How to implement subscription based on WCF to get more context.

Using the Code

ESB component is actually a client connecting different services and accept different subscriptions. ESB component has the ability to connect many service providers. It is able to call different service providers’ methods, and it is also able to listen to different service providers’ subscriptions.

BusEngine.RegisterNewService("Beijing Hub", "net.tcp://localhost:8886/Inventory");
BusEngine.RegisterNewService("China Hub", "net.tcp://localhost:8887/Inventory");
//In RegisterNewService, there is a reference to SAOTEnpoint. 
//The method will open RemoteCall service as well as Subscription Service
//Disconnected event will happen when any registered services connection is broken, 
//in the event, arg sender will tell you which service is broken.
BusEngine.Disconnected += new EventHandler(BusEngine_Disconnected);
//HeartBeatMessage will tell you if one service connection is still connected.
BusEngine.HeartBeatMessage += new EventHandler(BusEngine_HeartBeatMessage);
//MessageDelivered Event will get all messages from all registered services. 
//In the event, argument Trilobita.SAOT.Object.Message e will let you know 
//the detail information of the message.
//Trilobita.SAOT.Object.Message.ServiceName indicates the message is from which 
//registered service.
//Trilobita.SAOT.Object.Message. FunctionCode indicates customer defined flag like: 
//OUTOFSTOCK means the message is to notify one particular product is out of stock.
BusEngine.MessageDelivered += new EventHandler<trilobita.saot.object.message>

//In order to provide an abstracted service bus, The message structure is predefined. 
//Users can use the predefined structure to define the customized data. 
//Content and Context is prepared for complex customized data. See the below diagram:

public class Message: EventArgs
        #region Properties
        public string Title { get; set; }
        public string Content { get; set; }
        public string Context { get; set; }
        public DateTime Createtime { get; set; }
        public string PrimaryID { get; set; }
        public string Sender { get; set; }
        public string To { get; set; }
        public string CC { get; set; }
        public DateTime ExpiredTime { get; set; }
        public string Attachment { get; set; }
        public string FunctionCode { get; set; }
        public string ServiceName { get; set; }


We still use the Hub example. ESB component connects HubBeijing service as well as HubChina service. When Beijing client requests a product more than HubBeijing inventory, HubBeijing will subscribe a message to all registers. ESB component will get the message as well and bring up MessageEvent. Now, we need HubChina to provide the requested product. Therefore, we have an external application to capture the MessageEvent from ESB component, and write relevant logic in to invoke HubChina service to provide the product. See the below diagram:

Step 0: Start Service [project: SAOTEngine]

//service engine has been abstracted, it is not able to recognize unknown type object. 
//AddKnowType method will tell service engine to be able to serialize the 
//given type object 
//open, close event to notify external application if the service is successfully opened.
            ServiceController.ServiceOpenedEvent += 
		new EventHandler(ServiceController_ServiceOpenedEvent);
            ServiceController.ServiceFaultEvent += 
		new EventHandler(ServiceController_ServiceFaultEvent);

//open service for client invoking
ServiceController.OpenFunctionService<serviceadapter />(int.Parse(this.txtPort.Text), this.txtName.Text);
//open service for subscription
ServiceController.OpenMessageService(int.Parse(this.txtPort.Text), this.txtName.Text);

Step 0: Client connect service [Project:SAOTEndPoint]

//client also need know the object type to deserialize the object 

//connect service, including subscription service 
this.MyService = new SAOTServiceStructre<istandardservice>

//get subscription service client
MessageDistributor messager = this.MyService.MessageService;
//HeartBeat message indicate the connection is still ok
messager.HeartBeatFromServer += new EventHandler(Instance_HeartBeatFromServer);
//Disconnected message indicate the connection is broken
messager.ServerDisconnectedEvent += new EventHandler(messager_ServerDisconnectedEvent);
//Normal Message, the structure is predefined, please refer to the previous section
messager.MessageDelivered += new EventHandler<trilobita.saot.object.message>

Step 0: Service Bus connect Beijing hub as well as China hub [Project: ServiceBusMonitor, SAOTServiceBus]

//client also need know the object type to deserialize the object 

//add beijing service
BusEngine.RegisterNewService("Beijing Hub", "net.tcp://localhost:8886/Inventory");
//add china service
BusEngine.RegisterNewService("China Hub", "net.tcp://localhost:8887/Inventory");
BusEngine.Disconnected += new EventHandler(BusEngine_Disconnected);
BusEngine.HeartBeatMessage += new EventHandler(BusEngine_HeartBeatMessage);
BusEngine.MessageDelivered += new EventHandler

Step 1: Beijing Client Request New Product [Project: InventoryClient]

//since the service is abstracted, we use reflect to invoke remote method
this.GetService().Inovke("ChangeInventory", this.txtUser.Text, inventory.ProductID, 
//T  is the unknown type. we use AddKnowType to let service know T
object obj = typeof(T).InvokeMember(methodname, BindingFlags.Static |
BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.InvokeMethod, 
		null, null, args);
return obj;

Step 2: Beijing Hub Service subscribe the inventory is not enough [Project: InventoryServer]

public bool ChangeInventory(string user, int number)
            int restnumber = this.CurrentInventory + number;
            if (restnumber > 0)
                this.CurrentInventory += number;
                if (InventoryChangedEvent != null) 
                    InventoryChangedEvent(null, new InventoryArgs 
		{ Inventory = this, ChangedNumber = number, Operator=user });
                return true;
            //when the inventory is out of stock, trigger OutofStockEvent
                if (OutofStockEvent != null)
                    OutofStockEvent(null, new InventoryArgs 
		{ Inventory = this, ChangedNumber = number, Operator = user });
                return false;
Inventory.InventoryChangedEvent += 
	new EventHandler<inventoryargs>(Inventory_InventoryChangedEvent);
static void Inventory_OutofStockEvent(object sender, InventoryArgs e)
            Trilobita.SAOT.Object.Message message = new Trilobita.SAOT.Object.Message();
            message.Sender = e.Operator;
            message.PrimaryID = e.Inventory.ProductID.ToString();
            message.Title = e.ChangedNumber.ToString();

            Trilobita.SAOT.Service.MessageBox box = 
		new Trilobita.SAOT.Service.MessageBox("OutofStock");
            box.MessageEnable = true;
            box.EmailEnable = false;
            box.DatabaseEnable = false;
            //subscribe that inventory is out of stock

Step 3: Service Bus request inventory from China hub [Project: ServiceBusMonitor]

//get china hub service and request for change inventory

Step 4: ... I didn't really implement it. By default, china hub will fulfill the request.


How to Demonstrate the Application

  1. Double click SAOT.exe, confirm the ServicePort textbox value is 8886, Service Name is Inventory. Click Confirm to open the service, a message will display on the form if the service is successfully opened. SAOT Service is for remote call, MessageSender is for subscription. This service simulates Beijing Hub.
  2. Repeat step 1, only change the ServicePort value to 8887. This service simulates China Hub.
  3. Double click ServiceBusMonitor.exe, make sure the list services are connected. This is ESB application.
  4. Double click ClientTest.exe and click the confirm button if you exactly followed the upper steps. The light on top-left corner will flash if the connection is good.
  5. In ClientTest form, input one product name and default inventory, like product name: computer, inventory:50. Switch to first SAOT.exe (Beijing Hub), you will see computer with inventory 50. Switch to the second SAOT.exe (China Hub), you will see computer with inventory 500, because I put one logic in ESB application, if there is a new product from Beijing Hub, China Hub will prepare 10 times inventory. Here is the point, the first SAOT.exe (Beijing Hub) doesn’t connect the second SAOT.exe (China Hub), and ClientTest.exe only connects the first SAOT.exe (Beijing Hub). New product inventory is started from ClientTest. Why the second SAOT.exe (China hub) can add 10 times inventory is because ServiceBusMonitor detected the new product inventory message from Beijing hub and added the China hub inventory automatically.
  6. In ClientTest form, change computer inventory from 50 to 40 (to put the demonstration simple, I didn’t pup up a form to request the number of product). It equals to we request 10 computers. Switch to Beijing hub, you will see the product inventory changed to 40.
  7. In ClientTest form, change computer inventory from 40 to -10 (sorry, I just want to take 50 computers). Switch to Beijing hub, the number didn’t change. Switch to China hub, the number is 450. The logic here is, when client requests 50 computer, Beijing hub will send out OutOfStock message, ServicebusMonitor gets the message and transfers 50 computers to Beijing hub to keep Beijing hub inventory on a safe level.

Picture_2.png - Click to enlarge image


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

China China
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Haipro7-Aug-13 23:18
memberHaipro7-Aug-13 23:18 
GeneralRe: My vote of 5 Pin
WillCaptain16-Aug-13 4:55
memberWillCaptain16-Aug-13 4:55 
GeneralRe: My vote of 5 Pin
Joh Chen16-Jun-14 18:53
memberJoh Chen16-Jun-14 18:53 
GeneralMy vote of 5 Pin
manoj kumar choubey16-Apr-12 4:20
membermanoj kumar choubey16-Apr-12 4:20 
QuestionRockBus Pin
bram0528-Jun-11 21:56
memberbram0528-Jun-11 21:56 
AnswerRe: RockBus Pin
WillCaptain29-Jun-11 0:04
memberWillCaptain29-Jun-11 0:04 
GeneralMy vote of 5 Pin
IvanKhan27-Feb-11 10:03
memberIvanKhan27-Feb-11 10:03 
GeneralRe: My vote of 5 Pin
Haipro22-Jun-14 16:27
memberHaipro22-Jun-14 16:27 
GeneralLooks really interesting, but you need to explain the code more Pin
Sacha Barber9-Feb-11 22:36
mvpSacha Barber9-Feb-11 22:36 
GeneralRe: Looks really interesting, but you need to explain the code more Pin
WillCaptain9-Feb-11 22:42
memberWillCaptain9-Feb-11 22:42 
GeneralRe: Looks really interesting, but you need to explain the code more Pin
WillCaptain11-Feb-11 20:21
memberWillCaptain11-Feb-11 20:21 
GeneralRe: Looks really interesting, but you need to explain the code more Pin
Sacha Barber12-Feb-11 1:23
mvpSacha Barber12-Feb-11 1:23 
GeneralRe: Looks really interesting, but you need to explain the code more Pin
WillCaptain13-Feb-11 22:46
memberWillCaptain13-Feb-11 22:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180218.2 | Last Updated 11 Feb 2011
Article Copyright 2011 by WillCaptain
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid