WCF, Ninject, and life cycle
I'm adding Ninject to my WCF service (hosted in IIS). I intend for the WCF service to automatically instantiate instances of my NHibernate repository classes.
I originally used Castle (and later Ninject) to instantiate those objects in my ASP.NET MVC client and pass them to a service class. In this case, the scope was PerWebRequest (Castle) or InRequestScope (Ninject). The NHibernate session would be implicitly created along with a transaction that would be committed when the repositories were disposed.
I would like to keep this same approach by having the client implicitly control the lifetime of the session/transaction by newing/closing the service.
Would one of the existing Ninject scope methods work in this manner (InThreadScope or InRequestScope perhaps)? Is there another开发者_运维问答 way to achieve this goal?
I would let the client explicitly control the lifetime of those kind of objects. Don't inject such an object, but inject a factory that can create such an object for you. This follows the principle of least surprise. See this SO answer for more information.
精彩评论