RestSharp: Use HttpClient instead of HttpWebRequest - breaking change
Currently, RestSharp uses HttpWebRequest, since that was the “way to do it” back in the days.
Time passed by and HttpClient dominates now, so we need to move on.
There are several issues with HttpWebRequest implementation that could be resolved by using HttpClient:
- Use proper async flow with cancellation and context
- Use a single instance of
HttpClientfor connection pooling - Support HTTP2
These are the main issues but there’re advantages that overall come with more modern implementation.
In the latest versions of .NET framework (full and core), HttpWebRequest uses the HttpClient anyway.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 16
- Comments: 29 (9 by maintainers)
⚠️ This issue has been marked wontfix and will be closed in 3 days
I would also like to vouch for enabling the injection of an HttpClient into an IRestClient, so that we can use the clients that are being set up by
TestHostin .NET Core integration tests, e.g.:@zachrybaker sorry, but no, I can’t. I have a lid job and after half a year since RestSharp has sponsorship enabled, no one is willing to contribute depicts the package is being downloaded tens of thousands times per day. I can do charity for just as much, but no more.
It’s been about a month so I’m just checking if there are any updates on this matter. I would love to keep with RestSharp instead of switching all my code over to the HTTPClientFactory.
Is there a branch accessible with what’s been done so far?
Thanks for all your hard work.
@TeunKooijman it won’t be that straightforward.
As part of the
HttpClientimplementation, I need to apply theRestClientconfiguration properties to theHttpMessageHandlerinstance. EachHttpClientinstance must use that handler instance, otherwise, the settings won’t get applied. But it means that when instantiatingHttpClient, I need to pass the handler as the constructor parameter.If we allow setting the
HttpClientfrom the outside ofRestClient, none of theRestClientsettings will apply to that client and it won’t really test anything.Is there a branch with the in-progress work? Is any assistance needed?
I am also curious as to what breaking changes may be expected to
IRestClient,IRestRequestandIRestResponse.@zachrybaker @mycomycul @bluetentacle if you’re not precious about RestSharp as a dependency, Refit has full support HttpClient and HttpClientFactory and is a reasonably mature library.
I noticed when doing an integration with Xero their official
netstandardSDK is generated using the OpenAPIcsharp-netcoregenerator which usesRestSharpunder the hood. I have a rule to only use 3rd party SDKs if they allow our engineers full control over the HTTP requests by usingHttpClient, so went with a hybrid solution where we used the models generated by the OpenAPI tooling, but coupled that with aRefitclient which lets us useHttpClient.The ability to add a custom pipeline of
DelegatingHandler’s to do logging/tracing/metrics/exception handling/rate-limiting/fault-tolerance/auth in a way that is completely decoupled from the actual client itself has been quite a game changer in terms of maintainability and stability of our integrations. It doesn’t make sense to not be usingHttpClientin 2020. I’m glad to see support for it inRestSharpis under consideration, as I know this library still has a large install-base and a large part of the mindshare as the go-to solution due to its long-standing popularity.I have a branch that I can share. The struggle is around the set of settings that would require creating a new HttpMessageHandler instance. The rest is quite trivial.
I’d love to see RestSharp get this update. @alexeyzimarev, please reach out if there’s any questions on
HttpClient.