c# - Autofac and ASP .NET MVC bad lifetime scope in controller -
i have external controller (for e.g. extcontroller ) in assembly ( folder config/extensions ).
registration:
builder.registercontrollers(assembly).named<icontroller>(t => t.name.replace("controller", string.empty) );
getting controller ( have own controller factory ):
public override icontroller createcontroller (system.web.routing.requestcontext requestcontext, string controllername) { try { var ctrl = _base.createcontroller(requestcontext, controllername); return ctrl; } catch (httpexception htte) { object ic = null; if (_container.tryresolvenamed(controllername, typeof(icontroller), out ic)) { return ic icontroller; } else throw htte; } }
and if doing request controller "root" autofac lifetime scope. in other controllers got "autofacwebrequest" scope.
could me ? maybe way controller creation assembly ?
edit
i resolved problem think not best way can it. changed from:
if (_container.tryresolvenamed(controllername, typeof(icontroller), out ic))
to:
if ( (dependencyresolver.current autofac.integration.mvc.autofacdependencyresolver).requestlifetimescope.tryresolvenamed(controllername, typeof(icontroller), out ic))
unfortunately, if integration point custom controller factory working against named services, stuck have. can use autofacdependencyresolver.current
rather casting dependencyresolver.current
yourself, principle still same.
however, did notice didn't show base controllerfactory is. appears have sort of decorator pattern going _base
being called rather base
(without underscore). defaultcontrollerfactory
runs controller instantiation through dependency resolver. of course, mean need not register controllers named services , instead register them standard registercontrollers
method.
again, if have register them named (for whatever reason) and/or if base call createcontroller
isn't going through standard defaultcontrollerfactory
, have correct. if can stop registering controllers named services, though, standard dependency resolution pipeline "just work" , won't need code.
there detailed documentation on autofac mvc integration on autofac doc site.
Comments
Post a Comment