All You Need To Know About IHttpClientFactory in ASP.Net Core

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there and welcome to the code wrinkles channel in this video we'll talk about the different ways that we can use HTTP clients in asp.net core now I have already created a previous video in which I talked about different challenges and caveats that come with the use of the regular HTTP client and in this video I said that that's the reason why actually starting with asp.net core 2.1 we have ihtp client Factory as the mean to work safely with HTTP clients in asp.net core so you'll find the link to the video about HTTP client in the description below and in the corner up right here on this video and this one we'll then concentrate on how to use or how or which are the different ways in which we could use the HTTP client Factory in asp.net core applications and there are virtually three ways in which we can use it and a fourth one that's optional and that I will still show you it doesn't really or it is not a plain iftp client Factory but you'll see that it kind of like makes sense but before we get into it don't forget to hit a thumbs up button and subscribe to this channel if you find this video useful so what I have here is already four Contours that I have created for the different ways that we can use the HTTP client in asp.net core application and we'll start from the simplest one to the most complex one so the first way in which we could use an HTTP client in hp.net core is a very simple HTTP client through ihtp client Factory and to set things up here I have prepared a already populated code sample in this controller in which we instantiate a private static read-only HTTP client using the simple HTTP clients through asp.net core Clan Factory is actually very easy only thing that we need to configure here is in the program.ts on the Builder on the services we need to call this add HTTP client method and this will allow us in our controllers or wherever we need an HTTP client to just simply resolve an instance of an HTTP client through ihtp client Factory so let's go back here to the simple client controller and we have this setup here and first what I would like to do is simply just copy this and paste it here once again and you'll see just in a few seconds why I do this so to instantiate a new HTTP client to ihp client Factory first of all we need a new Constructor and in this Constructor we will then provide the ihtp client Factory as an incoming parameter and it will be resolved through dependency injection and once we have an instance of this HTTP client Factory what we can do is actually use this client and use the iftp client Factory to create a new instance of an HTTP client so it would work something like that obviously no need that we need to do here is we need to remove this static word because in this case since we get this HTTP client Factory in a Constructor we cannot use the HP client in a static context and now we would be good to go so this is literally the only change that I have made to this controller and now I am using an HTTP client through HTTP client Factory and here we have we configure The Base address some request headers and finally we do a request to the GitHub API now the reason why I just wanted you to keep this commented code line here is that a lot of people seem to underestimate how powerful this simple HTTP client Factory used is however I found it very useful because maybe 60 percent of my work may be even more I don't really work with asp.net core I often work with Legacy applications that we want to Port from older.net framework versions to.net core now in those Legacy applications usually I find a lot of such HTTP clients like we had here previously in this commented code now when I want to do this move from an older Legacy application to the newest.net core the thing is that with the HTTP client is very easy to use because I can just use this very simple client to create a new client whenever I have a new instance of the new HTTP client in the Legacy code and nothing will break everything else will still work the same but I will get a lot of different benefits and my application would overall be better by using ihtp client Factory now let's move to the second way through which we could use HTTP clients in hp.net core and this is what we usually know under named HTTP clients or named clients so I also have here a lot of stuff but here I'm just creating a new instance of the HTTP client directly in this method which is obviously bad but we will refactor this entire code and I always like to start from the HTTP client registration because that already tells us a lot about how we will use it later so I am back here at program.cs and just let me add the following what you can see here is that also on the iservice collection we call the same ad HTTP client method but in this case we can provide a name for the client and in this case I named this GitHub and another very cool thing here is that we get this I this HTTP client configuration object that we can configure so we can move all the configuration that we had previously or that we all still have basically in the controller we can move it in the configuration of the client when we register it because these are configurations that will be used basically in all calls that we will do with this GitHub client and from the registration point of view that's it we don't really need to do anything else so let's go back to our controller to make use of a named HTTP Clan first of all we also need this ihp client Factory as an incoming parameter to our Constructor that will be resolved through dependency injection and then assign change to this field and then when we need to instantiate a new client we can simply just put HTTP Clan Factory and we then create a client and provide the name with which we have registered the client so it means that right now what it will happen that is the asp.net core will give us an HTTP client that is configured is exactly the way that we have provided here be based on the name that we have used so that's why name clients are very useful because we can configure them in a central place with all the needy or all the teams we need for all the requests and we don't need to care about them anymore but this means that we can in turn come back here and we can simply get rid of all this stuff and what we keep here in this controller is only the fact that we use the HTTP client to just send a get request and read the response and then return the response to our API caller and that's it this is how you use a named HTTP client in asp.net core the third and probably most popular similar way to which we can use HTTP clients in hp.net score is what we generally call type HTTP clients and we call them tight HTTP clients because actually what we can do we can create real services that use HTTP clients and expose Behavior to Showcase this I have already created this interface I GitHub client and its interface I will Define this task of inter method that's called get followers count and I also have created here this class which is the GitHub client this class will obviously implement the igeta plan interface and as part of the interface we will need to implement the missing members now the fun part or the interesting part about this type HTTP clients is the following what we can do is we can specify here this private ring only field of type HTTP client so actually the HTTP client that we want then we can get a Constructor here and in the Constructor we also get an HTTP client and then we assign it to the private field now this is a very important important difference in comparison to the other options that we have looked earlier because in this one we don't actually need to inject ihtp cleanse Factory but instead we will inject an HTTP client and the dependency injection container will provide us a client that is configured the way that we have specified or the way that we will specify just in a few seconds now it would be a little bit fancy I can simply go here and create a private method for configured it or configuring the client and what we'll do here in this method we will add the Base address and everything that we need for this specific HTTP client instance when we instantiate the service so what I'll do in the Constructor is then I will call this configure client method now coming to the implementation let's make this async and this will be a very naive implementation of some logic in which we simply use the client to get the followers for a certain user and then we just return how many followers that specific user has so this is a very simplistic example but it showcases actually what the power of this type HTTP clients are because you would normally use type HTTP clients when you have business logic that is based on the use of a certain HTTP client or a certain external API like as you can see here we are not just returning the followers but we are applying some business logic here and we are returning the count the followers count on not just the followers also here in this type of services you can do more complex things like you can for instance call different several endpoints and then return a certain summary or a certain aggregation of those endpoints or something after you have applied some business rules on the things that were returned from those specific endpoints and now let's move over to this type client controller and start implementing it so as a private field in this case we would obviously need the I GitHub client we'll also have have a Constructor for it and then we'll Implement a very simple method that uses this service to get the follower count and then it just Returns the count to the color of the API registering this type of service is very easy what we need to do and let me go maybe here after all these other registrations is Builder services at HTTP client so the method is exactly the same but we also have a generic version in which we can specify an interface and an implementer for this interface so this is why when you use the ad HTTP client and provide this interface and the GitHub client obviously we could provide just the just the GitHub client without the interface and it will still work the same but the thing is that if we have this type of construct what it will happen is that automatically asp.net core will inject an instance of HTTP client in this service so we can just request it and it will dedicate it for this specific service that's why we don't need the HP Clan Factory or ihtp client Factory Here explicitly but behind the scenes it's actually happening exactly the same thing cool we are almost done the last thing that I wanted to show you is what we can usually call as generated client and therefore I have created this generated clients controller now generated clients is not something that you have kind of like by default in asp.net core but there are external libraries that you could use and they would generate HTTP clients for you and obviously you can then use them to make calls and I want to show you this example because I think there is this rapid Library that's actually very useful in this regard because it allows us to very easily configure HTTP clients so I have already installed in my solution refit and Rapid HTTP client Factory so as the name implies this RF it also relies on the HTTP client Factory to register or to give us HTTP clients now the reason why I think that using rapid as an HTTP client might be very useful is that if we go to this interface for instance the only thing that we need to do is just provide an interface for that specific HTTP call according to the graphic documentations so in this case I see that I want to do a get to this URL and it will return an innumerable or of object and the cool thing about this is that once you have this interface you don't actually need any other class so if we take a step back and we looked when we talked about the typed client we had this I GitHub client so you needed the interface but then you need it to also implement it the service for it now when it comes to refit in this case we can just have this interface and we don't need anything else besides the configuration that we need to put here in the program.cs the configuration here would be very similar to what we already had in ad HTTP client but the only difference here is that we call this add a refit client which is a method from Rapid HTTP client Factory and we provide the interface and based on the interface rapid will create the needed clients and make the needed calls and just give us the answer and as you can see here we also can configure the HTTP client with based receives with with Base address with default headers we can use Uber we can add event delegating Handler everything that you can con that you can configure on HTTP client you can configure it via refit so if we go back to our controller what we can do here is we can just inject this irfit GitHub client and we can use it to just get the repo branches and then return those specific branches so we just provide an interface and refuted anything else for us and that's why I think that this use of refit might also come in very handy in a lot of different circumstances now before we wrap up there is one thing that I wanted to also highlight when it comes to the difference between using this generated client like refit or our GitHub client that we have specified as a class now the thing is that refit can just call the API based on the API contract it cannot perform any business logic so if you have this type of concept of services where you need to make maybe one or more API calls to an external API but then do something with the data process that data somehow and then return something then you would definitely need to use a typed HTTP client like this GitHub client that we have created just previously but if you just need to make simple HTTP equals to an API and get the response directly from the API and don't do anything else with it then we can simply use this irrefic GitHub client which just gets us the branches now on Revit I have created a longer video dedicated only to Rapid and how we can use graphic to create basically an API client for us so if you want to check it out I will also leave a link in this in the description and probably also in a card here in the corner upright once again if you enjoyed this video and found it useful don't forget to hit the thumbs up button and like it and subscribe to this channel if you're for the first time here and if you have any question or just want to get in touch with me don't be shy and head over to the comment section leave me a comment and I would be more than happy to get in touch with you this being said thank you very much for watching and until the next time I wish you the very best
Info
Channel: Codewrinkles
Views: 7,381
Rating: undefined out of 5
Keywords:
Id: xI6uMT0bg4I
Channel Id: undefined
Length: 15min 11sec (911 seconds)
Published: Tue Jul 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.