Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [6.0.0] - 2026-05-28
### *Changes (Breaking)*
- RabbitMessageRouter now takes `IServiceProvider` instead of `IEnumerable<IMessageHandler>` and creates a DI scope per received message. This ensures scoped services (e.g. DbContext) are fresh for each message, preventing stale tracked entities from leaking across messages.
- Consumers must register handlers as Scoped (not Singleton) and pass `IServiceProvider` to the router.
- Added `Microsoft.Extensions.DependencyInjection.Abstractions` dependency.

## [5.2.0] - 2026-05-19
### *Changes*
- Make JsonMessageHandler<T>.HandleAsync(T, headers, ct) public instead of protected, allowing consuming applications to unit test their handler implementations directly with typed payloads rather than having to construct serialised Message objects
Expand Down
3 changes: 2 additions & 1 deletion src/Linn.Common.Messaging.RabbitMQ.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
<ImplicitUsings>enable</ImplicitUsings>
<AssemblyName>Linn.Common.Messaging.RabbitMQ</AssemblyName>
<PackageId>Linn.Common.Messaging.RabbitMQ</PackageId>
<Version>5.2.0</Version>
<Version>6.0.0</Version>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Linn.Common.Configuration" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.0" />
<PackageReference Include="RabbitMQ.Client" Version="7.2.1" />
</ItemGroup>
</Project>
20 changes: 10 additions & 10 deletions src/RabbitMessageRouter.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
using RabbitMQ.Client;
using Microsoft.Extensions.DependencyInjection;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

namespace Linn.Common.Messaging.RabbitMQ;

public class RabbitMessageRouter
{
private readonly IChannel channel;
private readonly Dictionary<string, IMessageHandler> handlers;
private readonly IServiceProvider serviceProvider;

public RabbitMessageRouter(
IChannel channel,
IEnumerable<IMessageHandler> handlers)
IServiceProvider serviceProvider)
{
this.channel = channel;

this.handlers = handlers
.GroupBy(h => h.RoutingKey)
.ToDictionary(
g => g.Key,
g => g.Single());
this.serviceProvider = serviceProvider;
}

public AsyncEventingBasicConsumer CreateConsumer(
Expand All @@ -28,7 +24,11 @@ public AsyncEventingBasicConsumer CreateConsumer(

consumer.ReceivedAsync += async (_, ea) =>
{
if (!this.handlers.TryGetValue(ea.RoutingKey, out var handler))
await using var scope = this.serviceProvider.CreateAsyncScope();
var handler = scope.ServiceProvider.GetServices<IMessageHandler>()
.FirstOrDefault(h => h.RoutingKey == ea.RoutingKey);

if (handler == null)
{
Console.WriteLine(
$"No handler registered for routing key '{ea.RoutingKey}'");
Expand Down
Loading