Get MSMQ Queue Counts in C#

本文介绍了一种在不依赖COM互操作的情况下获取Microsoft Message Queuing (MSMQ)队列中消息数量的方法。通过使用MessagePropertyFilter并指定特定属性,可以避免加载完整消息内容并高效地获取队列中的消息计数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://aspalliance.com/2086_Get_MSMQ_Queue_Counts_in_C

 

I’m working with NServiceBus and MSMQ for one of my projects, and I wanted to be able to show a simple dashboard with the numbers of messages in each of the relevant queues for the application.  Unfortunately, there isn’t a simple ".Count()” method or property in the built-in System.Messaging namespace for MSMQ queues, so if you want to get the message count there are a few ways to go about it.  You can use COM interop, but just as this blog’s author, I didn’t want to take that dependency.  In the end, the result I came up with is from that post’s comments, which is to specify a MessagePropertyFilter on the Queue, and then when you call GetAllMessages() it will use this filter and will avoid pulling back the full message body contents as well as avoid removing the messages from the queue.  Here’s my simple function for fetching the count for a given queue:

 
   
protected int GetMessageCount(MessageQueue q)
   
{
   
    var filter = new MessagePropertyFilter()
   
                     {
   
                         AdministrationQueue = false,
   
                         ArrivedTime = false,
   
                         CorrelationId = false,
   
                         Priority = false,
   
                         ResponseQueue = false,
   
                         SentTime = false,
   
                         Body = false,
   
                         Label = false,
   
                         Id = false
   
                     };
   
    q.MessageReadPropertyFilter = filter;
   
    return q.GetAllMessages().Length;
   
}

I created a simple MVC Controller to display the counts, with an action like this one:

 
   
public ActionResult Index()
   
{
   
    string machine = Environment.MachineName;
   
    string[] queues = new []{machine + @"\private$\queue1", 
   
        machine + @"\private$\queue2", 
   
        machine + @"\private$\queue3"};
   
 
   
    Dictionary<string, int> qcounts = new Dictionary<string, int>();
   
 
   
 
   
    foreach (var queue in queues)
   
    {
   
        var messageQueue = new MessageQueue(queue);
   
        qcounts.Add(queue, GetMessageCount(messageQueue));
   
    }
   
 
   
    return View(qcounts);
   
}

And just to make things complete, here’s the View:

 
   
@model System.Collections.Generic.Dictionary<string,int>
   
@{
   
    ViewBag.Title = "Queue Counts";
   
}
   
<h2>Queue Counts</h2>
   
<table>
   
    <thead>
   
        <tr>
   
            <td>Queue</td>
   
            <td>Message Count</td>
   
        </tr>
   
    </thead>
   
    <tbody>
   
    @foreach (KeyValuePair<string, int> keyValuePair in Model)
   
    {
   
    <tr>
   
        <td>@keyValuePair.Key</td>
   
        <td>@keyValuePair.Value</td>
   
    </tr>
   
    }
   
    <tr>
   
    </tr>
   
    </tbody>
   
</table>
With that, you can quickly view the counts of the queues on the local machine.  I’m assuming this will work for remote queues just the same, provided you have the necessary security credentials for the appdomain your web app is running under, but I admit I’ve not yet tried that.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值