Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disposable per-request lifetime with ServiceStack and NinjectContainerAdapter

When using Ninject with ServiceStack, how would I specify that an object's lifetime is to be per-request, calling any IDisposable.Dispose method if necessary?

Per the docs, the default way of doing this is:

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

However, I have to use the NinjectContainerAdapter for my project. Normally I'd install Ninject.Web.Common and write:

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

and my instance would be disposed of at the end of the request. How would I go about doing this in ServiceStack, say in a self-hosted scenario?

like image 620
rossipedia Avatar asked Nov 21 '25 19:11

rossipedia


1 Answers

You will have to implement an INinjectHttpApplicationPlugin for ServiceStack. In the method object GetRequestScope(IContext context); you must return some object that has the lifetime of a request (an equivalent to HttpContext.Current))

Probably you could add an object to HostContext and return that one as scope. Best you make that object implement INotifyWhenDisposed and dispose it at the end of the request so that the objects are released imediately after the request instead of waiting for the object to be garbage collected..

like image 121
Remo Gloor Avatar answered Nov 23 '25 12:11

Remo Gloor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!