如何使用Ninject获取单个实例?这是我的服务模块:
public class ServicesModule : NinjectModule { public override void Load() { Kernel.Bind().To ().InRequestScope(); Kernel.Bind ().To ().InRequestScope(); Kernel.Bind ().To ().InRequestScope(); } }
我需要一个实例IReportGenerationProcessor
来触发我从Azure服务总线队列收到的消息.
我见过很多不同的方法,但没有一个对我有用.我不断得到错误:Object instance not set to an instance of an object
.
//I do instantiate this class using new WebJobBase(); public class WebJobBase { public void ProcessMessage(BrokeredMessage message) { // Just need an instance of IReportGenerationProcessor here var _processor = new ReportGenerationProcessor(); _processor.ProcessMessage(message); } }
这是我的IReportGenerationProcessor
实现:
public interface IReportGenerationProcessor { DocumentMetaData ProcessMessage(BrokeredMessage message); } public class ReportGenerationProcessor : IReportGenerationProcessor { // Go figure these don't work either [Inject] public IConfigurationService _config { get; set; } [Inject] public IDocumentServiceApi _docService { get; set; } public DocumentMetaData ProcessMessage(BrokeredMessage message) { var report = message.GetBody(); //Do some stuff return new DocumentMetaData(); // just a place holder } }
如果我能提供更多信息,请告诉我.我对Ninject知之甚少.
好的,所以文档类型暗示了这一点,但他们没有使用接口并注册.ToSelf()
.
基本上我需要StandardKernel
用my ServicesModule
从上面实例化一个新的,并.Get<>();
为我注册的接口调用方法.
IKernel kernel = new StandardKernel(new ServicesModule()); var processor = kernel.Get(); processor.ProcessMessage(message);