【问题标题】:Injecting a class that is in other assembly using ninject使用ninject注入其他程序集中的类
【发布时间】:2011-12-14 00:46:41
【问题描述】:

我的项目是使用可移植区域完成的,我使用 ninject 进行 DI,我正在注入一个在其他程序集中的类,所以我在 areaRegistraction 中有这段代码:

DependencyResolver.Current.GetService<IModuleManager>().Add(this.module);  
IKernel kernel = DependencyResolver.Current.GetService<IKernel>();  
kernel.Bind<IConfigurationRepository>().To<ConfigurationRepository>();  

在我的构造函数中,我有这个代码:

public RequestController(IconfigurationRepository configurationRepository)
{
    this.configurationRepository= configurationRepository;
}

但由于某种原因 configurationRepository 为空

但如果我说:

public RequestController()
{
   this.configurationRepository = ((StandardKernel)DependencyResolver.Current.GetService<IKernel>()).GetAll<IConfigurationRepository>().First();
}

它工作正常。他们之间有什么不同?

任何线索将不胜感激。

【问题讨论】:

  • 它到底是怎么不工作的?它会给你两周的通知还是什么?
  • 我更新了我的问题。问题是 configurationRepository 为空

标签: c# ninject portable-areas


【解决方案1】:

如果注册了多个IConfigurationRepository,则第二个实现将起作用,但在这种情况下第一个将失败。

第一种情况有什么例外?如果在第二种情况下使用Single 而不是First 会发生什么?

【讨论】:

  • 在第一种情况下 configurationRepository 为空。如果我使用单
  • 如果我在第二个中使用 single 而不是 First,请给它例外:Sequence contains more element.
  • 你有问题。 Ninject 不允许有多个绑定,因为使用哪一个会不明确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多