Message context - rebus-org/Rebus GitHub Wiki

Rebus' message context is available when you're inside a message handler. It's accessed via the MessageContext class, e.g. like so

var currentContext = MessageContext.Current;

which is perfectly safe to do anywhere you want, it will just return null when called outside of a message handler.

The message context is actually stored in the current ITransactionContext, which is a logically thread-bound context class that spans the receive-and-handle operation ("logically" thread-bound because it follows the execution context on to the next thread when you await something).

Rebus relies on the message context to do some of its things, e.g. it needs the message context to be able to reply to a message that is currently being handled.

You may also access the message context which gives you access to

  • the transaction context
  • the incoming step context
  • the current transport message
  • possibly the deserialized message