Skip to content
Related Articles

Related Articles

What is Message Handler ?

Improve Article
Save Article
  • Last Updated : 14 Mar, 2022
Improve Article
Save Article

Message Handler basically derives from the abstract class i.e. HttpMessageHandler class and It is also the base class of the Message Handler. It can be defined as the class that receives an HTTP request and returns the HTTP response. Let’s take an example – Suppose An HTTP Server receives an HTTP Request and now this request will be transferred to Handler A then after processing it in Handler A, It goes to Handler B and the process continues. Thus it performs the specific given tasks within each handler as per the needs. However, Message Handler is generally a chain of classes that are chained together in a series way like –

The First Message Handler receives an HTTP request and then performs some processing. After that give the request to the next message handler. After some time, At some particular point, the response is created and goes back up to the chain. All these handling patterns are known as “Delegating Handler”.

Why use Message Handler?

Message Handler is useful in various ways like:

  • It helps us to perform the operation efficiently in the HTTP request and response.
  • It is also available on the client-side.
  • Depending upon the needs, It helps to perform the specific tasks within each handler.
  • It provides the facility to examine the contents of an XML/JSON request and response.
  • It also enables us to change the content of an XML/JSON request or response whenever find essential.

Following are some of the types of Message Handlers. They are as follows:

  1. Server-Side Message Handlers: Server-Side Message Handlers are the one where the Web API pipelines use some of the built-in Message Handlers, which has been set up by the MVC Web API framework. And those are- HttpServer, HttpRoutingDispatcher, HttpControlDispatcher. So Server-Side Message Handlers are basically pre-defined that basically provides read HTTP Request and Response in the server-side.
  2. Custom Message Handlers: Custom Message Handlers basically derived from the System.Net.Http.DelegatingHandler and it overrides the SendAsync method. Here The method takes input as an HttpRequestMessage and then asynchronously returns an HttpResponseMessage.
  3. Per-Route Message Handlers: Using The Per-Route Message Handlers, if you want to enable some basic authentication for some of the routes only then it’s just a matter of configuring this per-route message handler in this route itself and you are all set to go.

There are several programs that are being used as message handlers and are invoked with the same interface with a channel that holds a number of containers. Containers are further classified into the following types:

1. Control Containers: Control Containers are essential only for pipeline operation. It basically performs the task to modify the sequence in which message handlers are processed.

2. Context Containers: Context Containers are used in some situations where message handler programs need information about the context about how they are invoked. We can also change the information in our message handler that context containers hold.

3. User Containers; User Containers basically holds the information that one message handler needs to pass to another message handler.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!