ASP .NET 9 API Error (Multiple constructors accepting all given argument types have been found)

Tran, Truong Minh 0 Reputation points
2025-04-09T16:10:46.2833333+00:00

I got the error below after upgrading from .NET 8 to .NET 9.

I checked in the NuGet Package Manager, but my solution does not contain Microsoft.AspNetCore.Cors package.

InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware'. There should only be one applicable constructor.

Microsoft.Extensions.DependencyInjection.ActivatorUtilities.TryFindMatchingConstructor(Type instanceType, Type[] argumentTypes, out ConstructorInfo matchingConstructor, out Nullable<int>[] parameterMap)
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.FindApplicableConstructor(Type instanceType, Type[] argumentTypes, ConstructorInfoEx[] constructors, out ConstructorInfo matchingConstructor, out Nullable<int>[] matchingParameterMap)
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, object[] parameters)
Microsoft.AspNetCore.Builder.UseMiddlewareExtensions+ReflectionMiddlewareBinder.CreateMiddleware(RequestDelegate next)
Microsoft.AspNetCore.Builder.ApplicationBuilder.Build()
Microsoft.AspNetCore.Hosting.WebHost.BuildApplication()
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
420 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. SurferOnWww 4,391 Reputation points
    2025-04-15T05:24:15.68+00:00

    I checked in the NuGet Package Manager, but my solution does not contain Microsoft.AspNetCore.Cors package.

    You do not have to install NuGet package.

    Only addition of the service and middleware in the Program.cs according to the Microsoft document CORS with named policy and middleware will enable the CORS. There is no difference between .NET 8.0 and .NET 9.0.

    0 comments No comments

  2. Tran, Truong Minh 0 Reputation points
    2025-04-15T05:27:56.19+00:00

    I found the root cause.

    In Startup.cs, I called UseCors() twice, and then this issue happened.

    The solution is simple: just remove one UseCors() caller.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.