Unable to resolve service for type while attempting to activate

For the Dependency Injection framework to resolve IRepository, it must first be registered with the container. For example, in ConfigureServices, add the following:

services.AddScoped<IRepository, MemoryRepository>();

For .NET 6+, which uses the new hosting model by default, add the following in Program.cs instead:

builder.Services.AddScoped<IRepository, MemoryRepository>();

AddScoped is just one example of a service lifetime:

For web applications, a scoped lifetime indicates that services are created once per client request (connection).

See the docs for more information on Dependency Injection in ASP.NET Core.

Leave a Comment