gpt4 book ai didi

conventions - Ninject 3.0 中基于约定的绑定(bind)

转载 作者:行者123 更新时间:2023-12-01 09:34:44 25 4
gpt4 key购买 nike

我正在尝试在我的新项目上设置 Ninject,并且我想使用 convetions 扫描程序集,以便

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:

最佳答案

使用 SelectAllClasses 而不是 SelectAllTypes

关于conventions - Ninject 3.0 中基于约定的绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10352805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com