From 37cd432a9c1ff7975d34a92dacd95db4c9ef806d Mon Sep 17 00:00:00 2001 From: Jimmy Bogard Date: Fri, 24 Nov 2023 18:02:50 -0600 Subject: [PATCH] Updating MediatR and switching to keyed services (#99) * Updating MediatR and switching to keyed services * Restoring the event types registration; removing unused HandlerTypes since it is using keyed services --- Directory.Packages.props | 4 ++-- .../Abstractions/EventBusSubscriptionInfo.cs | 2 -- .../Extensions/EventBusBuilderExtensions.cs | 21 +------------------ src/EventBusRabbitMQ/RabbitMQEventBus.cs | 10 +-------- 4 files changed, 4 insertions(+), 33 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 0de40a69..ede8fcdc 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -72,7 +72,7 @@ - + @@ -81,4 +81,4 @@ - + \ No newline at end of file diff --git a/src/EventBus/Abstractions/EventBusSubscriptionInfo.cs b/src/EventBus/Abstractions/EventBusSubscriptionInfo.cs index f00a284e..f18c47b1 100644 --- a/src/EventBus/Abstractions/EventBusSubscriptionInfo.cs +++ b/src/EventBus/Abstractions/EventBusSubscriptionInfo.cs @@ -7,8 +7,6 @@ public class EventBusSubscriptionInfo { public Dictionary EventTypes { get; } = []; - public Dictionary> HandlerTypes { get; } = []; - public JsonSerializerOptions JsonSerializerOptions { get; } = new(DefaultSerializerOptions); internal static readonly JsonSerializerOptions DefaultSerializerOptions = new() diff --git a/src/EventBus/Extensions/EventBusBuilderExtensions.cs b/src/EventBus/Extensions/EventBusBuilderExtensions.cs index 1208ceee..5fea83f7 100644 --- a/src/EventBus/Extensions/EventBusBuilderExtensions.cs +++ b/src/EventBus/Extensions/EventBusBuilderExtensions.cs @@ -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(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(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(); + eventBusBuilder.Services.AddKeyedTransient(typeof(T)); eventBusBuilder.Services.Configure(o => { @@ -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; diff --git a/src/EventBusRabbitMQ/RabbitMQEventBus.cs b/src/EventBusRabbitMQ/RabbitMQEventBus.cs index ef8d255e..71580a2c 100644 --- a/src/EventBusRabbitMQ/RabbitMQEventBus.cs +++ b/src/EventBusRabbitMQ/RabbitMQEventBus.cs @@ -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(eventType)) - - _subscriptionInfo.HandlerTypes.TryGetValue(eventType, out var handlerTypes); - - handlerTypes ??= []; - - foreach (var handlerType in handlerTypes) + foreach (var handler in scope.ServiceProvider.GetKeyedServices(eventType)) { - var handler = scope.ServiceProvider.GetRequiredService(handlerType) as IIntegrationEventHandler; - // Deserialize the event var integrationEvent = DeserializeMessage(message, eventType);