Exploring Delegating Handlers in C# .NET

In modern software development, especially when dealing with web services, the ability to customize and extend HTTP request and response processing is crucial. In C# .NET, delegating handlers provide a robust mechanism to build such custom HTTP pipelines. This article delves into the concept of delegating handlers, illustrating their use and benefits with practical examples.

Understanding Delegating Handlers

Delegating handlers in C# .NET are middleware components designed to intercept and process HTTP requests and responses. They are part of the HttpClient and HttpMessageHandler architecture. By deriving from the DelegatingHandler class, developers can inject custom logic into the HTTP request/response lifecycle, enabling pre-processing and post-processing capabilities.

Creating a Custom Delegating Handler

To create a custom delegating handler, you inherit from the DelegatingHandler class and override the SendAsync method. This method is asynchronous and allows you to implement custom logic that executes before and after the HTTP request is processed.

Here’s a basic example of a custom delegating handler that adds a custom header to each outgoing request:

using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

public class CustomHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        // Pre-processing logic: Add a custom header
        request.Headers.Add("X-Custom-Header", "MyCustomValue");

        // Call the inner handler to send the request
        HttpResponseMessage response = await base.SendAsync(request, cancellationToken);

        // Post-processing logic: Handle the response
        if (response.IsSuccessStatusCode)
        {
            // Additional processing for successful responses
        }

        return response;
    }
}

Integrating a Custom Handler with HttpClient

Once you have your custom handler, the next step is to integrate it with HttpClient. You achieve this by creating an instance of HttpClient and configuring it to use your custom handler.

var customHandler = new CustomHandler()
{
    InnerHandler = new HttpClientHandler() // The actual handler that sends the request
};

var httpClient = new HttpClient(customHandler);

// Use httpClient to send requests
var response = await httpClient.GetAsync("https://example.com");

Chaining Multiple Handlers

A powerful feature of delegating handlers is their ability to be chained together, forming a processing pipeline. This is useful for implementing layered processing logic such as logging, authentication, and retry policies.

var handler1 = new Handler1
{
    InnerHandler = new Handler2
    {
        InnerHandler = new HttpClientHandler()
    }
};

var httpClient = new HttpClient(handler1);

// Use httpClient to send requests
var response = await httpClient.GetAsync("https://example.com");

Practical Use Cases

Delegating handlers can be employed for a variety of purposes in a real-world application:

  • Logging: Capturing request and response details for monitoring and debugging.
  • Authentication: Automatically attaching authentication tokens and handling token refresh.
  • Retry Logic: Implementing policies to retry requests in case of transient errors.
  • Compression/Decompression: Managing the compression of request bodies and decompression of response bodies.
  • Custom Headers: Adding or modifying HTTP headers based on specific requirements.

Example. Logging Handler

Here’s an example of a logging handler that logs the details of each request and response:

using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

public class LoggingHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        // Log request details
        Console.WriteLine($"Request: {request}");

        HttpResponseMessage response = await base.SendAsync(request, cancellationToken);

        // Log response details
        Console.WriteLine($"Response: {response}");

        return response;
    }
}

Conclusion

Delegating handlers are a powerful feature in C# .NET, offering developers the flexibility to build sophisticated and reusable HTTP processing pipelines. By creating custom handlers and chaining them together, you can enhance the functionality of HttpClient to meet the specific needs of your application. Whether it’s adding custom headers, implementing logging, or handling authentication, delegating handlers provide a structured way to extend and customize HTTP request and response processing.

By leveraging these techniques, you can ensure that your application’s HTTP interactions are robust, maintainable, and tailored to your unique requirements.


Similar Articles