question

HuynhPhuongThao-3483 avatar image
0 Votes"
HuynhPhuongThao-3483 asked HuynhPhuongThao-3483 commented

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

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, Nullable`1[]& parameterMap) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.FindApplicableConstructor(Type instanceType, Type[] argumentTypes, ConstructorInfo& matchingConstructor, Nullable`1[]& 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

dotnet-aspnet-core-generaldotnet-aspnet-generaldotnet-aspnet-core-mvcdotnet-aspnet-core-webapi
image.png (443.2 KiB)
image.png (528.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Bruce-SqlWork avatar image
1 Vote"
Bruce-SqlWork answered HuynhPhuongThao-3483 commented

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.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you, Bruce! Now I have another error. But I will open it in a new question.

0 Votes 0 ·