.NET 6.0 DI Service Registration Issue

Viewed 37

I am using a repository pattern and trying to register services in .NET 6.0 using DI.

In Program.cs,

builder.Services.AddSingleton<IDomain, Domain>();
builder.Services.AddScoped<IApiUserRepository, ApiUserRepository>();

My classes:

public class ApiUserRepository : IApiUserRepository
{
    public ApiUserRepository(Domain domain) { }
}

public class Domain : IDomain
{
    public Domain(IConfiguration configuration) { }
}

I am getting the following exception,

Unable to resolve service for type 'Repositories.Domain' while attempting to activate 'Repositories.ApiUserRepository'.

1 Answers

You should change the constructor of ApiUserRepository to inject IDomain in stead of Domain

Domain is registered only as IDomain, so Domain itself cannot be resolved.

Alternatively you can register the Domain class.

Related