NServiceBus is a product that solves problems similar to BizTalk. BizTalk is a message broker. It is a central place to receive all messages which are distributed to backend systems for further processing. NServiceBus implements the bus architecture where backend systems have the infrastructure to process messages. NServiceBus is developed by Particular Software. There is a good comparision between BizTalk and NServiceBus in the documentation.

NServiceBus allows two types of message processing: Commands and Events. Commands are used to implement endpoint processing. Command messages are sent to endpoints which are processed by NServiceBus applications. Events are used to implement publish-subscribe mechanism.

The objective of the post is to show a simple NServiceBus demo application which implements command processing at endpoints. The example uses NServiceBus 4. NServiceBus uses MSMQ for receiving messages and RavenDB for scalability and transactions.

Overview of the Sample application

The Sample application has five projects:

  1. Console application which sends messages to an endpoint named queue1.
  2. Message Processor class library which processes the message in queue1 and moves it to queue2.
  3. Message Processor class library which processes the message in queue2 and moves it to queue3.
  4. Message Receiver class library which processes the message in queue3 and displays the message contents in the console.
  5. Class library which has the definition of messages used by all NServiceBus applications including the Console application.

Command Message Definition

In NServiceBus, messages are of two types: Command and Event. NServiceBus defines marker interfaces in NServiceBus.Interfaces class library. IMessage is a marker interface representing a NServiceBus message. A command message and an event message are two different logical constructs. To differentiate between a command message and an event message, two additional marker interfaces: ICommand and IEvent are defined. ICommand is used to implement command processing at endpoints. IEvent is used to implement publish-subscribe mechanism. The code below shows the Message class used in the sample application which is marked by the ICommand interface:

The DummyMessage is found in the NServiceBusDemo.Messagesproject. The project references the NServiceBus Interfaces package from NuGet.

Sending messages to an endpoint

To kickstart command processing at an endpoint, command messages need to be sent to the endpoint. Endpoint refers to a MSMQ queue. For this example, create three private queues in MSMQ: queue1, queue2, queue3. Make all queues transactional and ensure that security is appropriately set to read messages from the queue and send messages to the queue.

NServiceBus allows console applications and web applications to send messages to the endpoint using a sendOnly bus. The sendOnly bus does not require complicated configuration. To send a message to queue1, the following code can be used:

NServiceBus uses the IBus interface to send messages to an endpoint. IBus is configured with MSMQ Transport, Unicast Bus and XML serialization.

NServiceBus uses an IoC container to build the dependent objects used by the Bus. The standard IoC container in NServiceBus 4 is Autofac.

The console application uses a sendOnly bus. SendOnly buses are not endpoints which receives messages for processing.

The console application can be found in the NServiceBusDemo console application. The console application references the NServiceBus Interfaces and NServiceBus packages from NuGet.

Message processing at an Endpoint

Endpoint configuration

For processing messages at an Endpoint, the endpoint needs to be configured. The endpoint needs to be configured as a Client, Server, or Publisher. Client and Server endpoints are used for processing command messages. Publisher endpoints are used to implement Publish-Subscribe mechanism.

For the sample application, we have defined three Endpoints: Message Processor 1, Message Processor 2, Message Receiver. Message Processor 1 and Message Processor 2 listens to incoming messages in a queue. When a message is received, it processes the message. The processed message is then sent to the next queue to be picked by the next Message processor. All the endpoints in the sample application are configured as servers using the below code:

Points to Note while configuring Endpoint:

  1. Endpoint name can be specified by the EndpointName attribute on the class implementing IConfigureThisEndpoint. The Endpoint name is queue1. This means that the Endpoint is receiving messages from the MSMQ queue – queue1.
  2. IConfigureThisEndpoint is a marker interface which indicates NServiceBus to configure the Endpoint. Endpoint can be configured using three inbuilt configurations: Client, Server, Publisher. In the example code above, the endpoint is configured AsA_Server.
  3. Apart from built-in configurations, NServiceBus allows custom configuration. To implement custom configuration, the EndpointConfig class should implement IWantCustomInitialization. The Init method of the interface is implemented.
  4. In the custom initialization, two additional configurations are performed – XML Serialization and Disabling the Timeout Manager.
  5. Disabling the Timeout manager will ensure that RavenDB need not be configured and the sample application will work without any further configurations.

Handler for processing Messages

Handler classes implement IHandleMessages<T> interface. Handler classes are the ones which do the actual processing of messages. The actual processing of messages is done by the Handle method.

Handler classes are dependent on the Bus. Bus objects are injected into Handler objects via dependency injection. In the above Handler, the message is marked as processed and is sent to the next queue.

NServiceBus Host

NServiceBus applications are class libraries which require three packages from NuGet:

  1. NServiceBus Interfaces.
  2. NServiceBus.
  3. NServiceBus Host.

NServiceBus Host package does the following:

  1. Provides an app.config to allow you to configure NServiceBus endpoints via XML.
  2. A default Endpoint configuration class using AsA_Server built-in configuration.
  3. Modify Project properties so that the class library project can be debugged using NServiceBusHost.exe.

NServiceBus applications are class libraries which can be run as a console application using NServiceBusHost.exe or Windows service applications by running additional installation scripts. For the sample application, we  run each of the NServiceBus applications as console applications hosted by NServiceBusHost.exe.

Running the Sample application

The sample application can be run using the following steps:

  1. Create three private queue – queue1, queue2, queue3 in MSMQ.
  2. Open Visual Studio in administrator mode (Otherwise, you will get AutoFac dependency resolution error).
  3. Run the NServiceBusDemo console application.
  4. Run the MessageProcessor1 library.
  5. Run the MessageProcessor2 library.
  6. Run the MessageReceiver library.
  7. Run in the above steps = Debug => Start New Instance by right clicking the project from the Solution Explorer.
  8. Running the sample application will create a message in queue1 which moves to queue2 and then to queue3.  After the message receiver processes the message from queue3, the message will be removed.

Download the sample application: NServiceBusDemo

Introduction to NServiceBus message processing at MSMQ endpoints
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *