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
parent
commit
54f1241099
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 7
      framework/src/Volo.Abp.EventBus.Azure/Volo/Abp/EventBus/Azure/AzureDistributedEventBus.cs
  2. 7
      framework/src/Volo.Abp.EventBus.Kafka/Volo/Abp/EventBus/Kafka/KafkaDistributedEventBus.cs
  3. 7
      framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/RabbitMqDistributedEventBus.cs
  4. 7
      framework/src/Volo.Abp.EventBus.Rebus/Volo/Abp/EventBus/Rebus/RebusDistributedEventBus.cs
  5. 5
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs
  6. 8
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs
  7. 6
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs
  8. 6
      framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs
  9. 6
      framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Local/LocalEventBus_Dynamic_Test.cs

7
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)

7
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)

7
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)

7
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)

5
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);
}

8
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<IJsonSerializer>();
var json = jsonSerializer.Serialize(data);
return jsonSerializer.Deserialize(targetType, json);
}
protected void ThrowOriginalExceptions(Type eventType, List<Exception> exceptions)

6
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);
}

6
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<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]

6
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<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]

Loading…
Cancel
Save