One-time Lifetime On Demand with ServiceStack and NinjectContainerAdapter

When using Ninject with a ServiceStack, how would I specify that the lifetime of the object should be per request, by calling any IDisposable.Dispose method as needed?

The default docs use the following:

container.RegisterAutoWiredAs<MyType,IMyType>().ReusedWithin(ReuseScope.Request); 

      

However, I have to use NinjectContainerAdapter

for my project. I used to install Ninject.Web.Common

and write:

kernel.Bind<IMyType>().To<MyType>().InRequestScope()

and my instance will be deleted at the end of the request. How can I do this in a ServiceStack, say in a self-serving scenario?

+3


source to share


1 answer


You will need to implement INinjectHttpApplicationPlugin

for ServiceStack. In the method, object GetRequestScope(IContext context);

you must return some object that has a request lifetime (equivalent HttpContext.Current

))



Perhaps you could add an object to HostContext

and return it as a scope. Your best bet is to have this object implement INotifyWhenDisposed

and place it at the end of the request so that the objects are released immediately after the request, rather than expecting the object to be garbage collected.

+3


source







All Articles