C# Ninject WithConstructorArgument Causing ActivationExcepti
本文关键字:ActivationExcepti Causing WithConstructorArgument Ninject | 更新日期: 2023-09-27 18:35:07
继我在这里遇到的另一个问题之后,我有一个MainConfig
对象,我想注入该对象的某些属性,而不是实际的MainConfig
对象本身。摘自我的模块Load
方法:
Bind<IConfig>()
.ToMethod<IConfig>(ctx => MainConfig.Load())
.InSingletonScope();
var config = Kernel.Get<IConfig>(); //this returns a fully-loaded IConfig, so I know this works
Bind<IProcessingBuffer>()
.To<ProcessingBuffer>()
.InSingletonScope()
.WithConstructorArgument("processingInterval",
ctx => ctx.Kernel.Get<IConfig>().WinService.DataProcessingInterval);
ProcessingBuffer
的构造函数为:
public ProcessingBuffer(int processingInterval, IProcessingResults procResults)
但是,当我尝试启动应用程序时,我得到了一个ActivationException
,因为processingInterval
没有注入ProcessingBuffer
。例外情况是:
Ninject.ActivationException was unhandled
Message=Error activating int
No matching bindings are available, and the type is not self-bindable.
Activation path:
5) Injection of dependency int into parameter processingInterval of constructor of type ProcessingBuffer
4) Injection of dependency ProcessingBuffer into parameter procBuffer of constructor of type MyCompanyDataService
3) Injection of dependency MyCompanyDataService into parameter instance of constructor of type NinjectServiceHost{MyCompanyDataService}
2) Injection of dependency NinjectServiceHost{MyCompanyDataService} into parameter dataHost of constructor of type MyCompanyWinService
1) Request for MyCompanyWinService
Suggestions:
1) Ensure that you have defined a binding for int.
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.
Source=Ninject
StackTrace:
at Ninject.KernelBase.Resolve(IRequest request) in c:'Projects'Ninject'ninject'src'Ninject'KernelBase.cs:line 362
at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent) in c:'Projects'Ninject'ninject'src'Ninject'Planning'Targets'Target.cs:line 197
at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) in c:'Projects'Ninject'ninject'src'Ninject'Planning'Targets'Target.cs:line 165
at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) in c:'Projects'Ninject'ninject'src'Ninject'Activation'Providers'StandardProvider.cs:line 114
at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target) in c:'Projects'Ninject'ninject'src'Ninject'Activation'Providers'StandardProvider.cs:line 96
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Ninject.Activation.Providers.StandardProvider.Create(IContext context) in c:'Projects'Ninject'ninject'src'Ninject'Activation'Providers'StandardProvider.cs:line 96
at Ninject.Activation.Context.Resolve() in c:'Projects'Ninject'ninject'src'Ninject'Activation'Context.cs:line 157
at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in c:'Projects'Ninject'ninject'src'Ninject'KernelBase.cs:line 386
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent) in c:'Projects'Ninject'ninject'src'Ninject'Planning'Targets'Target.cs:line 197
at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) in c:'Projects'Ninject'ninject'src'Ninject'Planning'Targets'Target.cs:line 165
at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) in c:'Projects'Ninject'ninject'src'Ninject'Activation'Providers'StandardProvider.cs:line 114
at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target) in c:'Projects'Ninject'ninject'src'Ninject'Activation'Providers'StandardProvider.cs:line 96
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Ninject.Activation.Providers.StandardProvider.Create(IContext context) in c:'Projects'Ninject'ninject'src'Ninject'Activation'Providers'StandardProvider.cs:line 96
at Ninject.Activation.Context.Resolve() in c:'Projects'Ninject'ninject'src'Ninject'Activation'Context.cs:line 157
at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in c:'Projects'Ninject'ninject'src'Ninject'KernelBase.cs:line 386
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent) in c:'Projects'Ninject'ninject'src'Ninject'Planning'Targets'Target.cs:line 197
at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) in c:'Projects'Ninject'ninject'src'Ninject'Planning'Targets'Target.cs:line 165
at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) in c:'Projects'Ninject'ninject'src'Ninject'Activation'Providers'StandardProvider.cs:line 114
at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target) in c:'Projects'Ninject'ninject'src'Ninject'Activation'Providers'StandardProvider.cs:line 96
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Ninject.Activation.Providers.StandardProvider.Create(IContext context) in c:'Projects'Ninject'ninject'src'Ninject'Activation'Providers'StandardProvider.cs:line 96
at Ninject.Activation.Context.Resolve() in c:'Projects'Ninject'ninject'src'Ninject'Activation'Context.cs:line 157
at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in c:'Projects'Ninject'ninject'src'Ninject'KernelBase.cs:line 386
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent) in c:'Projects'Ninject'ninject'src'Ninject'Planning'Targets'Target.cs:line 197
at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) in c:'Projects'Ninject'ninject'src'Ninject'Planning'Targets'Target.cs:line 165
at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) in c:'Projects'Ninject'ninject'src'Ninject'Activation'Providers'StandardProvider.cs:line 114
at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target) in c:'Projects'Ninject'ninject'src'Ninject'Activation'Providers'StandardProvider.cs:line 96
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Ninject.Activation.Providers.StandardProvider.Create(IContext context) in c:'Projects'Ninject'ninject'src'Ninject'Activation'Providers'StandardProvider.cs:line 96
at Ninject.Activation.Context.Resolve() in c:'Projects'Ninject'ninject'src'Ninject'Activation'Context.cs:line 157
at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in c:'Projects'Ninject'ninject'src'Ninject'KernelBase.cs:line 386
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter[] parameters) in c:'Projects'Ninject'ninject'src'Ninject'Syntax'ResolutionExtensions.cs:line 37
at MyCompany.WinService.Program.Main(String[] args) in C:'Projects'MyProject'MyCompanyWinService'Program.cs:line 41
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 ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
有什么方法可以确认我的WithConstructorArgument
lambda甚至被调用,然后跟踪它?我已经下载了 Ninject 源代码,但我无法确定在哪里设置断点以查找使用我的回调的位置。
该应用程序是一个使用 Ninject 3.0 和 Ninject.Extensions.Wcf 来承载 WCF TCP 服务的 Windows 服务。
编辑以添加:如果我更改ProcessingBuffer
构造函数以获取整个IConfig
对象并删除WithConstructorArgument
调用,则成功注入IConfig
对象,并且我可以访问相应的属性。但是,我想不再注入整个IConfig
对象来删除依赖项,而是只传入所需的数据。
您的MyCompanyDataService
采用ProcessingBuffer
而不是IProcessingBuffer
的实例。因此,您的绑定不适用。