Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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??
Posted
Comments
Sergey Alexandrovich Kryukov 2-Dec-15 19:40pm    
Are you asking about MSMQ service? If so, the question would sound strange. How it would be possible to not know about it? So, let me assume you are asking about your own service. But then, it would mean that the approach is wrong. You should not write code which relies on the presence of the message in the queue. You should read it unconditionally and get blocked...
—SA

1 solution

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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900