是否可以对已存在 Ninject 约定绑定的接口使用显式绑定?
Is it possible to use explicit binding for an interface where a Ninject convention binding already exists?
将我的接口之一绑定到 class
时,我收到 NinjectActivationException
Error activating IInitialUploadFolderCleaner More than one matching
bindings are available. Matching bindings: 1) binding from
IInitialUploadFolderCleaner to InitialUploadFolderCleaner 2) binding
from IInitialUploadFolderCleaner to InitialUploadFolderCleaner
Activation path: 1) Request for IInitialUploadFolderCleaner
Suggestions: 1) Ensure that you have defined a binding for
IInitialUploadFolderCleaner only once.
因此,我正在使用 BindDefaultInterface()
方法的约定绑定,但是对于此 class 我想添加一个构造函数参数 - 一个字符串。
为了实现这一点,我想指定显式绑定,希望 ninject 仅在未找到显式绑定时使用默认绑定。
kernel.Bind<IInitialUploadFolderCleaner>().To<InitialUploadFolderCleaner>().WithConstructorArgument("siteRootPath", HostingEnvironment.ApplicationPhysicalPath);
kernel.Bind(x =>
{
x.FromAssembliesMatching("MyPrefix.*")
.SelectAllClasses()
.BindDefaultInterface();
});
然而,事实证明这样行不通,或者需要一些额外的设置。
我没有在其他任何地方显式绑定此接口,而且当我重命名实现以使其不遵循命名约定时,它工作正常并且我得到所有构造函数参数 OK。
您应该能够在基于约定的绑定中排除特定类型,这样您仍然可以为它们编写显式绑定。
kernel.Bind<IInitialUploadFolderCleaner>().To<InitialUploadFolderCleaner>().WithConstructorArgument("siteRootPath", HostingEnvironment.ApplicationPhysicalPath);
kernel.Bind(x =>
{
x.FromAssembliesMatching("MyPrefix.*")
.SelectAllClasses()
.Excluding<InitialUploadFolderCleaner>()
.BindDefaultInterface();
});
将我的接口之一绑定到 class
时,我收到 NinjectActivationExceptionError activating IInitialUploadFolderCleaner More than one matching bindings are available. Matching bindings: 1) binding from IInitialUploadFolderCleaner to InitialUploadFolderCleaner 2) binding from IInitialUploadFolderCleaner to InitialUploadFolderCleaner Activation path: 1) Request for IInitialUploadFolderCleaner Suggestions: 1) Ensure that you have defined a binding for IInitialUploadFolderCleaner only once.
因此,我正在使用 BindDefaultInterface()
方法的约定绑定,但是对于此 class 我想添加一个构造函数参数 - 一个字符串。
为了实现这一点,我想指定显式绑定,希望 ninject 仅在未找到显式绑定时使用默认绑定。
kernel.Bind<IInitialUploadFolderCleaner>().To<InitialUploadFolderCleaner>().WithConstructorArgument("siteRootPath", HostingEnvironment.ApplicationPhysicalPath);
kernel.Bind(x =>
{
x.FromAssembliesMatching("MyPrefix.*")
.SelectAllClasses()
.BindDefaultInterface();
});
然而,事实证明这样行不通,或者需要一些额外的设置。
我没有在其他任何地方显式绑定此接口,而且当我重命名实现以使其不遵循命名约定时,它工作正常并且我得到所有构造函数参数 OK。
您应该能够在基于约定的绑定中排除特定类型,这样您仍然可以为它们编写显式绑定。
kernel.Bind<IInitialUploadFolderCleaner>().To<InitialUploadFolderCleaner>().WithConstructorArgument("siteRootPath", HostingEnvironment.ApplicationPhysicalPath);
kernel.Bind(x =>
{
x.FromAssembliesMatching("MyPrefix.*")
.SelectAllClasses()
.Excluding<InitialUploadFolderCleaner>()
.BindDefaultInterface();
});