HttpMessageHandler vs DelegatingHandler

DelegatingHandler inherits from HttpMessageHandler . But I did not understand the difference, given that you have to implement the same method, SendAsync , for both to work.

What is the difference between these two handlers? When should I use each of them?

+6
source share
2 answers

If you are familiar with ASP.NET, HTTP handlers and modules are a good match. If you implement the HttpMessageHandler , you implement the Send and SendAsync and return a response or promise of a response. This is similar to the Http handler. If you implement DelegatingHandler and add it to the config.MessageHandlers collection, your class works in the pipeline and gets the opportunity to see and respond to requests and responses, as well as the HTTP module. DelegatingHandler also an HttpMessageHandler , except that as part of the SendAsync implementation, it simply calls SendAsync internal handler. The internal handler will do the same, and you will get Chinese boxes or the effect of a Russian doll. HttpServer where the pipeline starts is the DelegatingHandler .

+14
source

The difference is very subtle. @Badri gave you a good quick explanation.

Looking for this poster, you understand what it is. Keep in mind that when you create your own DelegatingHandlers, you will not bother with anything that is not specific HTTP material. This is not the place to play with BODY in the case of POST. eg.

One useful thing you can do is to detect very early in the pipe so that the token is not present in the headers, then you can end the request right then and there and create a StatusCode.Forbidden response. Of course, he does not need a simple website. Just a bust. But if you receive millions of calls per minute, this is very convenient, as it happens right before the controller is actually created.

There are only a few cases when you really need it. Or say that the client making the rest of the calls can only do GET and POST, but in the headers it indicates X-Method-Override = PUT, then you can change the request method from POST to PUT at that moment so that your controller / action dispatchers create the correct instance and invoke the correct action.

Here is an interesting poster. PRINT IT: D

http://www.asp.net/media/4071077/aspnet-web-api-poster.pdf

0
source

Source: https://habr.com/ru/post/953350/