Thursday, June 16, 2011

Service Listening to TCP, HTTP and Named Pipe at the same time

Introduction
The example below implements a simple service that is able to receive requests via TCP, HTTP and Named Pipe. Therefore, the service can be available for clients using different communication. E.g. Windows Phone 7 environment supports only HTTP.

The example is based on the Eneter Messaging Framework 2.0 that provides components for various communication scenarios.

(Full, not limited and for non-commercial usage free version of the framework can be downloaded from http://www.eneter.net. The online help for developers can be found at http://www.eneter.net/OnlineHelp/EneterMessagingFramework/Index.html.)

Multiple Listening 
To implement the multiple listener, we will use the Dispatcher component from the Eneter Messaging Framework.
The Dispatcher receives messages from all attached input channels and forwards them to all attached output channels.
In our scenario, the dispatcher will have three input channels (TCP, HTTP and Named Pipe) and only one output channel (local channel to the message receiver).
So, if a message is received e.g. via the TCP input channel, the dispatcher will forward it through the output channel to the typed message receiver. The typed message receiver then notifies the user code implementing the service.

MultilisteningService.gif


Multiple Listening Service Application
The service is implemented as a simple console application. The most important part is using of the dispatcher component for the multiple listening (TCP, HTTP and Named Pipe).
Since the service listens also to HTTP, you must execute it under sufficient user rights. (I recommend administrator for the debug purposes.)
The whole implementation is very simple.

using System;
using Eneter.Messaging.EndPoints.TypedMessages;
using Eneter.Messaging.MessagingSystems.HttpMessagingSystem;
using Eneter.Messaging.MessagingSystems.MessagingSystemBase;
using Eneter.Messaging.MessagingSystems.NamedPipeMessagingSystem;
using Eneter.Messaging.MessagingSystems.SynchronousMessagingSystem;
using Eneter.Messaging.MessagingSystems.TcpMessagingSystem;
using Eneter.Messaging.Nodes.Dispatcher;
namespace MultiReceivingService
{
    public class RequestData
    {
        public int Number1 { get; set; }
        public int Number2 { get; set; }
    }
    class Program
    {
        // Receiver receiving 'RequestData' and responding 'int'.
        // Note: Duplex typed message receiver can receive messages of specified type
        //       and send response messages of specified type.
        private static IDuplexTypedMessageReceiver<int, RequestData> myReceiver;
        static void Main(string[] args)
        {
            // Create local messaging connecting the dispatcher with the receiver.
            IMessagingSystemFactory aLocalMessaging = 
new SynchronousMessagingSystemFactory();
            IDuplexInputChannel aLocalInputChannel =
                aLocalMessaging.CreateDuplexInputChannel("MyLocalAddress");
            IDuplexTypedMessagesFactory aTypedMessagesFactory = 
new DuplexTypedMessagesFactory();
            myReceiver = aTypedMessagesFactory.CreateDuplexTypedMessageReceiver
<int, RequestData>();
            myReceiver.MessageReceived += OnMessageReceived;


Read more: Codeproject