3
votes

I need to get the Id of a msmq message inside of my handler so I can write that Id to a log.

When a message is sent to the error queue an email is sent informing us of a failed message. Once the error that caused the message is resolved we need to use the 'ReturnToSourceQueue' NServiceBus tool to try that message again. Without logging that Id, it would be difficult to track down which message is which when looking through the message queues.

Every where I've looked suggests that Bus.CurrentMessageContext.Id will give me the same Id that's in the Message ID column when looking at the queues in ComputerManagement->Services and Applications->Message Queuing->[Some Queue]->Queue messages. However, those ids don't seem to be the same.

What am I missing?

2

2 Answers

3
votes

The reason that the message ID that you see in MMC plugin or Queue Explorer is different is that when the message is "moved" to the error queue, what actually happens is that a new MSMQ message is created with the same body and headers and that is sent to the error queue.

Also, when the processing a message fails, NServiceBus already logs this for you and includes the ID of the message, so that's already done for you.

If you take the ID that was logged and pass that to the ReturnToSourceQueue tool, everything will just work.

The last piece of the puzzle for you is sending an email when a message fails. Now, I'm not sure that that is the wisest idea as you may end up spamming your ops team when a database goes offline or a 3rd party webservice becomes unresponsive. Still, if that's what you want to do, then I'd suggest using an email appender for when errors are logged.

Finally, let me say that we're in the process of building this kind of notification functionality into the Particular Service Platform around NServiceBus. We've got a UI showing errors and allowing messages to be reprocessed coming as a beta in November '13 and the notification functionality will probably be ready towards the end of the year.

It's really a question of whether you want to wait or to build this yourself.

0
votes

Just create an instance of your bus in your handler:

public IBus Bus { get; set; }

Then use that to get the message id:

this.Bus.CurrentMessageContext.Id

The Bus instance will be injected when the handler is called.

EDIT

Now that I have actually read the question...

The CurrentMessageContext.Id returns what's in the message header under the CorrId field. This can be seen in the Label column in Server Management.

The Message ID displayed in the MessagId column is the message ID as it existed on the sending computer. I am not sure how to access this value from CurrentMessageContext but you should not need to do this to find a local message.