Unity lazy resolve?

The lifetime of a service should always be equal or shorter than that of its dependencies, so you would typically register ITest as Per Http Request or Transient, but if that's not possible, wrap the dependency ( DelegateHandler I assume) that has a per Http Request lifetime in a proxy.

Up vote 1 down vote favorite share g+ share fb share tw.

I have mvc webApi application which works with Unity. I have to resovle interface ITest to singleton class (DelegateHandler). But interface ITest has per httprequest lifetime manager and it is important.

So I can't resolve ITest on Application_Start event cause there isn't HttpRequest right now but DelegateHandler will use ITest only in httprequest life cycle. So is it possible to send lazy resolve to DelegateHandler or maybe somebody have other interesting solution? .net dependency-injection unity ioc-container lazy link|improve this question edited Apr 5 at 10:35Steven23.5k21551 asked Apr 5 at 9:29Denis Agarev340113 88% accept rate.

The lifetime of a service should always be equal or shorter than that of its dependencies, so you would typically register ITest as Per Http Request or Transient, but if that's not possible, wrap the dependency (DelegateHandler I assume) that has a per Http Request lifetime in a proxy: // Proxy public class DelegateHandlerProxy : IDelegateHandler { public Container Container { get; set; } // IDelegateHandler implementation void IDelegateHandler.Handle() { // Forward to the real thing by resolving it on each call. This.Container.Resolve().Handle(); } } // Registration container. Register(new InjectionFactory( c => new DelegateHandlerProxy { Container = c })).

Thanks, very nice answer! – Denis Agarev Apr 5 at 10:59 Would be cool to convert it generic solution but propably it's impossible) – Denis Agarev Apr 6 at 7:51.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions