Sending a reply after the message handler has completed?

Apr 16, 2013 at 3:41 PM
Hello. I have a handler that provides me with a message and an IMessageContext. I store both these in a list and let the handler complete. At some point in the future I do some processing and try to send a reply by taking the context I stored and calling “context.Endpoint.MessageBus.Reply(myResponse)”. Unfortunately this throws an exception “Object reference not set to an instance of an object”. Is this asynchronous way of replying possible or can “reply” only be used within the handler?

Many thanks, Ian
Apr 16, 2013 at 5:21 PM
I'm pretty sure the context is short-lived. You should be able to send a reply later, the trick being to find a reference to the MessageBus. I haven't tried this, you probably need to poke around. Maybe if you store a reference to the message bus instead?
Apr 17, 2013 at 11:40 AM
I can get access to the MessageBus so that isn’t a problem. By stepping into the “Reply” code the problem is with the line “MessageEnvelope currentMessageEnvelope = MessageCurrentlyBeingReceived”, this comes back as null. When it then tries to get the MessageId from this object it throws a null exception. Anyone know if I can use “Send” and still get it to work in the same was as if I’d used “Reply” within the handler?
Apr 17, 2013 at 2:35 PM
I have implemented a fix which appears to work but will need some testing. I have added a new “Reply” function to the MessageBus class which is the same as the original Reply command but takes a “MessageEnvelope” parameter. I grab the messageEnvelope from the context in the handler and use it when I need to reply. This works fine in my application so hopefully I’m not breaking any internal code by doing this.
Apr 17, 2013 at 5:14 PM
That's probably fine, but I wonder if you could instead set the related id on a new message and just use Send.