Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Web API Message Handlers

Is it possible to control the execution order of custom message handlers?

As an example, I might want a logging handler to execute first, so I always log a request.

Apart from adding the logging handler last, I'm failing to see how to achieve this.

config.MessageHandlers.Add(new CustomHandlerOne()); 
config.MessageHandlers.Add(new CustomHandlerTwo());
config.MessageHandlers.Add(new LoggingHandler());
like image 441
Darren Avatar asked Apr 20 '12 17:04

Darren


Video Answer


1 Answers

The order in which you register the handlers determines when they are called but as Aliostad points out they work in a Russian doll model so the first one in is also called as the last one out and so forth.

The registered handlesr are invoked in a bottom-up fashion in the incoming path and top-down in the outgoing. That is, the last entry is called first for an incoming request message but invoked last for an outgoing response message.

like image 59
Henrik Frystyk Nielsen Avatar answered Sep 30 '22 21:09

Henrik Frystyk Nielsen