httprequest - Configuring lifetime scopes in autofac when used as ServiceStack's IoC -



httprequest - Configuring lifetime scopes in autofac when used as ServiceStack's IoC -

i'm using autofac di container our servicestack web services app. i'm able configure wiring , everything, after reading section on scopes, i'm @ loss @ scope best utilize when registering components. in our particular case, think perhttprequest scope ok since (please right me if im wrong) want dispose dependencies request ends.

my question is, how set in container? can't seem find "perhttprequest" lifetime scope within included methods in autofac. i'm unsure if servicestack kind of automagic me behind scenes.

i'm using autofac 3.0.1 on servicestack 3.9.35 on .net 4 (running regular asp host, not mvc). i'm using class described here icontainer adapter.

i think have figured out how create work (using autofac 2.6, stuck on right now.) involves using next adapter , autofac.mvc3 package:

public class autofaciocadapter : icontaineradapter { private readonly icontainer _autofacrootcontainer; private readonly container _funqcontainer; public autofaciocadapter(icontainer autofacrootcontainer, container funqcontainer) { // register requestlifetimescopeprovider (from autofac.integration.mvc) funq var lifetimescopeprovider = new requestlifetimescopeprovider(autofacrootcontainer,null); funqcontainer.register<ilifetimescopeprovider>(x => lifetimescopeprovider); // store autofac application (root) container, , funq container later utilize _autofacrootcontainer = autofacrootcontainer; _funqcontainer = funqcontainer; } public t resolve<t>() { homecoming activescope.resolve<t>(); } public t tryresolve<t>() { t result; if (activescope.tryresolve(out result)) { homecoming result; } homecoming default(t); } private ilifetimescope activescope { { // if there active httpcontext, retrieve lifetime scope resolving // ilifetimescopeprovider funq. otherwise, utilize application (root) container. homecoming httpcontext.current == null ? _autofacrootcontainer : _funqcontainer.resolve<ilifetimescopeprovider>().getlifetimescope(); } } }

steps implement:

add autofac.mvc3 nuget bundle web project (note: not matter project isn't using mvc. solution might different autofac 3, cannot utilize mvc3 integration.) follow servicestack ioc page in hooking custom icontaineradapter autofac, using next implementation

scope httprequest servicestack autofac

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -