简体   繁体   English

在C#中查询MSMQ系统队列

[英]Query MSMQ System Queues in C#

I use the very handy GetPrivateQueuesByMachine and GetPublicQueuesByMachine methods of the System.Messaging namespace. 我使用System.Messaging命名空间的非常方便的GetPrivateQueuesByMachine和GetPublicQueuesByMachine方法。 There is no equivalent GetSystemQueuesByMachine that I can find, so I've written my own: 我找不到等效的GetSystemQueuesByMachine,所以我写了自己的:

private MessageQueue[] GetSystemQueuesByMachine(string hostName)
{
    MessageQueue[] queueList = new MessageQueue[3];

    // System Journal
    string queuePath = GetQueuePath(hostName, "system$;JOURNAL");
    queueList[0] = new MessageQueue(queuePath);

    // Get the Dead Letter queue
    queuePath = GetQueuePath(hostName, "system$;DEADLETTER");
    queueList[1] = new MessageQueue(queuePath);

    // Transactional Dead Letter Queue
    queuePath = GetQueuePath(hostName, "system$;DEADXACT");
    queueList[2] = new MessageQueue(queuePath);

    return queueList;
}

private static string GetQueuePath(string hostName, string queueName)
{
    return "FormatName:DIRECT=OS:" + hostName + @"\" + queueName;
}

The queuePath returned look correct: 返回的queuePath看起来正确:

"FormatName:DIRECT=OS:localhost\\system$;JOURNAL" “ FormatName:DIRECT = OS:localhost \\ system $; JOURNAL”

But an exception is thrown when I try to access the QueueName property or call the GetAllMessages() method: 但是,当我尝试访问QueueName属性或调用GetAllMessages()方法时,将引发异常:

"The specified format name does not support the requested operation. For example, a direct queue format name cannot be deleted." “指定的格式名称不支持所请求的操作。例如,不能删除直接队列格式名称。”

Any idea how I can programmatically retrieve the contents of the System Queues (System Journal, Dead Letter and Dead Letter Transactional)? 知道如何以编程方式检索系统队列的内容(系统日志,死信和事务性死信)吗?

So, turns out that certain properties of the MessageQueue class are not accessible for instances of the System queues. 因此,事实证明,系统队列实例无法访问MessageQueue类的某些属性。 QueueName being one of them! QueueName是其中之一!

I ended up creating a custom class, SystemMessageQueue, with a custom method GetSystemQueuesByMachine. 我最终使用自定义方法GetSystemQueuesByMachine创建了一个自定义类SystemMessageQueue。 This returns MessageQueue instances with custom names that I use to populate a ListView and I can still use GetAllMessages() against the MessageQueue instance. 这将返回具有自定义名称的MessageQueue实例,我使用该自定义名称填充ListView,但仍可以对MessageQueue实例使用GetAllMessages()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM