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

Huynh, Phuong Thao 81 Reputation points
2022-03-25T21:17:18.027+00:00

Hi Y'all,

If someone could please help me with the below issues?

I built a Web API using ASP.Net Core (from Microsoft Tutorial). When calling the API, I got this error below:

187036-image.png

The error can be read here:

System.InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'TapAPI.Controllers.TapController'. There should only be one applicable constructor. at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.TryFindMatchingConstructor(Type instanceType, Type[] argumentTypes, ConstructorInfo& matchingConstructor, Nullable1[]& parameterMap) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.FindApplicableConstructor(Type instanceType, Type[] argumentTypes, ConstructorInfo& matchingConstructor, Nullable1[]& matchingParameterMap) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateFactory(Type instanceType, Type[] argumentTypes) at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.CreateActivator(ControllerActionDescriptor descriptor) at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.CreateControllerFactory(ControllerActionDescriptor descriptor) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvokerCache.GetCachedResult(ControllerContext controllerContext) at Microsoft.AspNetCore.Mvc.Routing.ControllerRequestDelegateFactory.<>c__DisplayClass10_0.b__0(HttpContext context) at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext) --- End of stack trace from previous location --- at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) HEADERS ======= Accept: text/plain Host: localhost:7292 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.46 :method: GET Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9 Referer: https://localhost:7292/swagger/index.html sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="99", "Microsoft Edge";v="99" DNT: 1 sec-ch-ua-mobile: ?0 sec-ch-ua-platform: "Windows" sec-fetch-site: same-origin sec-fetch-mode: cors sec-fetch-dest: empty

The below is the code I implemented in my controller:
187083-image.png

I don't know why this error. Any help would be greatly appreciated.

Thank you and best regards,

Thao

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,294 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,376 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 59,306 Reputation points
    2022-03-25T21:25:37.18+00:00

    the error is pretty clear. you have two constructors, how does it know which one to call? you probably wanted one constructor with both DI parameters.

    3 people found this answer helpful.

0 additional answers

Sort by: Most helpful