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.
| MsmqTransportConfig | InputQueue="ReceivingEndpointQueue" | ErrorQueue="Error" | NumberOfWorkerThreads="1" | MaxRetries="5" |
|---|---|---|---|---|
| UnicastBusConfig | MessageEndpointMappings | <!-- 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.
- public class MyMessageHandler : IMessageHandler<OneMessage>
- public void Handle(OneMessage message) { /* process message */ }
- public class OneMessage : IMessage { public string Title { get; set; } }
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.
- public static IBus Bus { get; private set; }
- Bus = Configure.WithWeb().Log4Net().SpringBuilder().XmlSerializer().MsmqTransport().IsTransactional(true).UnicastBus().LoadMessageHandlers().CreateBus().Start();
- Global.Bus.Send(new OneMessage { Title = "This is a title" });