MapModule<T>(): resolve unregistered modules via ActivatorUtilities#18
Merged
carldebilly merged 2 commits intomainfrom Apr 4, 2026
Merged
MapModule<T>(): resolve unregistered modules via ActivatorUtilities#18carldebilly merged 2 commits intomainfrom
carldebilly merged 2 commits intomainfrom
Conversation
… parameterless modules ResolveModuleFromServices<T>() previously used GetService<T>(), which only resolved types explicitly registered in the container. Callers of MapModule<T>() had to pre-register their module or get an InvalidOperationException. Switch to ActivatorUtilities.GetServiceOrCreateInstance so that modules with a parameterless constructor (or constructor-injected dependencies already in the container) resolve without explicit registration — making MapModule<T>() a true first-class composition path alongside MapModule(new T()). Add [DynamicallyAccessedMembers] annotations for trim compatibility.
…ule path - Comment now says "constructor dependencies are resolved from DI" instead of "a parameterless constructor is enough" — ActivatorUtilities supports full constructor injection, not just parameterless types. - Add test that calls MapModule<T>() without registering the module in DI, verifying the CreateInstance fallback path with injected dependencies.
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.
Summary
ResolveModuleFromServices<T>()usedGetService<T>(), which only resolved types explicitly registered in the DI container. Callers ofMapModule<T>()had to pre-register their module or get anInvalidOperationException.ActivatorUtilities.GetServiceOrCreateInstance<T>()so modules with a parameterless constructor — or constructor dependencies already in the container — resolve without explicit registration.MapModule<T>()a true first-class composition path alongsideMapModule(new T()), enabling constructor injection in modules.Motivation
Without this fix, the only way to use
MapModule<T>()was to manually register the module type in the service collection. This defeated the purpose of the generic overload — callers ended up usingMapModule(new MyModule())instead, which bypasses DI entirely and forces[FromServices]on every handler parameter.With this fix, a module like:
resolves naturally via
app.MapModule<SessionModule>()as long asSessionManagerandViewerRegistryare registered — no module registration needed.Changes
IReplApp.cs[DynamicallyAccessedMembers]onMapModule<T>()generic parameterReplApp.csResolveModuleFromServices<T>()→ActivatorUtilities.GetServiceOrCreateInstance<T>()with trim annotationsTest plan
Given_ModuleCompositiontests pass (7/7) — validates both registered and unregistered module resolution