Skip to content

Don't register message types as services in lazy caching mode - #290

Open
yvanlo wants to merge 3 commits into
martinothamar:mainfrom
yvanlo:fix/lazy-message-type-registration-278
Open

yvanlo wants to merge 3 commits into
martinothamar:mainfrom
yvanlo:fix/lazy-message-type-registration-278

Conversation

@yvanlo

@yvanlo yvanlo commented Sep 21, 2026

Copy link
Copy Markdown

Fixes #278

Problem

In CachingMode.Lazy, handler wrappers were registered in DI using the message type as the service type:

services.Add(new ServiceDescriptor(typeof(MyCommand), _ => new CommandHandlerWrapper<MyCommand, string>(), Singleton));

ASP.NET Core Minimal APIs use IServiceProviderIsService to decide where to bind a parameter from. MyCommand was reported as a service, so it was bound from DI instead of the request body. The result was an InvalidCastException.

Fix

  • Wrappers are registered by their own type (RequestHandlerWrapper<,>, NotificationHandlerWrapper<>, ...), and the typed Send/Publish/CreateStream methods resolve them by that type.
  • For lookups based on the runtime type (projects above the "many messages" threshold), ContainerMetadata now has a HandlerWrapperTypes map from message type to wrapper type. It only holds typeof pairs, so the wrappers themselves are still resolved lazily.
  • Eager mode is unchanged.

Commits

  1. The template fix
  2. A test that message types are not registered as services (IServiceProviderIsService + GetService)
  3. Updated lazy snapshots (only cm=L snapshots change)

Testing

In CachingMode.Lazy, handler wrappers were registered in DI using the message type as the service type. ASP.NET Core Minimal APIs then bound message parameters from DI instead of from the request body (martinothamar#278). Wrappers are now registered by their own type, and message types are mapped to wrapper types for lookups based on the runtime type.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3.1.0-rc.1 CachingMode.Lazy: Minimal API body parameters are inferred as DI services after AddMediator

1 participant