ASP.Net Core API Error (Unable to resolve service for type 'TapAPI.Models.TodoContext' while attempting to activate 'TapAPI.Controllers.TapController')

Huynh, Phuong Thao 81 Reputation points
2022-03-25T22:12:09.157+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:

187037-image.png

The error can be read here:

System.InvalidOperationException: Unable to resolve service for type 'TapAPI.Models.TodoContext' while attempting to activate 'TapAPI.Controllers.TapController'. at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired) at lambda_method3(Closure , IServiceProvider , Object[] ) at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass7_0.b__0(ControllerContext controllerContext) at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass6_0.g__CreateController|0(ControllerContext controllerContext) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) 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

Controller implementation:

187019-image.png

TodoContext Implementation:

187092-image.png

187112-image.png

Could someone please point me out where to look at to address this issue.

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,059 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,202 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 53,251 Reputation points
    2022-03-26T15:25:25.427+00:00

    You probably missed registering TodoContext with Services at startup.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Puneet Sharma 30 Reputation points
    2023-04-06T20:46:31.83+00:00

    go to the Program.cs and register the context in the builder. example:

    builder.Services.AddDbContext<TodoContext>(options =>
        options.UseSqlServer(connectionString));
    
    6 people found this answer helpful.