在应用程序池重新启动后,OWIN Ninject中间件不可用(OWIN Ninject middleware unavailable after app pool restart)

编程入门 行业动态 更新时间:2024-10-23 04:47:40
在应用程序池重新启动后,OWIN Ninject中间件不可用(OWIN Ninject middleware unavailable after app pool restart)

我有一个以下启动的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.

更多推荐

本文发布于:2023-07-06 07:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047590.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重新启动   不可用   应用程序   中间件   OWIN

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!