diff --git a/framework/src/Volo.Abp.EventBus.Azure/Volo/Abp/EventBus/Azure/AzureDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus.Azure/Volo/Abp/EventBus/Azure/AzureDistributedEventBus.cs index 73bf5cfc3d..296f353681 100644 --- a/framework/src/Volo.Abp.EventBus.Azure/Volo/Abp/EventBus/Azure/AzureDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus.Azure/Volo/Abp/EventBus/Azure/AzureDistributedEventBus.cs @@ -206,12 +206,7 @@ public class AzureDistributedEventBus : DistributedEventBusBase, ISingletonDepen return PublishAsync(eventType, ConvertDynamicEventData(dynamicEventData.Data, eventType), onUnitOfWorkComplete); } - if (DynamicHandlerFactories.ContainsKey(eventName)) - { - return PublishAsync(typeof(DynamicEventData), dynamicEventData, onUnitOfWorkComplete); - } - - throw new AbpException($"Unknown event name: {eventName}"); + return PublishAsync(typeof(DynamicEventData), dynamicEventData, onUnitOfWorkComplete); } protected async override Task PublishToEventBusAsync(Type eventType, object eventData) diff --git a/framework/src/Volo.Abp.EventBus.Kafka/Volo/Abp/EventBus/Kafka/KafkaDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus.Kafka/Volo/Abp/EventBus/Kafka/KafkaDistributedEventBus.cs index 6a886afbc4..a5b2b2869a 100644 --- a/framework/src/Volo.Abp.EventBus.Kafka/Volo/Abp/EventBus/Kafka/KafkaDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus.Kafka/Volo/Abp/EventBus/Kafka/KafkaDistributedEventBus.cs @@ -206,12 +206,7 @@ public class KafkaDistributedEventBus : DistributedEventBusBase, ISingletonDepen return PublishAsync(eventType, ConvertDynamicEventData(dynamicEventData.Data, eventType), onUnitOfWorkComplete); } - if (DynamicHandlerFactories.ContainsKey(eventName)) - { - return PublishAsync(typeof(DynamicEventData), dynamicEventData, onUnitOfWorkComplete); - } - - throw new AbpException($"Unknown event name: {eventName}"); + return PublishAsync(typeof(DynamicEventData), dynamicEventData, onUnitOfWorkComplete); } protected override async Task PublishToEventBusAsync(Type eventType, object eventData) diff --git a/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/RabbitMqDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/RabbitMqDistributedEventBus.cs index 747c0ef4b3..b18afd2ea8 100644 --- a/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/RabbitMqDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/RabbitMqDistributedEventBus.cs @@ -236,12 +236,7 @@ public class RabbitMqDistributedEventBus : DistributedEventBusBase, IRabbitMqDis return PublishAsync(eventType, ConvertDynamicEventData(dynamicEventData.Data, eventType), onUnitOfWorkComplete); } - if (DynamicHandlerFactories.ContainsKey(eventName)) - { - return PublishAsync(typeof(DynamicEventData), dynamicEventData, onUnitOfWorkComplete); - } - - throw new AbpException($"Unknown event name: {eventName}"); + return PublishAsync(typeof(DynamicEventData), dynamicEventData, onUnitOfWorkComplete); } protected async override Task PublishToEventBusAsync(Type eventType, object eventData) 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 f192eeda4b..42ba15a10d 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 @@ -201,12 +201,7 @@ public class RebusDistributedEventBus : DistributedEventBusBase, ISingletonDepen return PublishAsync(eventType, ConvertDynamicEventData(dynamicEventData.Data, eventType), onUnitOfWorkComplete); } - if (DynamicHandlerFactories.ContainsKey(eventName)) - { - return PublishAsync(typeof(DynamicEventData), dynamicEventData, onUnitOfWorkComplete); - } - - throw new AbpException($"Unknown event name: {eventName}"); + return PublishAsync(typeof(DynamicEventData), dynamicEventData, onUnitOfWorkComplete); } protected async override Task PublishToEventBusAsync(Type eventType, object eventData) 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 f675f71b58..1d8e1ed680 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 @@ -180,11 +180,6 @@ public class LocalDistributedEventBus : DistributedEventBusBase, ISingletonDepen return PublishAsync(eventType, ConvertDynamicEventData(dynamicEventData.Data, eventType), onUnitOfWorkComplete, useOutbox); } - if (!DynamicEventNames.ContainsKey(eventName)) - { - throw new AbpException($"Unknown event name: {eventName}"); - } - return PublishAsync(typeof(DynamicEventData), dynamicEventData, onUnitOfWorkComplete, useOutbox); } diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs index 41fe39fb3f..e59cd95613 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs @@ -6,10 +6,10 @@ using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; -using System.Text.Json; using Volo.Abp.Collections; using Volo.Abp.DynamicProxy; using Volo.Abp.EventBus.Distributed; +using Volo.Abp.Json; using Volo.Abp.MultiTenancy; using Volo.Abp.Reflection; using Volo.Abp.Uow; @@ -228,8 +228,10 @@ public abstract class EventBusBase : IEventBus return data; } - var json = JsonSerializer.Serialize(data); - return JsonSerializer.Deserialize(json, targetType)!; + using var scope = ServiceScopeFactory.CreateScope(); + var jsonSerializer = scope.ServiceProvider.GetRequiredService(); + var json = jsonSerializer.Serialize(data); + return jsonSerializer.Deserialize(targetType, json); } protected void ThrowOriginalExceptions(Type eventType, List exceptions) diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs index 5d8947dc40..729afa2915 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs @@ -181,12 +181,6 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency return PublishAsync(eventType, ConvertDynamicEventData(dynamicEventData.Data, eventType), onUnitOfWorkComplete); } - var isDynamic = DynamicEventHandlerFactories.ContainsKey(eventName); - if (!isDynamic) - { - throw new AbpException($"Unknown event name: {eventName}"); - } - return PublishAsync(typeof(DynamicEventData), dynamicEventData, onUnitOfWorkComplete); } diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs index d6f4c2275c..b77f282a85 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs @@ -187,10 +187,10 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase } [Fact] - public async Task Should_Throw_For_Unknown_Event_Name() + public async Task Should_Not_Throw_For_Unknown_Event_Name() { - await Assert.ThrowsAsync(() => - DistributedEventBus.PublishAsync("NonExistentEvent", new { Value = 1 })); + // Publishing to an unknown event name should not throw (consistent with typed PublishAsync behavior) + await DistributedEventBus.PublishAsync("NonExistentEvent", new { Value = 1 }); } [Fact] diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/LocalEventBus_Dynamic_Test.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/LocalEventBus_Dynamic_Test.cs index c9d5180186..c9813b8717 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/LocalEventBus_Dynamic_Test.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/LocalEventBus_Dynamic_Test.cs @@ -118,10 +118,10 @@ public class LocalEventBus_Dynamic_Test : EventBusTestBase } [Fact] - public async Task Should_Throw_For_Unknown_Event_Name() + public async Task Should_Not_Throw_For_Unknown_Event_Name() { - await Assert.ThrowsAsync(() => - LocalEventBus.PublishAsync("NonExistentEvent", new { Value = 1 })); + // Publishing to an unknown event name should not throw (consistent with typed PublishAsync behavior) + await LocalEventBus.PublishAsync("NonExistentEvent", new { Value = 1 }); } [Fact]