ASP.NET Core “Unable to Resolve Service for Type” Error: Causes and Dependency Injection Solutions

ASP.NET Core “Unable to Resolve Service for Type” Error: Causes and Dependency Injection Solutions

A common Dependency Injection error in ASP.NET Core

When developing ASP.NET Core applications, you may encounter an exception such as:

InvalidOperationException: Unable to resolve service for type 'MyProject.Services.IProductService' while attempting to activate 'MyProject.Controllers.ProductController'. 

Although the message may initially look complicated, ASP.NET Core is actually explaining the problem clearly.

ASP.NET Core is essentially saying:

“I am trying to create ProductController, but I do not know how to create the IProductService required by its constructor.”

Understanding Dependency Injection

Consider this controller:

public class ProductController : Controller { private readonly IProductService _productService; public ProductController(IProductService productService) { _productService = productService; } public IActionResult Index() { var products = _productService.GetAll(); return View(products); } } 

The controller does not manually create a ProductService. Instead, it requests an IProductService through its constructor.

The ASP.NET Core Dependency Injection container must know which implementation should be created when IProductService is requested.

Registering the service

Assume we have:

public interface IProductService { List<Product> GetAll(); } public class ProductService : IProductService { public List<Product> GetAll() { return new List<Product>(); } } 

The registration should be added to Program.cs:

builder.Services.AddScoped<IProductService, ProductService>(); 

The container can now resolve:

IProductService ↓ ProductService ↓ ProductController 

1. The service registration is missing

This is the most common cause.

If the controller requests:

IProductService 

but Program.cs does not contain a corresponding registration, the application cannot build the controller.

Solution:

builder.Services.AddScoped<IProductService, ProductService>(); 

2. A nested dependency is missing

Sometimes the controller dependency is registered correctly, but the service itself depends on another service that has not been registered.

public class ProductService : IProductService { private readonly IProductRepository _repository; public ProductService(IProductRepository repository) { _repository = repository; } } 

The dependency chain becomes:

ProductController ↓ IProductService ↓ ProductService ↓ IProductRepository ↓ ??? 

Register the repository as well:

builder.Services.AddScoped<IProductRepository, ProductRepository>(); builder.Services.AddScoped<IProductService, ProductService>(); 

3. DbContext is not registered

Entity Framework Core repositories commonly depend on a DbContext.

public class ProductRepository : IProductRepository { private readonly AppDbContext _context; public ProductRepository(AppDbContext context) { _context = context; } } 

Register it using AddDbContext:

builder.Services.AddDbContext<AppDbContext>(options => options.UseSqlServer( builder.Configuration.GetConnectionString("SqlServer"))); 

4. Scoped, Transient and Singleton

ASP.NET Core provides three commonly used service lifetimes:

  • Transient: A new instance is created each time it is requested.
  • Scoped: The same instance is normally used during a single HTTP request.
  • Singleton: One instance is used for the lifetime of the application.

Typical registrations:

builder.Services.AddTransient<IMailService, MailService>(); builder.Services.AddScoped<IProductService, ProductService>(); builder.Services.AddSingleton<ICacheService, CacheService>(); 

5. Do not inject scoped services directly into singletons

Consider:

builder.Services.AddScoped<IProductRepository, ProductRepository>(); builder.Services.AddSingleton<IReportService, ReportService>(); 

If ReportService directly depends on IProductRepository, ASP.NET Core can report a lifetime error.

If ReportService does not need to be Singleton, register it as Scoped:

builder.Services.AddScoped<IReportService, ReportService>(); 

6. Read the dependency chain

When debugging the exception, find the service mentioned in:

Unable to resolve service for type '...' 

Then inspect the constructor of the class mentioned after:

while attempting to activate '...' 

This usually reveals exactly where the Dependency Injection chain is broken.

A typical layered architecture

ProductController ↓ IProductService ↓ ProductService ↓ IProductRepository ↓ ProductRepository ↓ AppDbContext ↓ SQL Server 

Every required dependency in this chain must be registered correctly.

Organizing service registrations

Large applications can move registrations into extension methods:

public static class ServiceRegistration { public static IServiceCollection AddApplicationServices( this IServiceCollection services) { services.AddScoped<IProductService, ProductService>(); services.AddScoped<ICategoryService, CategoryService>(); services.AddScoped<IProductRepository, ProductRepository>(); services.AddScoped<ICategoryRepository, CategoryRepository>(); return services; } } 

Program.cs:

builder.Services.AddApplicationServices(); 

Debugging checklist

  1. Find the unresolved service type in the exception.
  2. Check its Program.cs registration.
  3. Verify the interface and implementation pairing.
  4. Inspect constructor dependencies.
  5. Verify DbContext registration.
  6. Check Scoped, Transient and Singleton lifetimes.
  7. Verify namespaces and project references.

Conclusion

The ASP.NET Core “Unable to resolve service for type” exception is usually caused by a broken link in the Dependency Injection chain.

Instead of focusing only on the controller, follow the complete dependency path:

Controller ↓ Service ↓ Repository ↓ DbContext 

Once the missing or incorrectly configured dependency is identified, the fix is often only a small service-registration change in Program.cs.

//

0 Comments

Write Comment

E-Mail address will not be published