Stop using the HttpClient the wrong way in .NET

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

Hello everybody I'm Nick and in this video I will show you why the way you might be using the HttpClient in .NET could be completely wrong and then follow it up by showing you the right way to implement it.

Workshops

Don't forget to comment, like and subscribe :)

Social Media:

#csharp #dotnet #nickchapsas
Рекомендации по теме
Комментарии
Автор

Hello everybody, I forgot to mention something in the video. If you add a service with a specific lifetime in the DI container and then you use the typed client versions of AddHttpClient<TClient, TImplementation> (not the other ones for the IHttpClientFactory) then the service will be rewired as a Transient. If you need a singleton, or even scoped service, use the IHttpClientFactory which will guarantee that.

nickchapsas
Автор

Hey Nick, great video! I just wanted to say I love that you deliver videos on a constant schedule basically at the same time every week. I know that when I wake up on Monday mornings and am getting geared up for work I have a Nick Chapsas video to get my head in the zone, inspired to code. Thank you for creating these videos!

RENAUDADAM
Автор

Ive watched so many of your videos brother, this was the first time I was already doing what you suggest. Thank you so much for you content, it helps me all the time!

mrcarlpaton
Автор

FINALLY I get a Nick Chapsas video where I can say "oh yeah, I knew that 😎"
At my last job I was the driving force of getting the factory (using named clients) as THE way to use HttpClient. At my current job, it is much more of a struggle, but we will get there.

emerynoel
Автор

In my current (newest) project I used the static verstion you descriped. I've updated it to instanciate it from the builder without any problems. Thank you for sharing!

CRBarchager
Автор

The best channel for C# I have found on Youtube. Thankyou.

vivekkaushik
Автор

Note you have introduced a small unintended change in your app when you do AddHttpClient<IWeatherClient, OpenweatherClient. That method also adds IWeatherClient to the DI container, effectively redefining the scope so it is no longer a singleton. You can prove this by removing the AddSigleton line and it will all still work. This is a failing in the docs and caught me out too. I spent a while trying to understand why my HttpClient was injected as null, and it was because I had my AddSingleton AFTER my AddHttpClient.

davideglass
Автор

Best explanation I've seen, this is excellent. This problem has been painful! Subscribed, can't wait to learn about Polly.

johndirksen
Автор

I've been using IHttpClientFactory since I learned about the reason behind it. So many developers are unaware of its need and the problem it solves still.

drewkillion
Автор

Going to have to try this, I just had to build an in house authentication api and we were wondering exactly how to handle the client component of it. Thanks as always!!

joshodom
Автор

Thanks. I already had an opportunity to use the "new" method.

Whivel
Автор

Can't believe I had this right before Nick telling me. I'm improving! Thanks Nick!

ReMxDIT
Автор

Awesome, I am trying to edit already existing code and this gives me an idea why the things are the way they are.

krishnayele
Автор

You saved my life! Thank u so much! Greetings from Mexico!

revirecepcionista
Автор

Your explanation of the concept is very clear.

krishnamurthymadaraboina
Автор

Wow, I am amazed at how many videos you make. Respect.

octaviandobre
Автор

Thanks for bringing awareness to this sometimes silent issue. I would say the confusion have been due to how this type has evolved between runtimes over the years. Everything you explained can be found in the official docs around HttpClient. Microsoft have done a great job there.

yvc
Автор

Great video! I wanted to note that, for Blazor components - with a code behind that is a partial class, instead of:

private readonly IHttpClientFactory _httpClientFactory;
public MyClass(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}

you use:

[Inject]
public IHttpClientFactory _httpClientFactory { get; set; }

Hope this helps the Blazor folks!

davestorm
Автор

Hi Nick, well done on getting signed up on all the conferences, you deserve it as you are a fantastic ambassador... i hope they are looking after you :) Business Class I mean !

boommonkey
Автор

It's such a relief to find out I've been doing it the right way. I was a bit nervous there for a while...

Plankmeister