Historically, MSMQ is the first transport supported by NServiceBus. In version 5 it still is by far the most commonly used one. Because of these and also the fact that MSMQ client libraries are included in .NET Base Class Library (
System.Messaging assembly), MSMQ transport is built into the core of NServiceBus.
Because of the way MSMQ API has been designed i.e. polling receive that throws an exception when timeout is reached the receive algorithm is more complex than for other polling-driven transports (such as SQLServer).
The main loop starts by subscribing to
PeekCompleted event and calling the
BeginPeek method. When a message arrives the event is raised by the MSMQ client API. The handler for this event starts a new receiving task and waits till this new task has completed its
Receive call. After that is calls
BeginPeek again to wait for more messages.
Because of historic reasons, the configuration for MSMQ transport has been coupled to general bus configuration in the previous versions of NServiceBus.
By default, queues are created with
Anonymous Logon permissions to allow messages to be sent and received without additional configuration. Whilst initially convenient, this unrestricted access may be a security concern. If your organization requires it, you should set appropriate permissions on a queue after its creation.
For more on changing MSMQ permissions, check out the Administer Security for Message Queuing section on Microsoft TechNet.
Following settings are purely related to the MSMQ:
From version 4 onwards these settings are configured via a transport connection string (named
nservicebus/transport for all transports). Before version 4 some of these properties could be set via
MsmqMessageQueueConfig configuration section while other (namely the
connectionCache and the ability to use non-transactional queues) were not available prior to version 4.
<configuration> <configSections> <section name="MsmqMessageQueueConfig" type="NServiceBus.Config.MsmqMessageQueueConfig, NServiceBus.Core" /> </configSections> <MsmqMessageQueueConfig UseDeadLetterQueue="true" UseJournalQueue="false" /> </configuration>
<connectionStrings> <add name="NServiceBus/Transport" connectionString="cacheSendConnection=true;journal=false;deadLetter=true;useTransactionalQueues=true"/> </connectionStrings>
Often when debugging MSMQ using native tools it is helpful to have some custom text in the MSMQ Label. For example the message type or the message id. As of version 6 the text used to apply to Message.Label can be controlled at configuration time using the
ApplyLabelToMessages extension method. This method takes a delegate which will be passed the header collection and should return a string to use for the label. It will be called for all standard messages as well as Audits, Errors and all control messages. The only exception to this rule is received messages with corrupted headers. In some cases it may be useful to use the
Headers.ControlMessageHeader key to determine if a message is a control message. These messages will be forwarded to the error queue with no label applied. The returned string can be
String.Empty for no label and must be at most 240 characters.
BusConfiguration busConfiguration = new BusConfiguration(); busConfiguration.UseTransport<MsmqTransport>() //Set the msmq message label to the current Message Id .ApplyLabelToMessages(headers => headers[NServiceBus.Headers.MessageId]);
Last modified 2015-11-25 00:19:32Z