aspnetcore/breaking-changes/9/middleware-constructors.md
Previously, when a middleware type with multiple satisfiable constructors was instantiated from the dependency injection container, the one with the most parameters was used. Now that only happens if the dependency injection container implements xref:Microsoft.Extensions.DependencyInjection.IServiceProviderIsService. If it doesn't, an exception is thrown at runtime.
.NET 9 RC 1
Previously, the first of the following two constructors was preferred (when both were satisfied) because it has more parameters.
public class CookiePolicyMiddleware
{
public CookiePolicyMiddleware(RequestDelegate next, IOptions<CookiePolicyOptions> options, ILoggerFactory factory)
{
// ...
}
public CookiePolicyMiddleware(RequestDelegate next, IOptions<CookiePolicyOptions> options)
{
// ...
}
}
Starting in .NET 9, neither constructor is preferred, and construction fails with an error like:
System.InvalidOperationException: 'Multiple constructors accepting all given argument types have been found in type 'Microsoft.AspNetCore.CookiePolicy.CookiePolicyMiddleware'. There should only be one applicable constructor.'
This change is a behavioral change.
The activation mechanism was changed to help support keyed dependency injection.
If this happens and you can't upgrade to a dependency injection container that implements xref:Microsoft.Extensions.DependencyInjection.IServiceProviderIsService, you can add the xref:Microsoft.Extensions.DependencyInjection.ActivatorUtilitiesConstructorAttribute to the preferred constructor of the affected middleware type.
This change is known to cause errors when instantiating xref:Microsoft.AspNetCore.CookiePolicy.CookiePolicyMiddleware?displayProperty=fullName with Autofac.Extensions.DependencyInjection 7.x.