asp.net web api - Using Autofac's resolver on demand in WebAPI perRequest to handle Circular errors -
asp.net web api - Using Autofac's resolver on demand in WebAPI perRequest to handle Circular errors -
i utilize autofac on basic level handle dependency injection. config @ moment simple:
builder.registertype<testdbcontext>().as<dbcontext, testdbcontext>().instanceperrequest(); builder.registertype<userrepository>().as<iuserrepository>().instanceperrequest(); builder.registertype<testrepository>().as<itestrepository>().instanceperrequest();
my problem kind of related to: 'autofac circular component dependency detected' error
i don't want curcular component dependency error don't include iuserrepository
itestrepository
constructor (it's included other way round).
i wanted utilize sec suggestion above question's answer. how can code testrepository
utilize userrepository
on-demand? i've tried using beginlifetimescope next attempt:
public class testrepository : itestrepository { public testrepository() { } public void test() { using (var scope = autofacconfig.container.beginlifetimescope()) { var scopeuserrepo = scope.resolve<iuserrepository>(); } } }
but next exception:
no scope tag matching 'autofacwebrequest' visible scope in instance requested. indicates component registered per-http request beingness requested singleinstance() component (or similar scenario.) under web integration request dependencies dependencyresolver.current or ilifetimescopeprovider.requestlifetime, never container itself.
you can't begin lifetime scope in web api. request lifetime scope carted around request message. manually creating request lifetime scope this, you're not going results looking - especially if have per-request lifetime scope dependencies, because scope you're creating not same scope actual request lifetime. won't lifetime sharing should getting.
there detailed faq troubleshooting per-request lifetime scope issues should help on way. may interested in the documentation on handling circular dependencies in autofac.
asp.net-web-api autofac
Comments
Post a Comment