无法解析依赖项注入时的服务

huangapple go评论72阅读模式
英文:

Unable to resolve service when using DI

问题

我正在尝试实现仓储模式并使用依赖注入。在下面的代码中,我有一个通用仓储接口和类,其他仓储接口和类都是从中派生的,下面是InvestmentTransactionRepository的示例。

在我的GenericRepository类中,我试图进行应用程序dbContext的依赖注入。

代码:

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>();

当我运行以上代码时,构建成功,但在运行时出现以下错误:

无法解析类型“WebApi.Shared.Repositories.InvestmentTransactionRepository”,尝试激活“WebApi.Controllers.InvestmentsController”。

有人能看出我做错了什么吗?

英文:

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&lt;T&gt; where T : class
    {
    }
}

Interfaces/IInvestmentTransactionRepository.cs

namespace WebApi.Shared.Interfaces
{
    public interface IInvestmentTransactionRepository : IGenericRepository&lt;InvestmentTransactionEntity&gt;
    {
    }
}

/Repositories/GenericRepository.cs

using WebApi.Shared.Interfaces;

namespace WebApi.Shared.Repositories
{
    public class GenericRepository&lt;T&gt; : IGenericRepository&lt;T&gt; where T : class
    {
        protected readonly AccountingContext _context;

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

/Repositories/InvestmentTransactionRepository.cs

namespace WebApi.Shared.Repositories
{
    public class InvestmentTransactionRepository : GenericRepository&lt;InvestmentTransactionEntity&gt;, IInvestmentTransactionRepository
    {
        public InvestmentTransactionRepository(AccountingContext dbContext) : base(dbContext)
        {
        }
    }
}

/Controllers/InvestmentController.cs

namespace WebApi.Controllers
{
    [Route(&quot;[controller]&quot;)]
    [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(&quot;list&quot;)]
        public IActionResult List(RequestContext.Investment.ListDto request)
        {
        }
    }
}

/AccountingContext.cs

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

        public AccountingContext(DbContextOptions&lt;AccountingContext&gt; options) : base(options)
        {
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
        	// hidden
        }
    }
}

/Program.cs

services.AddScoped&lt;AccountingContext&gt;();
services.AddScoped(typeof(IGenericRepository&lt;&gt;), typeof(GenericRepository&lt;&gt;));
services.AddScoped&lt;IInvestmentEntityRepository, InvestmentEntityRepository&gt;();
services.AddScoped&lt;IInvestmentTransactionRepository, InvestmentTransactionRepository&gt;();

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?

答案1

得分: 4

你正在向容器中添加 IInvestmentTransactionRepository 接口,但尝试注入和解析 WebApi.Shared.Repositories.InvestmentTransactionRepository 类。你应该要么将类添加到容器中,要么(更好的方式)注入接口。

英文:

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.

huangapple
  • 本文由 发表于 2023年1月9日 04:54:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定