Home > Enterprise >  Unable to resolve service when using DI
Unable to resolve service when using DI

Time:01-09

I am attempting to implement a Repository pattern and make use of Dependency Injection. In the code below I have a Genmeric Repository interface and class from which other repository interfaces and classes are derived from, the example below InvestmentTransactionRepository.

In my GenericRepository class I am attempting to DI the application dbContext.

Code:

Interfaces/IGenericRepository.cs

namespace WebApi.Shared.Interfaces
{
    public interface IGenericRepository<T> where T : class
    {
    }
}

Interfaces/IInvestmentTransactionRepository.cs

namespace WebApi.Shared.Interfaces
{
    public interface IInvestmentTransactionRepository : IGenericRepository<InvestmentTransactionEntity>
    {
    }
}

/Repositories/GenericRepository.cs

using WebApi.Shared.Interfaces;

namespace WebApi.Shared.Repositories
{
    public class GenericRepository<T> : IGenericRepository<T> where T : class
    {
        protected readonly AccountingContext _context;

        public GenericRepository(AccountingContext context)
        {
            _context = context;
        }
    }
}

/Repositories/InvestmentTransactionRepository.cs

namespace WebApi.Shared.Repositories
{
    public class InvestmentTransactionRepository : GenericRepository<InvestmentTransactionEntity>, IInvestmentTransactionRepository
    {
        public InvestmentTransactionRepository(AccountingContext dbContext) : base(dbContext)
        {
        }
    }
}

/Controllers/InvestmentController.cs

namespace WebApi.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class InvestmentsController : ControllerBase
    {
        private Services.IUserService _userService;
        private Shared.Repositories.InvestmentTransactionRepository _investmentTransactionRepository;

        public InvestmentsController(Services.IUserService userService, 
                                     WebApi.Shared.Repositories.InvestmentTransactionRepository investmentTransactionRepository)
        {
            _userService = userService;
            _investmentTransactionRepository = investmentTransactionRepository;
        }

        [Authorize]
        [HttpPost("list")]
        public IActionResult List(RequestContext.Investment.ListDto request)
        {
        }
    }
}

/AccountingContext.cs

namespace WebApi.Shared
{
    public class AccountingContext : DbContext
    {
        public AccountingContext()
        {
        }

        public AccountingContext(DbContextOptions<AccountingContext> options) : base(options)
        {
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            // hidden
        }
    }
}

/Program.cs

services.AddScoped<AccountingContext>();
services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));
services.AddScoped<IInvestmentEntityRepository, InvestmentEntityRepository>();
services.AddScoped<IInvestmentTransactionRepository, InvestmentTransactionRepository>();

When I run the above, the build is succesfull but the following error is produced at run time:

Unable to resolve service for type "WebApi.Shared.Repositories.InvestmentTransactionRepository" while attempting to activate "WebApi.Controllers.InvestmentsController"

Can anyone see what I am doing wrong?

CodePudding user response:

You're adding IInvestmentTransactionRepository,the interface, to the container but attempting to inject and resolve WebApi.Shared.Repositories.InvestmentTransactionRepository, the class. You should either add the class to the container or (better) inject the interface.

  • Related