0

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.

Chris D
  • 134
  • 1
  • 10
  • Where is your configuration class built in your application? Is it being called by the `Application_Start` method? – NightOwl888 Feb 08 '17 at 20:39
  • @NightOwl888 This is a pure OWIN app, so there is no global Application_Start, only the Startup.cs with the `[assembly: OwinStartup(typeof(Startup))]` at the top. The Ninject configuration is built inside a method on Startup, `public virtual IKernel CreateKernel()`. It's pretty straightforward, at the moment, and just returns `new StandardKernel(new ApiModule(whoAmI))`. – Chris D Feb 08 '17 at 21:02

1 Answers1

0

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.

Chris D
  • 134
  • 1
  • 10