System.Messaging - why MessageQueue does not offer an asynchronous version of Send

StackOverflow https://stackoverflow.com/questions/19746992

  •  03-07-2022
  •  | 
  •  

Question

Would anyone know why System.Messaging is not offering an asynchronous version of the Send method to send an MSMQ message to a queue.

Actually there is asynchronous version of Peek and Receive methods (via Begin/End pairs that can be converted to a C#5 async awaitable method), but surprinsingly there is no BeginSend/EndSend methods offered, just a Send method which seems to me like it's a synchronous blocking I/O call.

I think this is not a limitation of System.Messaging but rather one of the native messaging queue API (mqrt.dll) that System.Messaging is using which takes an overlapped structure as a parameter in function MQReceiveMessage to use overlapping I/O with IOCP, whereas function MQsendMessage does not take such structure so seems like it's a purely synchronous call.

Still my question remains, anyone would know why MessageQueue API does not offer an asynchronous way of sending a message to a queue ?

Était-ce utile?

La solution

The MSMQ documentation states that a send is "always an asynchronous operation". It's been a while since I've worked with MSMQ, but IIRC as soon as you send, the message is flushed to disk locally before it even attempts to send over the network.

So, while it's not truly asynchronous (it has to wait for the disk write), it should be fairly fast.

Autres conseils

You are right, MessageQueue.Send is a blocking call. It does local IO but MSMQ is still going to disk and waiting for the result.

Call Task.Yield before you invoke any of the MessageQueue.Send overloads. This will improve the speed when you do a lot of sends.

This will make a big difference if your code base is async/await and you are not using threads anywhere.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top