Ninject 3.0中基于约定的绑定

时间:2012-04-27 14:47:06

标签: conventions ninject-3

我正在尝试在我的新项目上设置Ninject,我想使用对流扫描装配,以便

IFoo

将自动解析为

IBar

所有样本和文档都描述了使用这种方法

kernel.Scan(...)

虽然Ninject 3内核似乎使用了.Load。如果我使用Load进行扫描,那么如何使用约定配置扫描?

修改

我能够使用https://github.com/ninject/ninject.extensions.conventions/wiki/What-is-configuration-by-convention

让它正常工作

但是,现在我无法加载我的模块。我正在使用下面的代码并得到以下错误。

    IKernel kernel = new StandardKernel();
    kernel.Bind(x => x
        .FromAssembliesMatching("Crt.*.dll")
        .SelectAllTypes()
        .BindAllInterfaces()
        );
    kernel.Load("*.dll");
    return kernel;

错误:

Ninject.ActivationException was unhandled
  Message=Error activating ITrainingEngine
More than one matching bindings are available.
Activation path:
  1) Request for ITrainingEngine

Suggestions:
  1) Ensure that you have defined a binding for ITrainingEngine only once.

  Source=Ninject
  StackTrace:
       at Ninject.KernelBase.Resolve(IRequest request) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 380
       at Ninject.ResolutionExtensions.GetResolutionIterator(IResolutionRoot root, Type service, Func`2 constraint, IEnumerable`1 parameters, Boolean isOptional, Boolean isUnique) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:line 263
       at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter[] parameters) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:line 37
       at Crt.BlackBox.Train.Program.Run() in C:\code\Crimson\CRT\readmission\src\Crt.BlackBox\Crt.BlackBox.Train\Program.cs:line 54
       at Crt.BlackBox.Train.Program.Main(String[] args) in C:\code\Crimson\CRT\readmission\src\Crt.BlackBox\Crt.BlackBox.Train\Program.cs:line 24
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

1 个答案:

答案 0 :(得分:8)

使用SelectAllClasses而不是SelectAllTypes