Get MSMQ Queue Counts in C#
2012-01-06 18:29
495 查看
I’mworkingwithNServiceBusandMSMQforoneofmyprojects,andIwantedtobeabletoshowasimpledashboardwiththenumbersofmessagesineachoftherelevantqueuesfortheapplication.Unfortunately,thereisn’tasimple".Count()”methodorproperty
inthebuilt-inSystem.MessagingnamespaceforMSMQqueues,soifyouwanttogetthemessagecountthereareafewwaystogoaboutit.YoucanuseCOMinterop,butjustas
thisblog’sauthor,Ididn’twanttotakethatdependency.Intheend,theresultIcameupwithisfromthatpost’scomments,whichistospecifyaMessagePropertyFilterontheQueue,andthenwhenyoucallGetAllMessages()itwillusethisfilterand
willavoidpullingbackthefullmessagebodycontentsaswellasavoidremovingthemessagesfromthequeue.Here’smysimplefunctionforfetchingthecountforagivenqueue:
protectedintGetMessageCount(MessageQueueq)
{
varfilter=newMessagePropertyFilter()
{
AdministrationQueue=false,
ArrivedTime=false,
CorrelationId=false,
Priority=false,
ResponseQueue=false,
SentTime=false,
Body=false,
Label=false,
Id=false
};
q.MessageReadPropertyFilter=filter;
returnq.GetAllMessages().Length;
}
IcreatedasimpleMVCControllertodisplaythecounts,withanactionlikethisone:
publicActionResultIndex()
{
stringmachine=Environment.MachineName;
string[]queues=new[]{machine+@"\private$\queue1",
machine+@"\private$\queue2",
machine+@"\private$\queue3"};
Dictionary<string,int>qcounts=newDictionary<string,int>();
foreach(varqueueinqueues)
{
varmessageQueue=newMessageQueue(queue);
qcounts.Add(queue,GetMessageCount(messageQueue));
}
returnView(qcounts);
}
Andjusttomakethingscomplete,here’stheView:
@modelSystem.Collections.Generic.Dictionary<string,int>
@{
ViewBag.Title="QueueCounts";
}
<h2>QueueCounts</h2>
<table>
<thead>
<tr>
<td>Queue</td>
<td>MessageCount</td>
</tr>
</thead>
<tbody>
@foreach(KeyValuePair<string,int>keyValuePairinModel)
{
<tr>
<td>@keyValuePair.Key</td>
<td>@keyValuePair.Value</td>
</tr>
}
<tr>
</tr>
</tbody>
</table>
Withthat,youcanquicklyviewthecountsofthequeuesonthelocalmachine.I’massumingthiswillworkforremotequeuesjustthesame,providedyouhavethenecessarysecuritycredentialsfortheappdomainyourwebappisrunningunder,butIadmitI’ve
notyettriedthat.
相关文章推荐
- Get Sender Email Address from Outlook using CDO in C#
- Get date without time in C#
- 获取本机已安装的软件列表C#/VB.NET(Get the list of installed software in local pc)
- C#实战Microsoft Messaging Queue(MSMQ)消息队列(干货)<转>
- how to get domain user info from AD in C#
- Get Selected Radio button’s Text in C# windows app
- 【c#】队列(Queue)和MSMQ(消息队列)的基础使用
- Can I get word under mouse in any program in C# app?
- C#实战Microsoft Messaging Queue(MSMQ)消息队列
- C#实战Microsoft Messaging Queue(MSMQ)消息队列(干货)
- My Web Access Code in C#(Basic Post&Get method) / C#访问Web代码(基本Post和Get方法)
- 'dispath_get_current_queue'is deprecated:fist deprecated in iOS 6.0的解决办法
- Get Volume Serial Number in C#
- Work Tip - Get only date in C#
- Get program files folder in C#
- C#实战Microsoft Messaging Queue(MSMQ)
- HttpWebRequest GET in C#
- Code snippet of the day : Get Chinese Spell in C#
- To check how many messages are in a MSMQ Queue
- C# 解决InvalidProgramException: Invalid IL code in System.Data.DataTable:get_Columns (): IL_0000: ret