Skip to content

.Net Core System.InvalidOperationException: Unable to resolve service for type ‘X’ while attempting to activate ‘Y’

Cause: This occurs when trying to resolve a service, but the DI container cannot find a registration for the required dependency.

Example:

public class MyService
{
private readonly IRepository _repository;

public MyService(IRepository repository) // IRepository not registered
{
_repository = repository;
}
}

Solution: Ensure that all dependencies are properly registered:

services.AddScoped<IRepository, Repository>();

Leave a Reply