From 69849ead7ee4c987382723d00d7e157cad3de102 Mon Sep 17 00:00:00 2001 From: maliming Date: Sun, 2 Feb 2025 18:06:59 +0800 Subject: [PATCH] Make sure `eventType` is not `null`. --- .../AbpAspNetCoreMvcDaprEventBusModule.cs | 16 +++++++--- .../EventBus/Dapr/DaprDistributedEventBus.cs | 30 ++++++++----------- .../Rebus/RebusDistributedEventBus.cs | 7 ++++- .../Distributed/LocalDistributedEventBus.cs | 9 +++++- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprEventBusModule.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprEventBusModule.cs index a4a4f97f5d..fba9e12707 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprEventBusModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprEventBusModule.cs @@ -97,13 +97,21 @@ public class AbpAspNetCoreMvcDaprEventBusModule : AbpModule if (IsAbpDaprEventData(data)) { var daprEventData = daprSerializer.Deserialize(data, typeof(AbpDaprEventData)).As(); - var eventData = daprSerializer.Deserialize(daprEventData.JsonData, distributedEventBus.GetEventType(daprEventData.Topic)); - await distributedEventBus.TriggerHandlersAsync(distributedEventBus.GetEventType(daprEventData.Topic), eventData, daprEventData.MessageId, daprEventData.CorrelationId); + var eventType = distributedEventBus.GetEventType(daprEventData.Topic); + if (eventType != null) + { + var eventData = daprSerializer.Deserialize(daprEventData.JsonData, eventType); + await distributedEventBus.TriggerHandlersAsync(eventType, eventData, daprEventData.MessageId, daprEventData.CorrelationId); + } } else { - var eventData = daprSerializer.Deserialize(data, distributedEventBus.GetEventType(topic!)); - await distributedEventBus.TriggerHandlersAsync(distributedEventBus.GetEventType(topic!), eventData); + var eventType = distributedEventBus.GetEventType(topic); + if (eventType != null) + { + var eventData = daprSerializer.Deserialize(data, eventType); + await distributedEventBus.TriggerHandlersAsync(eventType, eventData); + } } httpContext.Response.StatusCode = 200; diff --git a/framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs index 2b0d2c7d0d..7c77340dda 100644 --- a/framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs @@ -153,7 +153,13 @@ public class DaprDistributedEventBus : DistributedEventBusBase, ISingletonDepend public async override Task PublishFromOutboxAsync(OutgoingEventInfo outgoingEvent, OutboxConfig outboxConfig) { - await PublishToDaprAsync(outgoingEvent.EventName, Serializer.Deserialize(outgoingEvent.EventData, GetEventType(outgoingEvent.EventName)), outgoingEvent.Id, outgoingEvent.GetCorrelationId()); + var eventType = GetEventType(outgoingEvent.EventName); + if (eventType == null) + { + return; + } + + await PublishToDaprAsync(outgoingEvent.EventName, Serializer.Deserialize(outgoingEvent.EventData, eventType), outgoingEvent.Id, outgoingEvent.GetCorrelationId()); using (CorrelationIdProvider.Change(outgoingEvent.GetCorrelationId())) { @@ -168,21 +174,9 @@ public class DaprDistributedEventBus : DistributedEventBusBase, ISingletonDepend public async override Task PublishManyFromOutboxAsync(IEnumerable outgoingEvents, OutboxConfig outboxConfig) { - var outgoingEventArray = outgoingEvents.ToArray(); - - foreach (var outgoingEvent in outgoingEventArray) + foreach (var outgoingEvent in outgoingEvents) { - await PublishToDaprAsync(outgoingEvent.EventName, Serializer.Deserialize(outgoingEvent.EventData, GetEventType(outgoingEvent.EventName)), outgoingEvent.Id, outgoingEvent.GetCorrelationId()); - - using (CorrelationIdProvider.Change(outgoingEvent.GetCorrelationId())) - { - await TriggerDistributedEventSentAsync(new DistributedEventSent() - { - Source = DistributedEventSource.Outbox, - EventName = outgoingEvent.EventName, - EventData = outgoingEvent.EventData - }); - } + await PublishFromOutboxAsync(outgoingEvent, outboxConfig); } } @@ -201,7 +195,7 @@ public class DaprDistributedEventBus : DistributedEventBusBase, ISingletonDepend public async override Task ProcessFromInboxAsync(IncomingEventInfo incomingEvent, InboxConfig inboxConfig) { - var eventType = EventTypes.GetOrDefault(incomingEvent.EventName); + var eventType = GetEventType(incomingEvent.EventName); if (eventType == null) { return; @@ -243,9 +237,9 @@ public class DaprDistributedEventBus : DistributedEventBusBase, ISingletonDepend ); } - public Type GetEventType(string eventName) + public Type? GetEventType(string eventName) { - return EventTypes.GetOrDefault(eventName)!; + return EventTypes.GetOrDefault(eventName); } protected virtual async Task PublishToDaprAsync(Type eventType, object eventData, Guid? messageId = null, string? correlationId = null) diff --git a/framework/src/Volo.Abp.EventBus.Rebus/Volo/Abp/EventBus/Rebus/RebusDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus.Rebus/Volo/Abp/EventBus/Rebus/RebusDistributedEventBus.cs index 9e8398a495..7a3d79e7d8 100644 --- a/framework/src/Volo.Abp.EventBus.Rebus/Volo/Abp/EventBus/Rebus/RebusDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus.Rebus/Volo/Abp/EventBus/Rebus/RebusDistributedEventBus.cs @@ -250,7 +250,12 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen OutgoingEventInfo outgoingEvent, OutboxConfig outboxConfig) { - var eventType = EventTypes.GetOrDefault(outgoingEvent.EventName)!; + var eventType = EventTypes.GetOrDefault(outgoingEvent.EventName); + if (eventType == null) + { + return; + } + var eventData = Serializer.Deserialize(outgoingEvent.EventData, eventType); var headers = new Dictionary(); diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs index 430c933bac..cdf2849c4a 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs @@ -72,6 +72,8 @@ public class LocalDistributedEventBus : DistributedEventBusBase, ISingletonDepen public override IDisposable Subscribe(Type eventType, IEventHandlerFactory factory) { + var eventName = EventNameAttribute.GetNameOrDefault(eventType); + EventTypes.GetOrAdd(eventName, eventType); return LocalEventBus.Subscribe(eventType, factory); } @@ -157,7 +159,12 @@ public class LocalDistributedEventBus : DistributedEventBusBase, ISingletonDepen EventData = outgoingEvent.EventData }); - var eventType = EventTypes.GetOrDefault(outgoingEvent.EventName)!; + var eventType = EventTypes.GetOrDefault(outgoingEvent.EventName); + if (eventType == null) + { + return; + } + var eventData = JsonSerializer.Deserialize(Encoding.UTF8.GetString(outgoingEvent.EventData), eventType)!; await LocalEventBus.PublishAsync(eventType, eventData, false); }