Search code examples
c#msmq

MSMQ - Cannot access a closed Stream when Sending message to another queue


I'm reading a message off of a request queue, processing the request, and then placing the result of the processed request on a response queue. If processing fails, I'm trying to move the request message onto a failed request queue using the same transaction used to read the original message.

failedRequestQueue.Send(message, transaction);
transaction.Commit();

However, calling Send fails with "Cannot access a closed Stream."


Solution

  • When reading the BodyStream of the original request message, I was using a StreamReader created within a using block, which was in turn closing the BodyStream of the message, preventing it from being read when moving it from the request queue to the failed request queue. Removing the using statement from the StreamReader fixed this.