我有一个以下启动的OWIN应用程序:
public virtual void Configuration(IAppBuilder app) { var config = new HttpConfiguration(); config.Filters.Add(new UserNotifyExceptionFilter()); ConfigureAuth(app); app.UseNinjectMiddleware(CreateKernel); app.UseNinjectWebApi(config); WebApiConfig.Register(config); ConfigureLogging(app); config.EnableCors(); ConfigErrorHandling(config); HelpPages(app, config); }它似乎正确地加载ninject,并且该应用程序在IIS 8下运行良好数小时或数天。然后,神秘地,它停止工作,并且所有控制器上的[Inject] ed依赖项都变为空。 在启动类的初始加载之后,我的代码中没有任何内容会干扰ninject的配置。
我对这个难以置信,也没有其他人似乎在SO上提出过这个问题。
I have an OWIN app with the following startup:
public virtual void Configuration(IAppBuilder app) { var config = new HttpConfiguration(); config.Filters.Add(new UserNotifyExceptionFilter()); ConfigureAuth(app); app.UseNinjectMiddleware(CreateKernel); app.UseNinjectWebApi(config); WebApiConfig.Register(config); ConfigureLogging(app); config.EnableCors(); ConfigErrorHandling(config); HelpPages(app, config); }It appears to load ninject, correctly, and the app runs fine for hours or days under IIS 8. Then, mysteriously, it stops working, and all [Inject]ed dependencies become null on all controllers. Nothing in my code meddles with ninject's configuration after the initial load in the startup class.
I am stumped on this one, and no one else seems to be asking about it on SO.
最满意答案
我回顾了我的旧问题,并认为我应该发布我最终找到的答案。 对于初学者来说,Ninject中间件已经打破了一段时间 。 我们最终交换了以下内容
config.Services.Replace(typeof(IHttpControllerActivator), new ServiceActivator(config, diKernel));将ServiceActivator定义为:
public class ServiceActivator : IHttpControllerActivator { private IKernel kernel; public ServiceActivator(HttpConfiguration configuration, IKernel kernel) { this.kernel = kernel; } public IHttpController Create( HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) { var controller = kernel.Get(controllerType) as IHttpController; return controller; } }注意:从长远来看,我们现在已经切换到AutoFac。 Ninject让你的编码更快,但AutoFac声称松耦合。 茶比咖啡好吗? 我们将看到。
I was looking back through my old questions, and thought I should post the answer I eventually found for this. For starters, the Ninject middleware has been broken for some time. We ended up swapping that for the following
config.Services.Replace(typeof(IHttpControllerActivator), new ServiceActivator(config, diKernel));With ServiceActivator defined as:
public class ServiceActivator : IHttpControllerActivator { private IKernel kernel; public ServiceActivator(HttpConfiguration configuration, IKernel kernel) { this.kernel = kernel; } public IHttpController Create( HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) { var controller = kernel.Get(controllerType) as IHttpController; return controller; } }Side note: in the longer run, we've switched to AutoFac, for now. Ninject gets you coding faster, but AutoFac claims looser coupling. Is tea better than coffee? We shall see.
更多推荐
发布评论