AddTransient, AddScoped and AddSingleton Services Differences

TL;DR Transient objects are always different; a new instance is provided to every controller and every service. Scoped objects are the same within a request, but different across different requests. Singleton objects are the same for every object and every request. For more clarification, this example from .NET documentation shows the difference: To demonstrate the … Read more

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: 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.

ASP.NET Core Dependency Injection error: Unable to resolve service for type while attempting to activate

To break down the error message: Unable to resolve service for type ‘WebApplication1.Data.BloggerRepository’ while attempting to activate ‘WebApplication1.Controllers.BlogController’. That is saying that your application is trying to create an instance of BlogController but it doesn’t know how to create an instance of BloggerRepository to pass into the constructor. Now look at your startup: That is saying whenever a IBloggerRepository is required, create a BloggerRepository and … Read more