c# - 如何配置 Ninject 以注入(inject) NodaTime IClock

标签 c# ninject nodatime

在我的 NinjectConfigurator 中有

container.Bind<IClock>().To<SystemClock>(); 

我也试过

container.Bind<IClock>().To<SystemClock>().InSingletonScope();

但是我得到这个错误:

Error activating IClock using binding from IClock to SystemClock 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/

相关文章:

c# - 如何将实现某个通用接口(interface)的所有类型放入字典中?

c# - 如何在 ASP.NET MVC 中浏览服务器上的文件?

c# - 注入(inject) ActivationException : Error activating IAlertManagement

petapoco - NPoco/PetaPoco 和 Noda 时间类型

c# - .Net 中的工具提示

c# - 正则表达式使用 C# 限制仅第二次出现左括号和右括号

c# - 使用 Ninject 绑定(bind)同一类型的多个版本

asp.net-mvc - 如何使用 Ninject 注入(inject) Identity 类?

c# - 特定时区的当前日期时间(带或不带 NodaTime 的 DST 偏移)

c# - NodaTime 中时区的长格式名称