Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Ninject:Named binding或WithConstructor Argument的多次注入不';行不通_C#_Asp.net Mvc_Ninject - Fatal编程技术网

C# 使用Ninject:Named binding或WithConstructor Argument的多次注入不';行不通

C# 使用Ninject:Named binding或WithConstructor Argument的多次注入不';行不通,c#,asp.net-mvc,ninject,C#,Asp.net Mvc,Ninject,我正在开发一个ASP.NETMVC5,其中包含C#、.NETFramework 4.7和Ninject 3.2.2.0 我正在尝试使用多重绑定,但我不知道如何使用: container.Bind<IUnitOfWork>().To<TRZFDbContext>().InRequestScope(); container.Bind<IUnitOfWork>().To<ERPDbContext>().InRequestScope().Named("ER

我正在开发一个ASP.NETMVC5,其中包含C#、.NETFramework 4.7和Ninject 3.2.2.0

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

container.Bind<IUnitOfWork>().To<TRZFDbContext>().InRequestScope();
container.Bind<IUnitOfWork>().To<ERPDbContext>().InRequestScope().Named("ERP");
我有一些绑定,他们将使用
ERPDbContext
TRZFDbContext

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

// ERP
container.Bind<IGenericRepository<IC_ORD_VIEW>>().To<GenericRepository<IC_ORD_VIEW>>();
我得到这个错误:

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

    [ ... ]

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

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

    [ ... ]
}

如何设置它必须使用的绑定?

第二个示例失败,因为您正在将
[Named(“ERP”)]
添加到
IGenericRepository
参数,但命名绑定用于
IUnitOfWork
,因此它找不到任何匹配的绑定

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

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

我已经将
与ConstructorArgument(“unitOfWork”,new ERPDbContext())一起使用了
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.
container.Bind<IGenericRepository<IC_ORD_VIEW>>()
    .To<GenericRepository<IC_ORD_VIEW>>()
    .WithConstructorArgument("ordViewRepository", context => new ERPDbContext());