docs: fix dynamic events subscription pattern — use IocEventHandlerFactory instead of raw handler instances#25200
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Updates ABP documentation and tests to recommend and demonstrate dynamic distributed event subscriptions via IocEventHandlerFactory, avoiding captive-dependency pitfalls and aligning examples with ABP’s typed-handler DI behavior.
Changes:
- Updated distributed event bus docs to recommend
IocEventHandlerFactoryfor dynamic subscriptions and added a captive-dependency warning forSingleInstanceHandlerFactory. - Updated the community article examples to use DI-friendly dynamic handler subscription patterns (factory + constructor injection).
- Added unit tests to validate
IocEventHandlerFactorybehavior for dynamic handlers (DI resolution + disposal).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs | Adds tests covering DI resolution and disposal behavior when subscribing dynamic handlers via IocEventHandlerFactory. |
| docs/en/framework/infrastructure/event-bus/distributed/index.md | Documents IocEventHandlerFactory as the recommended dynamic subscription approach and warns against captive dependencies with single-instance handlers. |
| docs/en/Community-Articles/2026-03-23-Dynamic-Events-in-ABP/POST.md | Fixes the dynamic subscription pattern to use IocEventHandlerFactory and constructor injection; updates mixed handler example to avoid raw instances. |
hikalkan
approved these changes
Apr 2, 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.
Fix the dynamic events subscription examples to use
IocEventHandlerFactoryinstead of injectingIServiceProviderdirectly into the handler. The old pattern caused a captive dependency — scoped services resolved from the root container are never properly disposed.IocEventHandlerFactoryis ABP's own mechanism for typed handlers: it creates a new DI scope per event, resolves a fresh handler instance, callsHandleEventAsync, then disposes the scope. The handler can use normal constructor injection without any manual scope management.Also updated the distributed event bus reference docs and added unit tests verifying scope-per-event creation, DI injection, and handler disposal.