Skip to content

Commit 7761284

Browse files
author
a.shafie
committed
fix: filter out non-instantiable types during DI registration
1 parent e596748 commit 7761284

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

Changelog.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
## v2.2.1
44

5-
- **Fixed**: Support for record structs as message types (commands, queries, events). Previously record structs couldn't
6-
be registered due to a type filtering condition that only allowed class types.
7-
- **Improved**: Message registration to handle all non-System types, allowing for greater flexibility in message
8-
definitions.
5+
- **Fixed**: DI container registration now properly filters out interfaces and abstract classes during service registration. Previously, `RegisterFromAssembly()` would cause DI container errors when trying to register non-instantiable types. LiteBus message registry continues to accept all types to support polymorphic dispatch, but only concrete classes are registered with the DI container.
96

107
## v2.2.0
118

src/LiteBus.Messaging.Extensions.MicrosoftDependencyInjection/ModuleRegistry.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ private void RegisterHandlersFromDescriptor(IMessageDescriptor descriptor)
7979
// Register each type once
8080
foreach (var handlerType in descriptorHandlerTypes)
8181
{
82+
// Only register concrete classes with DI container - interfaces and abstract classes are kept in
83+
// LiteBus registry for polymorphic dispatch but cannot be instantiated by the DI container.
84+
// Without this filter, DI would throw "Cannot instantiate implementation type" errors.
85+
if (handlerType is { IsClass: true, IsAbstract: false })
86+
{
87+
_services.TryAddTransient(handlerType);
88+
}
89+
8290
_services.TryAddTransient(handlerType);
8391
}
8492
}

0 commit comments

Comments
 (0)