Message Handlers | C#

[ Hide Show ]

By definition, a message handler is a class that receives a Web request and returns a Web response. In other words, a message handler is used to process a Web service request during input and/or to process the response during output.

In this chapter, we consider several popular cases of message handlers usage:

  • message handler for a Web request execution time logging,

  • message handler for a custom scheme implementation,

  • message handler for working with a ZIP archive,

  • message handler for network operation timeouts,

  • message handler for a Web request with password-based authentication schemes.

The following topics are available:

Creating a Custom Message Handler – You will get the idea behind the custom message handler and find information on how to implement a custom message handler using Aspose.HTML for .NET.

Popular Use Cases for Message Handlers – You will learn how to create message handlers to implement a custom schema, work with a ZIP archive, and log a Web request execution time.

What are Message Handlers?

Message handlers are a sequence of classes that are part of a process of a Web request and response through a pipeline. The key concept of message handlers work is chaining them together. The first handler receives a Web request, does some processing, and passes the request on to the next handler. At some point, the response is created and returned along the chain. This pattern is called a delegating handler.

Looking at the Figure, the sequence begins with the Request, then continues through the chain of message handlers. Once the message reaches the server, the Response is sent back through the chain of handlers in reverse order.

Text “Pipeline of message handlers”

The request message goes through each handler in turn, reaching the last handler. This handler is also the first to receive the response. At this point, this response goes back through the chain of handlers in reverse order, and each handler can check or use the response as needed.

Handlers behavior is governed by a set of protocols that describe what action message handlers can take in a given situation. The handlers, their protocols and their place in the chain may be defined by a system or user. For example, a message handler might: change the processing sequence of a pipeline, check requests or responses for errors, log the duration of an operation or other diagnostic information, etc.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.