英文:
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<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?
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论