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

2

5

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 ?

Timothee answered 2/11, 2013 at 21:18 Comment(0)
S
10

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.

Symbolic answered 2/11, 2013 at 21:38 Comment(1)
Thanks for pointing out the documentation and for your comment Stephen, make sense. btw I have posted a follow up related question here .. #19748629 ;)Timothee
S
1

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.

Standard answered 31/7, 2017 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.