Retrieving the message count for MSMQ queues

Sometimes it can be useful to retrieve the number of messages in an MSMQ queue, for example for monitoring. However, it’s not immediately apparent how to do it if you google it, so here are my thoughts on the subject.

Other blog posts suggest iterating over messages (e.g. Counting Messages in an MSMQ MessageQueue from C#) or doing it using WMI. WMI is the best alternative in my opinion and if you want a quick way of doing it then PowerShell is the easiest:

$queues = Get-WmiObject Win32_PerfFormattedData_msmq_MSMQQueue
$queues | ft -property Name,MessagesInQueue

The result will be something similar to this:

Name                                                         MessagesInQueue
----                                                         ---------------
active714\private$\notify_queue$                                           0
active714\private$\deltagarloggservice\deltagarloggservi...                0
active714\private$\order_queue$                                            0
active714\private$\admin_queue$                                            0
Computer Queues                                                           27

This can also be done on remote machines:

$host = ...
$cred = get-credential
$queues = Get-WmiObject Win32_PerfFormattedData_msmq_MSMQQueue -computer $host -credential $cred
$queues | ft -property Name,MessagesInQueue

The Get-Credential Cmdlet will display a login dialog which is fine in interactive sessions but if you need to set the credentials in a non-interactive script, then the tip in this blog post might help: PowerShell – How to create a PSCredential object.

Retrieving message counts from code takes a little more coding but here’s an example in C# that searches for a given queue and returns its message count:

private static int GetMsmqMessageCount(string queuePath, string machine,
  string username, string password)
  var options = new ConnectionOptions
    {Username = username, Password = password};
  var path = string.Format(@"\\{0}\root\CIMv2", machine);
  var scope = new ManagementScope(path, options);

  string queryString = 
    String.Format("SELECT * FROM Win32_PerfFormattedData_msmq_MSMQQueue WHERE Name = '{0}'",
  var query = new ObjectQuery(queryString);

  var searcher = new ManagementObjectSearcher(scope, query);

  IEnumerable<int> messageCountEnumerable = 
    from ManagementObject queue in searcher.Get()
    select (int) (UInt64) queue.GetPropertyValue("MessagesInQueue");

  return messageCountEnumerable.First();

This code also uses WMI but this time we do a query rather than enumerate all queues.

The above snippets has worked well for us so I felt it would be useful to post them here. Please leave a comment if you have issues with them or suggestions for improvement!


Book review: MCPD 70-519 Exam Ref: Designing and Developing Web Applications Using Microsoft® .NET Framework 4

I recently completed my MCPD certification for web development and before taking the 70-519 exam I read MCPD 70-519 Exam Ref: Designing and Developing Web Applications Using Microsoft® .NET Framework 4 by Tony Northrup. This blog post is a short review of the book.

Short version
If taking the 70-519 exam, this book is well worth a look as it discusses many development aspects related to the exam. However, it should not be used as general architecture guidance on the Microsoft .Net platform as it’s too narrowly scoped.

Longer version
The book has six chapters:

  1. Designing the Application Architecture
  2. Designing the User Experience
  3. Designing Data Strategies and Structures
  4. Designing Security Architecture and Implementation
  5. Preparing for and Investigating Application Issues
  6. Designing a Deployment Strategy

These chapters cover many design and development aspects you’ll likely come across in a real project but it should be noted that the content is pretty brief and doesn’t go into very much detail. This is in line with the scope of the 70-519 exam which assumes you have the technical know-how to develop solutions and is more geared towards architectural decisions.

Another aspect of the content of the chapters are that they are coloured by the author’s personal opinions, as so often is the case when discussing software architecture. This is probably unavoidable, but I especially noted this in the chapters discussing data access strategies and monitoring/logging. The author discusses the options available in the .Net Framework but doesn’t mention other alternatives such as common open source frameworks (e.g. NHibernate, log4net, etc). This can lead to confusion as some statements, if read literally, are inaccurate. For example, after reading page 90 you might come to the conclusion that WCF Data Services requires using the Entity Framework, which is wrong. (What is required is a class that exposes IQueryable<T> objects.)

This aspect of the book should be kept in mind when reading the book, and then it becomes useful for preparing for the exam. It also serves as a good overview of the technologies available in the .Net Framework as developers. The book is easy to read and not too long, just a little more than 250 pages.

On a personal note, the exam was actually pretty easy so if you’re an experienced developer I shouldn’t do too much studying before taking it. This book serves as an overview if you feel the need to refresh your knowledge but reading it is probably not required to pass the exam.

Final note
I read this book electronically as a PDF on my Android tablet, and if you do the same then I suggest taking some time to research the best PDF-reader app to use as the reading experience is greatly affected by this choice. My choice was ezPDF Reader Lite which allows tweaking the zoom factor and other settings. This is very useful as the aspect ratio of the tablet screen and the printed book are probably not identical.

I also tried using the ePub version but found that in that version too much formatting was lost in tables and other more advanced design elements.