Middleware Pattern For HttpClient With Delegating Handlers

preview_player
Показать описание

HttpClient allows you to send HTTP requests to external services. But do you know how to add middleware to an outgoing request? Middleware allows you to add behavior before or after the request. And you can also do that with HttpClient requests using custom message handlers. Find out how in this video.

Join my weekly .NET newsletter:

Read my Blog here:

Subscribe for more:

Chapters
0:00 Taking a look at the typed HttpClinet
1:40 Adding authentication headers using DelegatingHandler
6:52 Creating logging middleware with a DelegatingHandler
10:27 Resilient DelegatingHandler with retries using Polly
Рекомендации по теме
Комментарии
Автор

Excellent timing - I just completed the Authentication section of your Pragmatic Clean Architecture course and wanted to learn more about this point specifically. Thanks!

ThrottleScotty
Автор

If you liked this, join *The .NET Weekly* - my newsletter with 27k+ engineers - that teaches you how to improve at .NET & software architecture.

MilanJovanovicTech
Автор

Thank so much Milan. I understand better this concept with your explanation.

kodindoyannick
Автор

Clean thumbnail, really good content.

nove
Автор

Very nice video on an interesting topic. Ideas for continuing on this: write unit tests for this, write a handler that manages redirects (I think that would need to be the lowest handler, and be a different type than DelegatingHandler)

johnnyirish
Автор

this is brilliant. I was wondering if you could help me explain when we should use transient, scope, and singleton in DI for the practical case?

quocboy
Автор

Let's say there are 3 external APIs meaning their base address, authentication process would be different and only one of them would be called based on user input. In that case how would you register them in the startup with message handlers without duplicating code? Thank you.

microtech
Автор

I have a problem with this approach, i am logging the request and response that go trough httpClient. I need a scoped Service that logs the data. The problem is that the Scoped service contains properties that are same for the httpRequest comming trough api, controllers, services and httpClients that are called within. The problem is that the scoped logging service is somehow out of scope in the DelegateHandler, i tried HttpContextAccessor, it works when the requests come from API, but the problem comes up when the httpClient is called from a background task, scoped logging service is out of scope - therefore the properties that i have are lost when called within delegation handler. How do i solve that problem, do you have an idea?

DivljaVoda
Автор

Maybe it is more naive apporach but there is also a way to nest Http Handlers within constructors; something like:
var client = new HttpClient(
new myHttpMsgHanlder_1(
new myHttpMsgHanlder_2(
new HttpClientHandler()
)
));
how do you see it?

pigrebanto
Автор

how to solve this : Bearer error="invalid_token", error_description="The signature key was not found" when using http client in blazor

codecam
Автор

did it fail after 2 times? shouldn't have been 3 times the retry instead? tks

pigrebanto
Автор

This helped me so much. What Theme are you running with?

TheRealBenSanders
Автор

Do you have a video about Keycloak? Interesting topic

jaans
Автор

was getting a bit of déjà vu as i was thinking i had seen this in a video already very recently. Nick Chapsas literally had a video talking about this 4 days ago....maybe its a coincidence 👀

ThMoL
Автор

Title misleading where decorator pattern?

clashclan
Автор

I hope one day videos with goofy faces on thumbnails attract less clicks so that trend can go away. This is a programming channel, do these types of viewer's even click more from that?

altkev