asp.net web api - Adding message handler on the fly -


i have 4 message handlers - myhandler, myhandler1, myhandler2 , myotherhandler. have myhandler , myotherhandler added handlers collection, not myhandler1 or myhandler2.

config.messagehandlers.add(new myhandler()); config.messagehandlers.add(new myotherhandler()); 

i want myhandler1 or myhandler2 added pipeline on fly myhandler, depending on condition. know myhandler1 , 2 can added config.messagehandlers collection , nothing when turn comes, when 'some' condition not apply not want. let's have 100 such handlers , don't want of them run in pipeline when myhandler thinks appropriate.

i cannot manually insert myhandler1 pipeline setting myhandler.innerhandler. chain cached globally requests , cannot modify related specific request. here did.

i created base handler increases visibility of sendasync.

public abstract class mybasehandler : delegatinghandler {      public task<httpresponsemessage> wrappersendasync(                                     httprequestmessage request,                                         cancellationtoken cancellationtoken)     {         return this.sendasync(request, cancellationtoken);     } } 

i derived myhandler1 , 2 base.

public class myhandler1 : mybasehandler {     protected async override task<httpresponsemessage> sendasync(                                     httprequestmessage request,                                         cancellationtoken cancellationtoken)     {         // use request          var response = await base.sendasync(request, cancellationtoken);          // use response          return response;      } } 

now, myhandler can instantiate myhandler1 or myhandler2 based on condition , sets innerhandler own innerhandler , call , return sendasync through wrapper.

public class myhandler : delegatinghandler {     protected async override task<httpresponsemessage> sendasync(                                     httprequestmessage request,                                         cancellationtoken cancellationtoken)     {         // check condition , choose myhandler1 or myhandler2 or return         // await base.sendasync(request, cancellationtoken);         var h = new myhandler1();         h.innerhandler = this.innerhandler;         return await h.wrappersendasync(request, cancellationtoken);          // when myhandler1 , myhandler2 no good, want         // nothing , let other handlers job         // return await base.sendasync(request, cancellationtoken);      } } 

it work i'm not sure if i'm breaking doing this. overlooking taking approach?

your approach looks good. i'm pretty sure should work fine.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Socket.connect doesn't throw exception in Android -

SPSS keyboard combination alters encoding -