THIS Will Keep Your HttpClient CLEAN

preview_player
Показать описание
Using th e HttpClient is a fairly common task for .NET developers. However, when we use the HttpClient to make calls to external services, we often also add cross-cutting concerns to it, like logging, exception handling and so on. However, this will make our HttpClient look very ugly. In this video we take a deep look into HttpClient delegating handlers and evaluate how we could use them to move our cross cutting concerns. This will keep your HttpClient clean.

Join this channel to get source code access and other perks:

Mentioned videos:

Contents:
1. Intro: 00:00
2. This HttpClient is ugly! 00:40
3. Introducing delegating handlers: 02:51
4. Move logging in delegating handler: 04:03
5. Move exception handling in delegating handler: 10:06
6. A word about decorators: 11:40

My setup:
Рекомендации по теме
Комментарии
Автор

Trying to take the least amount of your time with this video, there is a small problem that slipped through the code. In both handlers the base.SendAsync() method should of course be awaited. I simply copied the base.SendAsync() and forgot to await it. You should definitely not to the same mistake :)

Codewrinkles
Автор

You are a fantastic source of knowledge. Thank you

Bugaluggit
Автор

Interesting stuff. I thought you would break it up further like Logger delegate > Stopwatch delegate > Call. But perhaps having logging and stopwatch in one delegate is ok. Great explanation as usual. You also helped me understand filters and middleware too, Dan, Thank you!

georgeti
Автор

Thanks and Nice video, is base.SendAsync in both handlers ( Exception and Logging) will call the GitHibService twice ?

ManojLobo-ojhc
Автор

thank you so much, I have some methods like that and now you helps me to make it simple and readable

nucasspro
Автор

what should be the proper order of AddHttpMessageHandler (time: 11:16) line 8 and 9 - when ExceptionHandler is outer functionality (try/catch is enclosing the logging and stopwatch) and LoggingHandler is inner functionality (the code inside the try/catch block) - so the proper order is from inside to out? or from out to inside? thanks :)

karolsykora
Автор

I'm struggeling with registering the handler to the pipeline. The injected behaviour is never triggered. What might cause this?

SilasPeters
Автор

Hi, I really like your content and I watch all your videos. I would have a question about the logging of the duration of the request. When all the logic is in the GithubClient class you are acutally awaiting the client.GetAsync call and the logging of the duration happens really after the response is back (thanks to the await). But in the LoggingHandler you are logging the duration before the response is really obtained (no await of the base.SendAsync call) so the duration does not represent same value as in the first case. Shouldn't the SendAsync override be marked as async and the base.SendAsync call be awaited to achieve same behaviour as was in the "start code" or would this lead to any problems? Thank you

tomasp.
Автор

Do we use HTTClient as a different Layer as API-Client with Token, how can we pass the token to APi-Client project layer

kiranr
Автор

Maybe I’m mistaken but I think you miss async/await in the handlers

arnonoordover
Автор

Hi, please do MAUI in the next video ;) i've messed with that technology is pretty nice i created an app and plan on using it to create further more apps

kranefivem