I’ve been working with NServiceBus at work lately, and it’s useful to be able to look at the messages that are landing in the queues. Unfortunately, the MMC snap-in provided by Microsoft is woefully inadequate when it comes to this task. There are some commercial tools available, but I thought I should be able to find a free tool out there somewhere with this seemingly simple functionality.
I found Msmq Studio, a nice little app that enables you to do many different queue management tasks both locally and remotely. However, it doesn’t seem to work on my machine now (or any of the servers at work), though it did at one time. I need something reliably do one simple thing, so I decided to take the opportunity to experiment with WPF, a platform for which I’ve never written an app. The result is what I’ve tentatively titled ‘Queue View’:
This app does one thing and one thing only – it shows, in XML format, the contents of messages in private queues on the local machine (assuming, of course, the messages have been serialized and stored in XML). Here is some of the relevant code:
public class MessageQueueGateway : IMessageQueueGateway { #region IMessageQueueGateway Members public MessageQueue[] GetPrivateQueuesByMachineName(string machineName) { return MessageQueue.GetPrivateQueuesByMachine(machineName); } #endregion } public class PlainXmlMessageFormatter : IMessageFormatter { #region IMessageFormatter Members public bool CanRead(Message message) { return true; } public object Read(Message message) { XmlTextReader xmlReader = new XmlTextReader(message.BodyStream) { WhitespaceHandling = WhitespaceHandling.Significant, ProhibitDtd = true }; var xml = XDocument.Load(xmlReader); return xml.ToString(); } public void Write(Message message, object obj) { throw new NotImplementedException(); } #endregion #region ICloneable Members public object Clone() { throw new NotImplementedException(); } #endregion }
Because of the way I used it, there really was no reason to make PlainXmlMessageFormatter implement the IMessageFormatter from System.Messaging, but I figured, “Why the hell not?”
In the UI, the main form’s controller has an event handler that looks like this:
private void view_MessageSelected(object sender, MessageSelectedEventArgs e) { selectedMessage = (from message in selectedQueue.GetAllMessages() where message.Id == e.SelectedMessageIdentifier.FullIdentifier select message).SingleOrDefault(); var messageBodyFormatter = new PlainXmlMessageFormatter(); var messageBody = messageBodyFormatter.Read(selectedMessage).ToString(); view.DisplayMessageBody(messageBody); }
You can get the code here. The solution was created in VS 2010 Beta 1. Binaries are included in the ‘Build’ folder. This is essentially pre-alpha software. If you run it on a machine that doesn’t have .NET 3.5 or MSMQ 3.0 installed, it will probably explode.
Comments