Creating a Custom Message Handler – Basic Information
In traditional client-server calls, the client sends a Web request to the server, waits for the result, then gets a response. There are message handlers both server-side and client-side, and sometimes you have to configure custom message handlers. In this article, we consider the concept of a message handler and create a custom message handler to do a specific task.
What is a Message Handler?
By definition, a message handler is a class that receives a Web request and returns a 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. The chain of handlers is a pipeline, which will all have the chance to process an outgoing Web request before it is sent or process a Web response.
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 by a user.
Custom Message Handler
People like customization, and sometimes they need to perform a custom logic in a request and response. You can add custom handlers to the pipeline. Each handler has a choice of actions that it can achieve. For example, the handler can be invoked only for request processing or only for response processing.
One or more custom message handlers can be created and organized in a chain of message handlers that get called with every request and response. Each Web request goes through a message handler pipeline, and we can create custom message handlers and extend the behavior of an existing pipeline. Graphically, the result looks something like this:
A message handler can return a response immediately. For example, you could create a handler that checks if a header is present on outgoing requests. If the header is missing, it doesn’t pass the request along to the next handler and generates an error response which it returns to the caller.
You can define the handler chain as a pipeline that will be able to process the outgoing Web request before sending it. For example, these handlers may check the request’s body or log some information about the request. Consider an example of a message handler creating to show the key concepts.
Create a Custom Message Handler
Aspose.HTML for .NET offers functionality for custom message handlers creating. Let’s develop a simple custom handler that prints a message when a request starts and finishes. Take the following steps:
Use the necessary Namespace, which is the Aspose.Html.Net. This Namespace is presented by classes and interfaces which are responsible for helping easy network processing.
To create a custom Message Handler, you need to define your own class that will be derived from the MessageHandler class. The MessageHandler class represents a base type for message handlers.
1using Aspose.Html.Net;
2...
3
4 public class LogMessageHandler : MessageHandler
5 {
6
7 }
So, you have defined your own LogMessageHandler class, now you need to do some operation within it.
- Override the Invoke() method of the MessageHandler class to implement the custom message handler behaviour.
1// This message handler prints a message about start and finish processing request
2public class LogMessageHandler : MessageHandler
3{
4 // Override the Invoke() method
5 public override void Invoke(INetworkOperationContext context)
6 {
7 Debug.WriteLine("Start processing request: " + context.Request.RequestUri);
8
9 // Invoke the next message handler in the chain
10 Next(context);
11
12 Debug.WriteLine("Finish processing request: " + context.Request.RequestUri);
13 }
14}
- Add the LogMessageHandler to the pipeline of existing message handlers. Use Insert() method to append LogMessageHandler at the first place in the collection of message handlers.
The following code snippet demonstrates how to add the LogMessageHandler to the pipeline and use it for a simple operation.
1// Create an instance of the Configuration class
2using var configuration = new Configuration();
3
4// Add the LogMessageHandler to the chain of existing message handlers
5var service = configuration.GetService<INetworkService>();
6var handlers = service.MessageHandlers;
7
8handlers.Insert(0, new LogMessageHandler());
9
10// Prepare path to a source document file
11string documentPath = Path.Combine(DataDir, "input.htm");
12
13// Initialize an HTML document with specified configuration
14using var document = new HTMLDocument(documentPath, configuration);
So, in this article, you have got the idea behind the custom Message Handler and have seen the simple example of its implementation.
In the Popular Use Cases for Message Handlers article, 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.