c# - Autofac - DelegatingHandler (HttpMessageHandler) Registration -


i have custom delegatinghandler in class library need register autofac. webapi host resolves it's dependencies on runtime, host has no references library.

public class localizationhandler : delegatinghandler {     protected override async task<httpresponsemessage> sendasync( httprequestmessage request, cancellationtoken cancellationtoken )     {} } 

on autofac initializer class i've tried things like:

protected override void load( containerbuilder builder ) {      builder.registertype<localizationhandler>();      builder.register(c => new localizationhandler()); } 

the normal way register such handlers within host be:

public static void register(httpconfiguration httpconfiguration) {             httpconfiguration.maphttpattributeroutes();               httpconfiguration.messagehandlers.add(new localizationhandler()); } 

but don't have access host project here. ideas how inject handler on containerbuilder?

it seems can't inject httpmessagehandler in web api

given message handler passed instance , being initialized once entire service. think easier have user code inject dependency first , register instance http configuration. allow people write custom message handler has constructor not need dependency injection well. current model more flexible.

>> http://aspnetwebstack.codeplex.com/workitem/62

but can create proxy want. example :

public class proxydelegatinghandler : delegatinghandler {     protected override task<httpresponsemessage> sendasync(         httprequestmessage request, cancellationtoken cancellationtoken)     {         ienumerable<delegatinghandler> innerhandlers =              request.getdependencyscope()                    .getservices(typeof(delegatinghandler))                    .oftype<delegatinghandler>();          httpmessagehandler handler = this.innerhandler;         foreach (delegatinghandler innerhandler in innerhandlers)         {             innerhandler.innerhandler = handler;             handler = innerhandler;         }          httpmessageinvoker invoker = new httpmessageinvoker(handler, false);         return invoker.sendasync(request, cancellationtoken);     } } 

and register handlers :

        builder.registertype<x1handler>().as<delegatinghandler>().instanceperrequest();         builder.registertype<x2handler>().as<delegatinghandler>().instanceperrequest();          config.messagehandlers.add(new proxydelegatinghandler()); 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -