How to know programmatically whether Message Queueing is enabled on the machine or not?

非 Y 不嫁゛ 提交于 2019-12-21 08:00:13

问题


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.


回答1:


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? }



回答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




回答3:


How to tell if MSMQ is installed




回答4:


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)



来源:https://stackoverflow.com/questions/6069052/how-to-know-programmatically-whether-message-queueing-is-enabled-on-the-machine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!