WCF MSMQ,服务如何知道消息是否在队列中? [英] WCF MSMQ, how do service know that the messages are sitting in the queue?

查看:89
本文介绍了WCF MSMQ,服务如何知道消息是否在队列中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



QQ,如果要用MSMQ公开WCF服务,而客户端则调用该服务来放置消息(或调用Invoke),我的服务如何知道在队列中查找消息?

Hello,

QQ, If were to expose a WCF service with MSMQ, and client were to call the service to place the messages (or to Invoke), how does my service know to look for the messages in the Queue??

推荐答案

请参阅我对该问题的评论。



从形式上讲,答案是:通过 System.Messaging.MessageQueue.GetAllMessages 读取当前的快照并查看其中的内容那里:

https://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.getallmessages%28v=vs.110%29.aspx [ ^ ]。



这是一个解决方案吗?没有!事实上,这将是一个很大的滥用。一般情况下,它甚至不可靠,因此是不可接受的。让我们来看看。您可能有多个线程(在同一进程中,或在几个不同的进程中)从同一队列中读取。你拍了一张快照,开始基于它阅读,但被阻止,因为其他一些线程删除了所有消息。



那么,该怎么办?关键字是已屏蔽。只需无条件地阅读消息并在呼叫时阻止当前线程,直到消息到达并获得读取。因此,它应该是一个专门用于此的单独线程。请参阅:

https://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.receive%28v=vs.110%29.aspx [ ^ ],

另见 https:/ /msdn.microsoft.com/en-us/library/system.messaging.messagequeue.peek%28v=vs.110%29.aspx [ ^ ]。



这个想法是:不做体检。 进攻性节目,而不是防守。只是收到或偷看,而不是询问你是否可以收到偷看,并可选择在你收到时处理事件。同一事物的替代变体是使用这些方法的异步版本:

https://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.beginreceive%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.beginpeek%28v=vs.110%29.aspx [ ^ ]。



这里的共同点是你使用推送方式,而不是拉,你在实际收到消息时处理事件,并且pe改变一些行动,比如说,进一步推动UI,或其他任何行动。



你是否明白这个想法?如果您的想法应该被修改,我将不会感到惊讶。但这是不可避免的。另见:

http://en.wikipedia.org/wiki/Pull_technology [< a href =http://en.wikipedia.org/wiki/Pull_technologytarget =_ blanktitle =New Window> ^ ],

https://en.wikipedia.org/wiki/Push_technology [ ^ ]。



另见我过去的答案,与你原来的问题相差甚远;但这需要考虑:网站帐户的应用程序'仪表板' [ ^ ]。



更一般地说,也许你需要接受相对论编程的想法:https://en.wikipedia.org/wiki/Relativistic_programming [ ^ ]。



-SA
Please see my comment to the question.

Formally speaking, the answer would be: read the current snapshot via System.Messaging.MessageQueue.GetAllMessages and see what's in there:
https://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.getallmessages%28v=vs.110%29.aspx[^].

Is it a solution? No! In fact, it would be a big abuse. In general case, it's not even reliable and hence won't be acceptable. Let's see. You may have more than one thread (in the same process, or in several different ones) reading from the same queue. You took a snapshot, started to read based on it, but get blocked, because some other thread removed all messages.

So, what to do? The keyword is "blocked". Just read the message unconditionally and get the current thread blocked at the call, until a message arrives and get read. Therefore, it should be a separate thread dedicated to this. Please see:
https://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.receive%28v=vs.110%29.aspx[^],
see also https://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.peek%28v=vs.110%29.aspx[^].

The idea is: no checkup. Offensive programming, not defensive. Just receive or peek, instead of asking if you can receive of peek, and, optionally, handle the event when you received. Alternative variant of the same thing is using asynchronous versions of these methods:
https://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.beginreceive%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.beginpeek%28v=vs.110%29.aspx[^].

The common point here is that you use push approach, not pull, you handle the event when you actually get the message, and perform some action, say, push it further, to the UI, or whatever else.

Are you getting the idea? I won't be surprised if it appears that your thinking should be changed, fixed. But this is unavoidable. See also:
http://en.wikipedia.org/wiki/Pull_technology[^],
https://en.wikipedia.org/wiki/Push_technology[^].

See also my past answer, quite far from your original question; but this is something to think about: Application 'dashboard' for website accounts[^].

And, more generally, perhaps you will need to embrace the idea of relativistic programming: https://en.wikipedia.org/wiki/Relativistic_programming[^].

—SA

这篇关于WCF MSMQ,服务如何知道消息是否在队列中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆