Ninject的多重注入:命名绑定或使用WithConstructorArgument无法正常工作。

3

我正在开发一个使用C#、.NET Framework 4.7和Ninject 3.2.2.0的ASP.NET MVC 5应用程序。

我尝试使用多重绑定,但不知道如何实现:

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

我正在尝试使用命名绑定。

我将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

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

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

第一个使用TRZFDbContext,第二个使用ERPDbContext
在以下控制器中:
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;
    }

    [ ... ]
}

I get the error:

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.

我该如何设置它使用哪个绑定?
1个回答

0
第二个示例失败了,因为您正在将[Named("ERP")]添加到IGenericRepository参数中,但命名绑定是针对IUnitOfWork的,因此它找不到任何匹配的绑定。

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

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

我使用了.WithConstructorArgument("unitOfWork", new ERPDbContext()) - VansFannel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接