DEV Community

Kiet Ly
Kiet Ly

Posted on

1

Centralize Request Configuration in .NET

These days in the .NET world, it's common to work with external services that require authentication or specific header configurations to function properly. Trust me—you don’t want to repeat the same setup logic everywhere you use HttpClient.

That’s where the DelegatingHandler class from the .NET library becomes incredibly useful.

A DelegatingHandler lets you intercept and modify HTTP requests before they are sent to the external service. This gives you the flexibility to apply required configurations like setting headers.

Just follow these 3 simple steps:

  1. Create a custom handler
    // Create a handler derived from DelegatingHandler
    public class AuthHandler : DelegatingHandler
    {
        // Override SendAsync
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            // Configure required header before actual request
            string token = await GetAccessTokenAsync();
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
            request.Headers.Add("Type", "INTERNAL");

            return await base.SendAsync(request, cancellationToken);
        }
    }
Enter fullscreen mode Exit fullscreen mode
  1. Register it in the dependency injection (DI) container
builder.Services.AddTransient<AuthHandler>();
builder.Services.AddHttpClient("VendorApi", client =>
{
    client.BaseAddress = new Uri("https://vendor.com");
})
.AddHttpMessageHandler<AuthHandler>();
Enter fullscreen mode Exit fullscreen mode
  1. Inject and use HttpClient throughout your code—without worrying about repeating header logic
app.MapGet("posts", async (IHttpClientFactory httpClientFactory) =>
{
    HttpClient httpClient = httpClientFactory.CreateClient("VendorApi");

    HttpResponseMessage response = await httpClient.GetAsync("posts");
    response.EnsureSuccessStatusCode();

    return await response.Content.ReadAsStringAsync();
})
.WithName("posts")
.WithOpenApi();
Enter fullscreen mode Exit fullscreen mode

Learn more about DelegatingHandler and IHttpClientFactory here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-9.0

Tiugo image

Fast, Lean, and Fully Extensible

CKEditor 5 is built for developers who value flexibility and speed. Pick the features that matter, drop the ones that don’t and enjoy a high-performance WYSIWYG that fits into your workflow

Start now

Top comments (0)

👋 Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someone’s day—leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay