String-Based Event Publishing with Dynamic Payload#25023
Merged
Conversation
Introduce a new IEventBus.PublishAsync(string eventName, ...) overload and make EventBusBase declare it. Implementations for Azure, Dapr, Kafka, RabbitMQ, Rebus, LocalDistributedEventBus and LocalEventBus resolve the event Type from an EventTypes map and delegate to the existing type-based PublishAsync. LocalEventBus now maintains an EventTypes dictionary (populated on Subscribe) to map event names to types. Unknown event names now throw an AbpException.
Introduce AnonymousEventData and add support for anonymous (name-based) events across the event bus implementations. Adds string-based Subscribe/Unsubscribe APIs, anonymous handler factories, and handling in distributed providers (Azure, Dapr, Kafka, RabbitMQ, Rebus) and local buses. Update EventBusBase and DistributedEventBusBase to resolve event names/data (GetEventName/GetEventData/ResolveEventForPublishing) and route/serialize/deserialize anonymous payloads. Also add AnonymousEventHandlerFactoryUnregistrar and minimal NullDistributedEventBus implementations, plus tests for anonymous local events.
Update event bus tests to avoid cross-test interference and ensure proper cleanup. In LocalDistributedEventBus_Test and LocalEventBus_Anonymous_Test: reset static handler state in test constructor, subscribe with IDisposable (using var subscription) so handlers are disposed after each test, replace hard-coded event names with generated unique event names, add missing System import, and adjust assertions (remove expectation of AbpException on publish after dispose). Also ensure local event bus subscriptions are stored/disposed. These changes make tests isolated and robust.
Replace direct System.Text.Json usage with the ABP Serializer for anonymous event payloads (deserialize to object) and remove the unused System.Text.Json using. Rework Subscribe(string, IEventHandlerFactory) to avoid duplicate handler registration, return a NullDisposable when already registered, add the consumer binding when the first anonymous handler is added (note: TODO for multi-threading), and keep the new unregistrar. Prevent AnonymousEventData from being added to EventTypes when adding to the outbox. Remove the old Subscribe implementation accordingly.
Add and centralize Subscribe/Unsubscribe(string, IEventHandlerFactory) implementations for Azure and Kafka to avoid duplicate anonymous handler registrations (checks IsInFactories / returns NullDisposable or skips adding). Switch Kafka anonymous payload handling from JsonElement to the generic Serializer.Deserialize<object> to preserve original types. Refactor RabbitMQ handler resolution to include anonymous handler factories by matching event names and return handler list early when concrete event type is found. Update DistributedEventBusBase to use ResolveEventForPublishing to obtain event name and data together, and ensure GetEventData is applied at the correct point when processing incoming events.
Introduce AnonymousEventData and add name-based (string) event publish/subscribe APIs across the event bus. Key changes: - New AnonymousEventData class with conversion helpers (ConvertToTypedObject/ConvertToTypedObject<T>/ConvertToTypedObject(Type)) and caching for JsonElement payloads. - Extended IEventBus and IDistributedEventBus interfaces with PublishAsync(string, ...), Subscribe/Unsubscribe/UnsubscribeAll overloads that accept string event names and factories/handlers. - DistributedEventBusBase implements name-based PublishAsync and Subscribe overloads and adapts anonymous event publishing to typed flows. - Updated concrete distributed bus implementations (Azure, Dapr, Kafka, RabbitMQ, Rebus, Local) to support anonymous handlers, inbox/outbox processing for anonymous events, serialization helpers, and handler-factory management. Changes include deduplication when registering factories, removal helpers for single-instance handlers, and preserving EventTypes mapping (AnonymousEventData is not added to EventTypes). - Fixed/centralized logic for mapping event names <-> event types, handler lookup (including anonymous handlers), and outbox/inbox processing so both typed and anonymous (name-based) events are handled consistently. Compatibility: existing typed event handling is preserved; new string-based APIs allow publishing and subscribing to events identified only by name.
RebusDistributedEventBus: properly handle AnonymousEventData by extracting its EventName when processing, wrap deserialized anonymous payloads into AnonymousEventData, and subscribe to AnonymousEventData when the first anonymous handler is registered (note: a TODO about multi-threading remains). DistDemoApp.MongoDbRebus Program: replace the previous Host/Serilog-driven async Main with an ABP application bootstrap using AbpApplicationFactory. The new Main initializes the ABP app, runs DemoService.CreateTodoItemAsync via AsyncHelper.RunSync, and then shuts down; prior Serilog/host startup code has been commented out and ABP logging/Serilog services are wired up.
Add support for anonymous events in the ASP.NET Core Dapr event bus module: when a topic is identified as anonymous, deserialize payloads as object and forward them as AnonymousEventData to handlers. In DaprDistributedEventBus, use GetEventName(eventType, eventData) when adding to the inbox (so dynamic/topic-based names are respected) and expose IsAnonymousEvent(eventName) to detect anonymous topics.
Introduce a new DistEvents test/demo suite and supporting infra. - Add AspNetCoreDapr and AzureEmulator sample apps (modules, programs, controllers, event handlers, appsettings). - Add persistence projects for EntityFrameworkCore and MongoDB, including EF Core migrations and updated model snapshot. - Update EfCoreRabbitMq project to support selectable DB provider (DistDemoDbProvider) and refactor RabbitMQ/Dapr event-bus configuration. - Add shared demo utilities (scenario runner/profile/hosted service), Dapr pubsub component, docker-compose and Service Bus emulator config. - Add comprehensive Visual Studio/.NET .gitignore and tweak .claude local permissions to allow "Bash(git show:*)".
Centralize and simplify anonymous event handling across transports. Introduces AnonymousEventDataConverter and changes AnonymousEventData to store optional JsonData with a FromJson factory. Adds helpers (CreateAnonymousEnvelope, TryPublishTypedByEventNameAsync, TryResolveStoredEventData, TryResolveIncomingEvent) and unifies handling logic in Dapr/Azure/Kafka/RabbitMQ/Rebus implementations. Also deduplicates subscription registration using locking, cleans up empty anonymous handler maps, and removes duplicated JSON conversion code. Tests updated to match the new anonymous event semantics.
Add GetHandlerType(IEventHandlerFactory) to determine handler Type from known factory implementations (SingleInstance, Ioc, Transient) instead of instantiating handlers. Update LocalEventBus to use this helper when reading LocalEventHandlerOrderAttribute to avoid unnecessary handler creation/disposal. Also switch DistEventScenarioRunner to use AnonymousEventDataConverter.ConvertToLooseObject(...) for anonymous event payload conversion.
Add native anonymous event support and simplify handling across transports. AnonymousEventData now contains conversion helpers (ConvertToTypedObject/ConvertToTypedObject<T>/ConvertToTypedObject -> loose typed object), caching JSON elements and replacing the removed AnonymousEventDataConverter. Multiple distributed event bus implementations (Azure, Dapr, Kafka, RabbitMQ, Rebus) were updated to: detect anonymous handlers via AnonymousHandlerFactories, construct AnonymousEventData when appropriate, resolve event types at publish/process time, simplify Subscribe/Unsubscribe logic (avoid duplicate-factory checks using IsInFactories then add), and throw on unknown event names in PublishAsync. AbpAspNetCoreMvcDaprEventBusModule was refactored to deserialize and trigger handlers inline for both envelope and direct Dapr events. Tests updated accordingly and a small cursor hook state file was added.
… event bus implementation
…across event bus implementations
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 49 out of 50 changed files in this pull request and generated 12 comments.
Files not reviewed (1)
- test/DistEvents/DistDemoApp.EfCoreRabbitMq/Migrations/20210910152547_Added_Boxes_Initial.Designer.cs: Language not supported
…remove dead IsDynamicEvent method from Dapr provider
…erializer 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
Contributor
|
Images automagically compressed by Calibre's image-actions ✨ Compression reduced images by 73%, saving 828.9 KB.
|
maliming
approved these changes
Mar 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related #24918
Summary
https://github.com/abpframework/abp/blob/dev/docs/en/Community-Articles/2026-03-23-Dynamic-Events-in-ABP/POST.md
Add dynamic (string-based) event publishing and subscription support to the ABP EventBus. This allows publishing and subscribing to events using string names instead of CLR types, enabling runtime event handling for scenarios where event types are not known at compile time.
Key changes
DynamicEventData— Pure POCO envelope class withEventNameandDatapropertiesIEventBus— NewPublishAsync(string, object),Subscribe(string, ...),Unsubscribe(string, ...),UnsubscribeAll(string)overloadsIDistributedEventBus— NewSubscribe(string, IDistributedEventHandler<DynamicEventData>)andPublishAsync(string, ..., useOutbox)EventBusBase—ResolveHandlerFactories,ResolveEventDataForHandler,ConvertDynamicEventData(virtual) for typed ↔ dynamic data conversionDynamicHandlerFactoriesdictionary,Subscribe(string),ProcessEventAsynchandlingAbpExceptionfor dynamic subscriptions (Dapr requires startup-time topic declaration)Design decisions
DynamicEventDatais treated as a "type")ConvertDynamicEventDatais virtual — providers can override it with their own serializerDynamicEventDatais a pure POCO (17 lines) — no serialization logic, noSystem.Text.Jsondependency in AbstractionsTest results
Unit tests — 51 tests passed
Covers Local EventBus and LocalDistributedEventBus: subscribe, unsubscribe, unsubscribeAll, mixed handlers, typed-from-dynamic publish, dynamic-only publish, data conversion, and more.
Integration tests — 32 scenarios passed across 4 providers
Integration test sample: abpframework/abp-samples#309
Test scenarios per provider:
DynamicEventData)