4
votes

I am working on a WebApplication which uses MVC5 and WebApi 2 with Owin. I recently updated Microsoft Asp.Net NuGet packages (Microsoft.AspNet.Mvc, etc.) from version 5.2.2 to 5.2.3, and the Owin NuGet packages (Microsoft.Owin, etc.) from 3.0.0 to 3.0.1. I also updated Microsoft.AspNet.Identity.Owin from version 2.1.0 to version 2.2.0

I then updated the corresponding Ninject WebApi packages (Ninject.Web.WebApi, etc.) from 3.2.3 to version 3.2.4 in order to get it to compile, but did not update Ninject.Web.Common.OwinHost, since this was at the latest version (3.2.3).

When I try to run the application, I get the following error:

Error loading Ninject component ICache No such component has been registered in the kernel's component container.

Suggestions:

1) If you have created a custom subclass for KernelBase, ensure that you have properly implemented the AddComponents() method.

2) Ensure that you have not removed the component from the container via a call to RemoveAll().

3) Ensure you have not accidentally created more than one kernel.

The Kernel that I am creating in the OwinStartup class using is being disposed from the Owin.AppBuilderExtensions.CreateOwinContext() method, which is indirectly from OwinBootstrapper.Execute().

This has only started happening since updating the Asp.Net NuGet packages to 5.2.3. Before updating the packages, OwinBootstrapper.Execute() is still called, but does not cause Owin.AppBuilderExtensions.CreateOwinContext() or KernelBase.Dispose() to be called.

I have not changed any of the code in OwinStartup, and my Ninject Kernel is still being created using:

    public virtual void Configuration(IAppBuilder app)
    {
        app.UseNinjectMiddleware(CreateKernel);
        app.CreatePerOwinContext(CreateKernel);
    }

I have tried updating the NuGet packages one at a time, and the specific update that causes the issue is Microsoft.AspNet.Identity.Owin to 2.2.0 Are there any known compatibility issues with Ninject and AspNet.Identity.Owin 2.2.0?

2
I'm seeing the same thing :( - guysherman
I've been having this issue since 2.2.0 came out, 2.2.1 didn't solve it. - Shay
We ran into the same issue. Unfortunately, we were not able to find a solution.The workaround is downgrading Microsoft.AspNet.Identity.Owin package to the version 2.1.0 - Ronnix

2 Answers

0
votes

This from Hao: I think that should be fine, so long as ninject takes care of disposing the per request objects somehow

0
votes

By looking at the previous source code and current source code, it looks like they are expecting a IDisposable object and calls it immediately at the end of its life cycle (aka request).

I also noticed that other CreatePerOwinContext they provide when installing OWIN such as app.CreatePerOwinContext(ApplicationDbContext.Create); was never disposed (in 2.1.0)? This seems like a big memory leak as they instantiate some classes every time there is a request.

To go around the problem when using CreatePerOwinContext with Ninject StandardKernel, I tried with the following code:

app.CreatePerOwinContext(
    (Microsoft.AspNet.Identity.Owin.IdentityFactoryOptions<IKernel> options, IOwinContext context) => kernel, 
    (Microsoft.AspNet.Identity.Owin.IdentityFactoryOptions<IKernel> options, IKernel instance) => {}
);

Basically, I do nothing in the dispose callback. I do not know if this will lead to some memory leak but it definitely makes the app work again.