c# - 使用 DI 时无法解析服务

标签 c# asp.net blazor

我正在尝试实现存储库模式并利用依赖注入(inject)。在下面的代码中,我有一个 Genmeric Repository 接口(interface)和类,其他存储库接口(interface)和类都从中派生,如下面的 InvestmentTransactionRepository 示例。

在我的 GenericRepository 类中,我尝试 DI 应用程序 dbContext。

代码:

接口(interface)/IGenericRepository.cs

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

接口(interface)/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.Controllers.InvestmentsController”时无法解析类型“WebApi.Shared.Repositories.InvestmentTransactionRepository”的服务

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

最佳答案

您正在向容器添加IInvestmentTransactionRepository(接口(interface)),但尝试注入(inject)和解析WebApi.Shared.Repositories.InvestmentTransactionRepository(该类)。您应该将类​​添加到容器或(更好)注入(inject)接口(interface)。

关于c# - 使用 DI 时无法解析服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75051221/

相关文章:

c# - 如何从在 asp.net 中用 scriptmanager.registerclientscript 编写的 javascript 确认框返回值?

ASP.NET MVC - 用于编辑的 ViewModel

c# - 如何使用组织身份验证从 Blazor Server 中的身份获取用户属性

c# - Blazor WebAssembly 托管在子文件夹中

c# - 如何将 httppostedfile 发布到 webapi

c# - 尝试在 C# WPF 应用程序中捕获

c# - 带有远程数据和 asp.net 的 jQuery select2

c# - 从使用 Excel-DNA 构建的 Excel .XLL 文件中解压内容

c# - 在哪里将 Razor 页面语言版本配置为 C# 6?

asp.net-core-mvc - Blazor 和 ASP.NET Core 3.0 MVC