在我的 NinjectConfigurator 中有
container.Bind<IClock>().To<SystemClock>();
我也试过
container.Bind<IClock>().To<SystemClock>().InSingletonScope();
但是我得到这个错误:
Error activating
IClock
using binding fromIClock
toSystemClock
No constructor was available to create an instance of the implementation type.Activation path: 3) Injection of dependency IClock into parameter clock of constructor of type SystemManager 2) Injection of dependency ISystemManager into parameter systemManager of constructor of type AccountController 1) Request for AccountController
Suggestions: 1) Ensure that the implementation type has a public constructor. 2) If you have implemented the Singleton pattern, use a binding with InSingletonScope() instead.
这是我的注入(inject)类,与我项目中所有其他使用 IoC 的工作类相同:
private readonly IDateTime _dateTime;
private readonly IClock _clock;
public SystemManager(IDateTime dateTime, IClock clock)
{
this._dateTime = dateTime;
this._clock = clock;
}
在这方面我找不到任何帮助。非常感谢任何帮助。
最佳答案
我自己已经有一段时间没有使用 NInject 了,但我相信您想使用 ToConstant()
来绑定(bind)到 SystemClock
的实例:
container.Bind<IClock>().ToConstant(SystemClock.Instance);
关于c# - 如何配置 Ninject 以注入(inject) NodaTime IClock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33645321/