👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Extending HTTPClient with Custom Http Message Handlers in dotnet

Extending HTTPClient with Custom Http Message Handlers in dotnet

http client

6 Articles

Improve

In this article, let's learn about how to extend HTTP Client with Custom Message Handlers in .NET.

Note: If you have not done so already, I recommend you read the article on Working with API that supports remote streaming using HTTPClient in dotnet.

Table of Contents

  1. Introduction
  2. Delegating Handler Pattern
  3. Use cases for Custom Http Message Handler
  4. Implementing Custom Http Message Handler
  5. Summary

Introduction

Have you ever wondered on how to add custom logic to your HTTP requests? For example, you want to add a custom header to all your HTTP requests. Or you want to add some custom logic to your HTTP requests. Then you are at the right place.

Custom Message Handlers are very powerful and useful when you want to add some custom logic to your HTTP requests. In this article we will learn how to extend HTTP Client with custom Http Message Handlers and how to chain them to form delegating handler pattern. Let's also look at some examples on where this would be helpful.

Delegating Handler Pattern

The Delegating Handler Pattern involves an orchestrated flow of HTTP requests through a series of message handlers that can be seamlessly linked together, forming what can be likened to a pipeline. It begins with the initial reception of the request by the first handler within this pipeline. This handler may carry out specific operations and then forwards the request to the next handler in line. As this process unfolds, a response is gradually crafted and journeys back up the pipeline in a structured manner.

Delegating Handler Pattern

In essence, this architectural design embodies the essence of a delegating handler. What makes this pattern truly powerful is the ability to engineer our custom handlers and seamlessly inject them into this intricate pipeline. This approach proves invaluable in tackling an array of common challenges that developers often encounter in the realm of HTTP request handling.

Use cases for Custom Http Message Handler

Let's look at some of the use cases for custom Http Message Handler.

  • Attaching Bearer Token in all requests
  • Renewing expired Bearer Token from Identity Provider and use it
  • Sending headers in all requests like API version or custom headers
  • Logging API request and response
  • Determine if request needs to be served from local cache or by communicating with actual API

Implementing Custom Http Message Handler

Before we implement custom Http Message Handler, I would like to highlight that DelegatingHandler is also an HttpMessageHandler except that as part of SendAsync implementation, it just calls the SendAsync of the inner handler. The inner handler will do the same and you get the Chinese boxes or the Russian dolls effect. HttpServer, where the pipeline starts is itself a DelegatingHandler.

Another cool thing is that you inject any Dependency in the constructor of the DelegatingHandler and it will be resolved by the DI container.

Let's implement a custom Http Message Handler that will add a custom header to all the requests.

Let's create a class called CustomHeaderHandler that will add a custom header to all the requests. We will inherit from DelegatingHandler and override the SendAsync method. In the SendAsync method, we will add a custom header to the request and call the base SendAsync method. This will add the custom header to all the requests.

Code Sample - Custom Header HTTP Message Handler

The final step is to register it in the Dependency Injection container and add it to the HttpClient pipeline as shown below.

Code Sample - Registering Custom Http Message Handler

That's it. Now if you check the network tab in the browser, you will see the custom header added to all the requests sent to https://ilovedotnet.org.

X-Client-ID in Request Header

Summary

In this article, we learned about how to extend HTTP Client with Custom Message Handlers in .NET. We also learned about the Delegating Handler Pattern and some of the use cases for custom Http Message Handler. We also learned how to implement custom Http Message Handler and register it in the Dependency Injection container and add it to the HttpClient pipeline. I hope you have enjoyed reading this article and found it useful. Please share it with your network and support me.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • Http Client
  • Delegating Handler
  • Http Message Handler