I’m creating an application that has a ASP.NET Web API interface using the Self Hosting approach. I want to use a scope similar to InRequestScope() provided by MVC3. When I host a Web API application on the IIS this seems to be supported by Ninject.Extension.WebAPI. But when self hosting the WebAPI I get a new instance when I create bindings InRequestScope(). Is there any way I can use this scope when I self host the web API?
I’m creating an application that has a ASP.NET Web API interface using the Self
Share
You can use the NamedScope extensions to define that the controller defines a scope and use that scope for everything that is in a request scope. Best you use conventions for this definition:
I recommend to implement
INotifyWhenDisposedfor the controllers so that the objects in request scope are immediately released after the request. E.g. by deriving from the following class instead ofApiControllerI try to provide an extension for WebAPI selfhosting somewhen in the comming weeks.
EDIT:
Selfhosting support is now provided by Ninject.Web.WebApi.Selfhosting
https://nuget.org/packages/Ninject.Web.WebApi.Selfhost/3.0.2-unstable-0
Example: https://github.com/ninject/Ninject.Web.WebApi/tree/master/src/Ninject.Web.WebApi.Selfhost