Skip to content

[Mocha] Fix the dispatch context never assigning its runtime - #10693

Merged
PascalSenn merged 2 commits into
ChilliCream:mainfrom
alisan3:ali/mocha-dispatch-context-runtime
Oct 27, 2026
Merged

PascalSenn merged 2 commits into
ChilliCream:mainfrom
alisan3:ali/mocha-dispatch-context-runtime

Conversation

@alisan3

@alisan3 alisan3 commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

In Mocha, IDispatchContext.Runtime is null while a dispatch middleware runs. Any middleware that reads it, for example to look up the outbound routes of the dispatched type through context.Runtime.Router, throws a NullReferenceException.

DispatchContext.Initialize receives the messaging runtime and uses it for Host and the default content type, but never assigns the Runtime property. Reset clears the property when the context returns to the pool, and ReceiveContext.Initialize assigns it, so the dispatch side was the only one missing the assignment. Every dispatch path goes through this method: publish, send, request, scheduled dispatch, and the fault, dead-letter, redelivery and outbox dispatchers.

This change assigns Runtime in DispatchContext.Initialize, in the same position as in ReceiveContext.Initialize.

The new test, Initialize_Should_AssignRuntime_When_MessageIsPublishedSentOrRequested, captures context.Runtime from a dispatch middleware for a publish, a send and a request, and checks that every captured value is the registered runtime instance. Before the fix all three cases failed with a null runtime. The request case also covers the reply dispatch.

DispatchContext.Initialize received the messaging runtime but only used it
for the host and the default content type, so IDispatchContext.Runtime was
null in every dispatch middleware. Assign it the way ReceiveContext does and
cover publish, send and request with a test.
@PascalSenn
PascalSenn merged commit 13b5197 into ChilliCream:main Oct 27, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants