Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Breaking change]: Handling of middleware types with multiple constructors #514

Open
1 of 3 tasks
amcasey opened this issue Oct 21, 2024 · 1 comment
Open
1 of 3 tasks
Labels
Breaking change Documented The breaking change has been published to the .NET Core docs

Comments

@amcasey
Copy link
Contributor

amcasey commented Oct 21, 2024

Description

Formerly, when a middleware type with multiple satisfiable constructors was instantiated from the dependency injection container, the one with the most parameters would be used. Now that only happens if the dependency injection container implements IServiceProviderIsService.

Version

.NET 9 RC 1

Previous behavior

Formerly, the first constructor was preferred (when both were satisfied) because it has more parameters.

public class CookiePolicyMiddleware
{
    public CookiePolicyMiddleware(RequestDelegate next, IOptions<CookiePolicyOptions> options, ILoggerFactory factory)
    {
        // Omitted for brevity
    }

    public CookiePolicyMiddleware(RequestDelegate next, IOptions<CookiePolicyOptions> options)
    {
        // Omitted for brevity
    }
}

New behavior

Now, 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.'

Type of breaking change

  • Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
  • Source incompatible: When recompiled using the new SDK or component or to target the new runtime, existing source code may require source changes to compile successfully.
  • Behavioral change: Existing binaries may behave differently at run time.

Reason for change

The activation mechanism was changed to help support keyed dependency injection.

Recommended action

If this happens and you can't upgrade to a dependency injection container that implements IServiceProviderIsService, you can add ActivatorUtilitiesConstructorAttribute to the preferred constructor of the affected middleware type.

Affected APIs

This is known to cause errors when instantiating Microsoft.AspNetCore.CookiePolicy.CookiePolicyMiddleware with Autofac.Extensions.DependencyInjection 7.x.

@dotnet-policy-service dotnet-policy-service bot locked and limited conversation to collaborators Oct 21, 2024
@amcasey
Copy link
Contributor Author

amcasey commented Oct 21, 2024

For discussion, please use dotnet/aspnetcore#57231.

@gewarren gewarren added the Documented The breaking change has been published to the .NET Core docs label Nov 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Breaking change Documented The breaking change has been published to the .NET Core docs
Projects
None yet
Development

No branches or pull requests

2 participants