c# - Castle Windsor: How to detect registrations with a longer lifetime than their dependencies? -
i have found situation can in castlewindsor di regarding dependent components registered different lifestyles.
suppose have following class , interface:
public class mything : imything { public mything() { } } public interface imything {} this registered such in relevant windsor installer, includes registration httpcontextbase:
container.register( component .for<imything>() .implementedby<mything>() .lifestyle.singleton); container.register( component .for<httpcontextbase>() .usingfactorymethod(_ => new httpcontextwrapper (httpcontext.current) ) .lifestyleperwebrequest()); all , good. developer decides mything has dependency on in httpcontext, changes mything take in httpcontextbase:
public class mything : imything { private readonly httpcontextbase _httpcontext; public mything(httpcontextbase httpcontext) { _httpcontext = httpcontext; } } this seems quite reasonable on face of - after all, when designing classes you're supposed able without giving thought specifics of dependency injection. however, in reality, have subtle (but potentially serious) bug whereby 1 component depends on component that has shorter lifestyle itself.
now, expecting throw kind of exception in castle windsor, appears not to. happily give mything whatever happened current httpcontext @ time, , continue give same object rest of life of application with same httpcontext.
after googling haven't been able find windsor configuration issue. have advice on this? project have moved onto has absolute dog's dinner of di setup, , finding these kind of issues proving hard (in example i've given should pretty obvious if controller has wrong httpcontext, less in other cases).
castle windsor not throw exceptions displays warning in debugger. (open image in new tab)

here's test can display it:
[testfixture] public class windsortests { [test] public void lifecyclewarningtest() { iwindsorcontainer container = new windsorcontainer(); container.register( component.for<isingleton>().implementedby<singleton>().lifestylesingleton(), component.for<itransient>().implementedby<transient>().lifestyletransient() ); isingleton singleton = container.resolve<isingleton>(); } } interface isingleton { } class singleton : isingleton { private readonly itransient _transient; public singleton(itransient transient) { _transient = transient; } } interface itransient { } class transient : itransient { }
Comments
Post a Comment