c# - Integrating Hyprlinkr with Autofac -
i'm trying integrate hyprlinkr in webapi project with autofac.
i've started writing custom ihttpcontrolleractivator get following exception when trying resolve controller:
no scope tag matching 'autofacwebrequest' visible scope in instance requested. indicates component registered per-http request being requested singleinstance() component (or similar scenario.) under web integration request dependencies dependencyresolver.current or ilifetimescopeprovider.requestlifetime, never container itself.
here's how class looks like:
public class autofaccontrolleractivator : ihttpcontrolleractivator { private readonly icontainer _container; public autofaccontrolleractivator(icontainer container) { if (container == null) throw new argumentnullexception("container"); _container = container; } public ihttpcontroller create(httprequestmessage request, httpcontrollerdescriptor controllerdescriptor, type controllertype) { var linker = _container.resolveoptional<routelinker>( new typedparameter(typeof(httprequestmessage), request)); return (ihttpcontroller) _container.resolve(controllertype, new typedparameter(typeof(iresourcelinker), linker)); } } how get lifecycle issue fixed?
you don't need write own ihttpcontrolleractivator because can register routelinker in container.
you need of registerhttprequestmessage method makes httprequestmessage resolvable container.
so routelinker registration this:
builder.registerhttprequestmessage(globalconfiguration.configuration); builder.register(c => new routelinker(c.resolve<httprequestmessage>())) .instanceperapirequest(); now can depend on routelinker in controllers:
public mycontroller : apicontroller { public mycontroller(routelinker routelinker) { //do stuff routelinker } }
Comments
Post a Comment