We have a very basic Owin application, where we want to use Ninject to resolve our dependencies. I would like to point out that there is no Web API or MVC or other middleware involved.
We set up a kernel like this:
private static StandardKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Test>().ToSelf().InRequestScope();
kernel.Load(Assembly.GetExecutingAssembly());
return kernel;
}
The class Test is simplified for better explaination of the problem.
We tried to set up Ninject and register the Kernel as followoing:
app.UseNinjectMiddleware(CreateKernel);
I really don't know if this is the way to go, but can't find anything similar in the documentation etc.
What we want is to resolve a single instance of Test, within the scope of the Owin request. So let's say we have the following middleware:
app.Use((context,next) =>
{
// How to access my Ninject Kernel or our test instance here?
return next.Invoke();
});
app.Use((context,next) =>
{
// How to access the same Ninject Kernel or our Test instance here?
return next.Invoke();
});
Does anybody know how to accomplish this???
Ok so i figured it out...
Everything is working as expected when i add the Ninject.Web and Ninject.Web.Common NuGet packages to my project.
Now ik can use .InRequestScope(), and all my dependencies are created once (and shared) per request. Also all implementations of IDisposable are correctly disposed. It works just as it does when using WebAPI or MVC.