c# - 使用 Ninject : Named binding or WithConstructorArgument doesn't work 进行多次注入(inject)

标签 c# asp.net-mvc ninject

我正在使用 C#、.NET Framework 4.7 和 Ninject 3.2.2.0 开发 ASP.NET MVC 5。

我正在尝试使用多重绑定(bind),但我不知道该怎么做:

container.Bind<IUnitOfWork>().To<TRZFDbContext>().InRequestScope();
container.Bind<IUnitOfWork>().To<ERPDbContext>().InRequestScope().Named("ERP");

我正在尝试命名绑定(bind)。

我使用 IUnitOfWork 作为 GenericRepository 构造函数的参数:

public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
    protected DbSet<TEntity> _dbSet;

    private DbContext _context;

    public GenericRepository(IUnitOfWork unitOfWork)
    {
        _context = (DbContext)unitOfWork;
        _dbSet = _context.Set<TEntity>();
    }

    [ ... ]

}

我有他们将要使用 ERPDbContextTRZFDbContext 的绑定(bind):

container.Bind<IGenericRepository<ProductGTINs>>().To<GenericRepository<ProductGTINs>>();

// ERP
container.Bind<IGenericRepository<IC_ORD_VIEW>>().To<GenericRepository<IC_ORD_VIEW>>();

第一个使用TRZFDbContext,第二个使用ERPDbContext

在以下 Controller 中:

public class ERPController : Controller
{
    private readonly IGenericRepository<IC_ORD_VIEW> ordViewRepository;

    public ERPController(IGenericRepository<IC_ORD_VIEW> ordViewRepository)
    {
        this.ordViewRepository = ordViewRepository;
    }

    [ ... ]
}

我收到这个错误:

Error activating IUnitOfWork
More than one matching bindings are available.
Matching bindings:
  1) binding from IUnitOfWork to TRZFDbContext
  2) binding from IUnitOfWork to ERPDbContext
Activation path:
  3) Injection of dependency IUnitOfWork into parameter unitOfWork of constructor of type GenericRepository{IC_ORD_VIEW}
  2) Injection of dependency IGenericRepository{IC_ORD_VIEW} into parameter ordViewRepository of constructor of type ERPController
  1) Request for ERPController

但是如果我改变构造函数:

public class ERPController : Controller
{
    private readonly IGenericRepository<IC_ORD_VIEW> ordViewRepository;

    public ERPController([Named("ERP")]IGenericRepository<IC_ORD_VIEW> ordViewRepository)
    {
        this.ordViewRepository = ordViewRepository;
    }

    [ ... ]
}

我得到错误:

Error activating IGenericRepository{IC_ORD_VIEW}
No matching bindings are available, and the type is not self-bindable.
Activation path:
  2) Injection of dependency IGenericRepository{IC_ORD_VIEW} into parameter ordViewRepository of constructor of type ERPController
  1) Request for ERPController

Suggestions:
  1) Ensure that you have defined a binding for IGenericRepository{IC_ORD_VIEW}.
  2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
  3) Ensure you have not accidentally created more than one kernel.
  4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
  5) If you are using automatic module loading, ensure the search path and filters are correct.

如何设置它必须使用哪个绑定(bind)?

最佳答案

第二个示例失败,因为您将 [Named("ERP")] 添加到 IGenericRepository 参数,但命名绑定(bind)是针对 IUnitOfWork,所以它没有找到任何匹配的绑定(bind)。

您可以在 IGenericRepository 绑定(bind)中提供构造函数参数,而不是使用命名绑定(bind),例如:

container.Bind<IGenericRepository<IC_ORD_VIEW>>()
    .To<GenericRepository<IC_ORD_VIEW>>()
    .WithConstructorArgument("ordViewRepository", context => new ERPDbContext());

关于c# - 使用 Ninject : Named binding or WithConstructorArgument doesn't work 进行多次注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51284548/

相关文章:

c# - AvalonDock 是否可以以编程方式将 DockableContent 状态更改为 float ?

c# - 将没有 LinqExpression 的自定义助手转换为使用 LinqExpression 的自定义助手

WCF 与 Ninject 抛出 ArgumentNullException

c# - Ninject.Web.Common 和 Ninject.Mvc 未将 DependencyResolver 设置为使用 NinjectDependencyResolver

c# - 我需要调用单个进程的准确CPU使用率

c# - 为什么 Console.WriteLine() 函数会遗漏字符串中的某些字符?

c# - 我将如何在 ASP.Net 服务器上运行连续任务?

c# - 样式与文件位置 bundle

asp.net-mvc - 使用 Ninject 重构依赖注入(inject)的 ASP.NET MVC 代码的真实示例

c# - 使用通配符递归地从文件夹复制文件(文件夹路径有通配符)