From 17315bc866231530715628022e95eac12af591b0 Mon Sep 17 00:00:00 2001 From: Alexander Bohle Date: Mon, 26 Oct 2026 09:43:46 +0100 Subject: [PATCH] [Mocha] Fix the dispatch context never assigning its runtime 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. --- .../src/Mocha/Middlewares/DispatchContext.cs | 1 + .../Middlewares/MiddlewareTests.cs | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/Mocha/src/Mocha/Middlewares/DispatchContext.cs b/src/Mocha/src/Mocha/Middlewares/DispatchContext.cs index 53e8719201d..d57c2bf0352 100644 --- a/src/Mocha/src/Mocha/Middlewares/DispatchContext.cs +++ b/src/Mocha/src/Mocha/Middlewares/DispatchContext.cs @@ -226,6 +226,7 @@ public void Initialize( Services = services; Endpoint = endpoint; Transport = endpoint.Transport; + Runtime = runtime; Host = runtime.Host; CancellationToken = cancellationToken; MessageType = messageType; diff --git a/src/Mocha/test/Mocha.Tests/Middlewares/MiddlewareTests.cs b/src/Mocha/test/Mocha.Tests/Middlewares/MiddlewareTests.cs index 4b5c98b3dad..adb02615c77 100644 --- a/src/Mocha/test/Mocha.Tests/Middlewares/MiddlewareTests.cs +++ b/src/Mocha/test/Mocha.Tests/Middlewares/MiddlewareTests.cs @@ -1,3 +1,4 @@ +using System.Collections.Concurrent; using Microsoft.Extensions.DependencyInjection; using Mocha.Transport.InMemory; @@ -329,6 +330,65 @@ public async Task Pipeline_Should_ProcessAllRequestsWhenConcurrent_When_Concurre Assert.All(responses, r => Assert.Equal("Shipped", r.Status)); } + [Theory] + [InlineData(MessageKind.Publish, 1)] + [InlineData(MessageKind.Send, 1)] + [InlineData(MessageKind.Request, 2)] + public async Task Initialize_Should_AssignRuntime_When_MessageIsPublishedSentOrRequested( + string kind, + int expectedDispatchCount) + { + // arrange + // the middleware runs for every outgoing message, so a request also captures its reply + var capturedRuntimes = new ConcurrentQueue(); + var services = new ServiceCollection(); + services.AddSingleton(new MessageRecorder()); + var builder = services.AddMessageBus(); + builder.AddRequestHandler(); + builder.ConfigureMessageBus(b => b.UseDispatch( + new DispatchMiddlewareConfiguration( + (_, next) => context => + { + capturedRuntimes.Enqueue(context.Runtime); + return next(context); + }, + "CaptureRuntime"))); + builder.AddInMemory(t => + { + t.DeclareQueue("events"); + t.DispatchEndpoint("events").ToQueue("events").Publish(); + t.DeclareQueue("commands"); + t.DispatchEndpoint("commands").ToQueue("commands").Send(); + }); + + await using var provider = services.BuildServiceProvider(); + var runtime = (MessagingRuntime)provider.GetRequiredService(); + await runtime.StartAsync(CancellationToken.None); + + using var scope = provider.CreateScope(); + var bus = scope.ServiceProvider.GetRequiredService(); + + // act + switch (kind) + { + case MessageKind.Publish: + await bus.PublishAsync(new OrderCreated { OrderId = "runtime" }, CancellationToken.None); + break; + + case MessageKind.Send: + await bus.SendAsync(new ProcessPayment { Amount = 42.00m }, CancellationToken.None); + break; + + case MessageKind.Request: + await bus.RequestAsync(new GetOrderStatus { OrderId = "runtime" }, CancellationToken.None); + break; + } + + // assert + Assert.Equal(expectedDispatchCount, capturedRuntimes.Count); + Assert.All(capturedRuntimes, captured => Assert.Same(runtime, captured)); + } + private static readonly TimeSpan s_timeout = TimeSpan.FromSeconds(10); private static MessagingRuntime CreateRuntime(Action configure)