4

I stumbled across the MessageProcessingHandler class in WebAPI. It's a child of DelegatingHandler. There's almost no information out there on the class and MS documentation is particularly thin, simply saying:

The MessageProcessingHandler is useful if the handler doesn't require asynchronous operations, because operations on request and response messages are fast. The most common usage is to derive from this class and override the ProcessRequest and ProcessResponse methods.

So I thought I'd take the class out for a spin, see how it feels. I'm stumped on one thing... how can I end the processing pipeline and send a ResponseMessage back?

For example: say I have a handler that checks for authentication, the check fails, and I want to return an HTTP 401/403 (I'm not saying which so I can avoid a holy war :-). It doesn't seem possible with MessageProcessingHandler. Right?

Community
  • 1
  • 1
EBarr
  • 11,826
  • 7
  • 63
  • 85

1 Answers1

0

As we can see the MessageProcessingHandler inherit from DelegatingHandler, and i found:

Firstly, we can't implement SendAsync method in implementation class of HttpMessageHandler, because it's sealed. But as we can see from MS docs, If we implement a DelegatingHandler, we can skip the inner handler and directly create the response in SendAsync override method.

Secondly, ProcessRequest method takes an HttpRequestMessage as input and returns an HttpRequestMessage, it won't return HttpResponseMessage. ProcessResponse do return reponse, but it will be excute after action finish executing.

Finally, MessageProcessingHandler class comments say:

A base type for handlers which only do some small processing of request and/or response messages.

So the answer is obvious, ProcessRequest don't send a ResponseMessage back, but only do some small processing. btw, some people also log here and return original request.

NicoXiang
  • 429
  • 9
  • 23