11

I know that when I try to create new MessageQueue, system throws InvalidOperationException if the Message Queuing is not enabled.

But how to know programmatically whether Message Queueing is enabled on the machine or not? I am using C# 2.0 & C# 4.0 in two different code bases.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Learner
  • 4,661
  • 9
  • 56
  • 102
  • You indeed answered your question, just try this try { //create or open a MessageQueue } catch (InvalidOperationException ) { // is not enabled } – Delta76 May 20 '11 at 08:07
  • 1
    Using exceptions to detect state/information is a bad practice unless absolutely necessary. – Travis May 21 '11 at 17:47

4 Answers4

21

You can use the System.ServiceProcess for this one, but first you need to add reference to your project the Service.ServiceProcess, and you can retrieve all the services and get their status like this:

List<ServiceController> services = ServiceController.GetServices().ToList();
ServiceController msQue = services.Find(o => o.ServiceName == "MSMQ");
if (msQue != null) {
    if (msQue.Status == ServiceControllerStatus.Running) { 
        // It is running.
    }
} else { // Not installed? }
Learner
  • 4,661
  • 9
  • 56
  • 102
Peyton Crow
  • 872
  • 4
  • 9
  • Excellent ! Accepting as the answer. Modified the exact name of the MSMQ service that OS runs when MSMQ is installed. The `ServiceName` is `MSMQ` on Windows 7 & Windows XP at least. – Learner May 20 '11 at 09:38
2

Answering little late, but if you are scripting fan Powershell is at your help. To get status update on numbers, use following script:

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

This will show you name of queue and number of items in each queue. Hope this will help someone someday. :D

Sanjay Zalke
  • 1,331
  • 1
  • 18
  • 25
  • Thanks for the answer.. But question was clearly for C# if you see the tags .. :) – Learner Nov 26 '13 at 04:57
  • Oops! did not pay attention, but now a days Powershell is so closely knitted in .net framework that it can be invoke natively. #Justsaying – Sanjay Zalke Nov 26 '13 at 15:13
1

How to tell if MSMQ is installed

John Breakwell
  • 4,667
  • 20
  • 25
-2

You have answered your own question there: try to create a new MessageQueue, and catch InvalidOperationException.

If you don't get an exception, MQ is enabled; if you get an exception, it's not enabled. (you may dispose of that MessageQueue instance if one was created, as you've only used it for detecting support)

Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222