Dependency Injection error: Unable to resolve service for type while attempting to activate, while class is registered
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:
services.AddScoped<IBloggerRepository, BloggerRepository>();
That is saying whenever a IBloggerRepository
is required, create a BloggerRepository
and pass that in.
However, your controller class is asking for the concrete class BloggerRepository
and the dependency injection container doesn't know what to do when asked for that directly.
I'm guessing you just made a typo, but a fairly common one. So the simple fix is to change your controller to accept something that the DI container does know how to process, in this case, the interface:
public BlogController(IBloggerRepository repository)
// ^
// Add this!
{
_repository = repository;
}
Note that some objects have their own custom ways to be registered, this is more common when you use external Nuget packages, so it pays to read the documentation for them. For example if you got a message saying:
Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' ...
Then you would fix that using the custom extension method provided by that library which would be:
services.AddHttpContextAccessor();
For other packages - always read the docs.
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.
How do I resolve Unable to resolve service for type '' while attempting to activate ''
The quick fix would be to change the controller constructor to depend on the abstraction instead of the implementation since the abstraction is what was registered with the container.
//...
private readonly ApplicationDbContext _context;
private readonly IAsyncPaymentsService<PaymentDetail> _service;
public PaymentController(ApplicationDbContext context, IAsyncPaymentsService<PaymentDetail> service)
{
_context = context;
_service = service;
}
//...
However, the generic abstraction could derived to a closed type if so desired
public interface IPaymentService : IAsyncPaymentsService<PaymentDetail> {
}
applied to the implementation
public class PaymentService : IPaymentService {
//...omitted for brevity
}
registered with the container
services.AddTransient<IPaymentService, PaymentService>();
and refactored in the controller
//...
private readonly ApplicationDbContext _context;
private readonly IPaymentService _service;
public PaymentController(ApplicationDbContext context, IPaymentService service)
{
_context = context;
_service = service;
}
//...
ASP.Net Core - Unable to resolve service for type *Context while attempting to activate *Controller
Look like you are missing out inject SFTPContext
which is the DbContext
to DI container.
Program.cs
builder.Services.AddDbContext<SFTPContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString(/* ConectionString key from appsettings.json */)));
Reference
Dependency injection (services) - ASP.NET Core fundamentals | Microsoft Docs
Related Topics
Differencebetween a C# Reference and a Pointer
Getting Downloads Folder in C#
Multi-Tenant with Code First Ef6
What Method in the String Class Returns Only the First N Characters
Date Format Yyyy-Mm-Ddthh:Mm:Ssz
Who Should Call Dispose on Idisposable Objects When Passed into Another Object
Button Inside a Winforms Textbox
Why Is It Impossible to Override a Getter-Only Property and Add a Setter
Display Progress Bar While Doing Some Work in C#
Async Call with Await in Httpclient Never Returns
How to Run a .Net 4.5 App on Xp
Remove File Extension from a File Name String
How to Execute an X86 Assembly Sequence from Within C#
Send Http Post Message in ASP.NET Core Using Httpclient Postasjsonasync
What Is a Good Unique Pc Identifier
Unzip a Memorystream (Containing the Zip File) and Get the Files