Ninject Owin Request Scope Silently Fails

Problem. The core Ninject always returns new instances, even if they are bound to the request.

-I am using OWIN with the SystemWeb host package so that I can use IIS.

- The project depends on the API application in which all the necessary Ninject / owin libraries are installed.

app.UseNinjectMiddleware(CreateKernel).UseNinjectWebApi(config);

      

-Everything worked as intended before converting to OWIN.

-When only one kernel instance is created.

- Registered some global filters that set specific properties on a query scoped object.

        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            var accountService = Startup.Kernel.Get<IAccountService>();
            // some changes to accountService etc.

      

-When other filters in the pipeline try to get an instance IAccountService

, they always return a new instance.

Based on my own research efforts this appears to be a known issue. Any ideas would be great. Thank.

+3


source to share


1 answer


I have seen a similar problem and this may be your case.

InRequestScope

doesn't work if:

Web API with OWIN in a separate assembly . This assembly has Ninject and Web API, and OWIN installed it.

A blank ASP.Net web project that simply references a previous assembly . The ASP.Net Web Project does not have a Web API or Ninject, it just has a package to allow OWIN to be hosted on top of ASP.Net.



Ninject works fine in this setup; except that InRequestScope is ignored and always allows different object instances.

The problem is that VS does not copy all the required assemblies to the results directory because they do not directly reference the application and are not used by your OWIN assembly, but are simply dynamically loaded by Ninject.

To solve the problem, just NuGet Ninject.Extensions.ContextPreservation in ASP.Net web project.

Perhaps this is just your problem or it looks like it. Take a look at Ninject Extensions and try to determine which extensions you need rather than being added to the results directory.

+2


source







All Articles