Archived page · captured 2012-02-08

Conquering NServiceBus part 2 – Initial configuration

Initial configuration for NServiceBus receiving and sending endpoints with MSMQ transport and message handlers.

Receiving Endpoint Configuration

The receiving endpoint requires MSMQ queues and configuration. A simple config uses MsmqTransportConfig with InputQueue set to 'ReceivingEndpointQueue' and ErrorQueue for failed messages. The UnicastBusConfig contains MessageEndpointMappings for routing messages to this endpoint.

MsmqTransportConfigInputQueue="ReceivingEndpointQueue"ErrorQueue="Error"NumberOfWorkerThreads="1"MaxRetries="5"
UnicastBusConfigMessageEndpointMappings<!-- This is the receiving endpoint -->

Sending Endpoint Configuration

The sending endpoint configures message routing by mapping assemblies or specific message types to target endpoints. Messages from 'MyProject.Messages' assembly are sent to 'ReceivingEndpointQueue'. Specific messages like timeout messages can be mapped to dedicated endpoints.

MessageEndpointMappings<add Messages="MyProject.Messages" Endpoint="ReceivingEndpointQueue" />
MessageEndpointMappings<add Messages="NServiceBus.Saga.TimeoutMessage, NServiceBus" Endpoint="TimeoutManager" />

Endpoint Initialization

Receiving endpoints implement IConfigureThisEndpoint and AsA_Server interfaces. This configures MSMQ transport with transactions, XML serialization, automatic message handler loading, and prevents queue purging at startup. The default IoC container is Spring, though StructureMap can be used instead.

Message Handler Implementation

Message handlers implement IMessageHandler<T> where T implements IMessage. When a message arrives at the configured queue, the Handle method is invoked with the message object containing the data to process.

Sending Messages from Web Application

Web applications configure a static IBus instance in Global.asax using Configure.WithWeb() with Log4Net logging, XML serialization, and MSMQ transport. The bus is started during application startup and messages are sent using Bus.Send() with message objects.