Browse Source
Allow publishing dynamic events without local subscribers, use IJsonSerializer for ConvertDynamicEventData
- Remove "Unknown event name" exception from all providers and LocalEventBus
to match typed PublishAsync behavior (no handler = silent, not exception)
- Use ABP's IJsonSerializer instead of System.Text.Json for ConvertDynamicEventData
to respect configured JSON serialization options
pull/25023/head
maliming
1 week ago
No known key found for this signature in database
GPG Key ID: A646B9CB645ECEA4
9 changed files with
15 additions and
44 deletions
framework/src/Volo.Abp.EventBus.Azure/Volo/Abp/EventBus/Azure/AzureDistributedEventBus.cs
framework/src/Volo.Abp.EventBus.Kafka/Volo/Abp/EventBus/Kafka/KafkaDistributedEventBus.cs
framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/RabbitMqDistributedEventBus.cs
framework/src/Volo.Abp.EventBus.Rebus/Volo/Abp/EventBus/Rebus/RebusDistributedEventBus.cs
framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs
framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs
framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs
framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs
framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/LocalEventBus_Dynamic_Test.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 )
@ -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 )
@ -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 )
@ -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 )
@ -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 ) ;
}
@ -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 < IJsonSerializer > ( ) ;
var json = jsonSerializer . Serialize ( data ) ;
return jsonSerializer . Deserialize ( targetType , json ) ;
}
protected void ThrowOriginalExceptions ( Type eventType , List < Exception > exceptions )
@ -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 ) ;
}
@ -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 < AbpException > ( ( ) = >
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]
@ -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 < AbpException > ( ( ) = >
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]