Delegating Handlers in C#

builder.Services.AddHttpClient<WeatherService>();
Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
public class LoggingHandler : DelegatingHandler
{
private readonly ILogger<LoggingHandler> _logger;

public LoggingHandler(ILogger<LoggingHandler> logger)
{
_logger = logger;
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
_logger.LogInformation("Right before making http request");

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

_logger.LogInformation("Response has been received");

return response;
}
}
builder.Services.AddHttpClient<WeatherService>()
.AddHttpMessageHandler<LoggingHandler>();

--

--

--

Software Engineer. C# .NET

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A digital product is a way to teach something you know. And you know something.

What To Test In A Mobile App?

CS 373 2/28 Blog

Linked List in Python

Making Contributions To A New Code Base

Email Is Out, Social Is In!

How to Make Things Better: A Case Study

Google App Engine — Configure custom domains with SSL

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vijesh Salian

Vijesh Salian

Software Engineer. C# .NET

More from Medium

C# Parallel Programming: Working with Concurrent Collections — Part V

Garbage Collector

C# .NET — Garbage Collector

.NET Global CLI’s

Evaluation of switch in C#