Skip to content

Commit

Permalink
Updating MediatR and switching to keyed services (#99)
Browse files Browse the repository at this point in the history
* Updating MediatR and switching to keyed services

* Restoring the event types registration; removing unused HandlerTypes since it is using keyed services
  • Loading branch information
jbogard authored Nov 25, 2023
1 parent b9331ab commit 37cd432
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 33 deletions.
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<PackageVersion Include="Automapper" Version="12.0.1" />
<PackageVersion Include="Dapper" Version="2.0.151" />
<PackageVersion Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageVersion Include="MediatR" Version="12.1.1" />
<PackageVersion Include="MediatR" Version="12.2.0" />
<PackageVersion Include="Microsoft.Web.LibraryManager.Build" Version="2.1.175" />
<PackageVersion Include="Polly" Version="8.2.0" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
Expand All @@ -81,4 +81,4 @@
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.1" />
<PackageVersion Include="Yarp.ReverseProxy" Version="2.1.0-preview.1.23556.5" />
</ItemGroup>
</Project>
</Project>
2 changes: 0 additions & 2 deletions src/EventBus/Abstractions/EventBusSubscriptionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ public class EventBusSubscriptionInfo
{
public Dictionary<string, Type> EventTypes { get; } = [];

public Dictionary<Type, HashSet<Type>> HandlerTypes { get; } = [];

public JsonSerializerOptions JsonSerializerOptions { get; } = new(DefaultSerializerOptions);

internal static readonly JsonSerializerOptions DefaultSerializerOptions = new()
Expand Down
21 changes: 1 addition & 20 deletions src/EventBus/Extensions/EventBusBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ public static IEventBusBuilder ConfigureJsonOptions(this IEventBusBuilder eventB
// Use keyed services to register multiple handlers for the same event type
// the consumer can use IKeyedServiceProvider.GetKeyedService<IIntegrationEventHandler>(typeof(T)) to get all
// handlers for the event type.

// MediatR assembly scanning breaks with keyed services see https://github.com/jbogard/MediatR/issues/942.
// eventBusBuilder.Services.AddKeyedTransient<IIntegrationEventHandler, TH>(typeof(T));

// Instead, we'll register the handler as transient and use the HandlerTypes on SubscriptionInfo to get all
// handlers for the event type.
eventBusBuilder.Services.AddTransient<TH>();
eventBusBuilder.Services.AddKeyedTransient<IIntegrationEventHandler, TH>(typeof(T));

eventBusBuilder.Services.Configure<EventBusSubscriptionInfo>(o =>
{
Expand All @@ -39,19 +33,6 @@ public static IEventBusBuilder ConfigureJsonOptions(this IEventBusBuilder eventB
// This list will also be used to subscribe to events from the underlying message broker implementation.
o.EventTypes[typeof(T).Name] = typeof(T);
// Handle the case where the same handler is registered twice for the same event type
if (o.HandlerTypes.TryGetValue(typeof(T), out var handlerTypes))
{
if (!handlerTypes.Add(typeof(TH)))
{
throw new InvalidOperationException($"Handler Type {typeof(TH).GetGenericTypeName()} already registered for '{typeof(T)}'");
}
}
else
{
o.HandlerTypes[typeof(T)] = [typeof(TH)];
}
});

return eventBusBuilder;
Expand Down
10 changes: 1 addition & 9 deletions src/EventBusRabbitMQ/RabbitMQEventBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,8 @@ private async Task ProcessEvent(string eventName, string message)
// REVIEW: This could be done in parallel

// Get all the handlers using the event type as the key
// foreach (var handler in scope.ServiceProvider.GetKeyedServices<IIntegrationEventHandler>(eventType))

_subscriptionInfo.HandlerTypes.TryGetValue(eventType, out var handlerTypes);

handlerTypes ??= [];

foreach (var handlerType in handlerTypes)
foreach (var handler in scope.ServiceProvider.GetKeyedServices<IIntegrationEventHandler>(eventType))
{
var handler = scope.ServiceProvider.GetRequiredService(handlerType) as IIntegrationEventHandler;

// Deserialize the event
var integrationEvent = DeserializeMessage(message, eventType);

Expand Down

0 comments on commit 37cd432

Please sign in to comment.