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 HttpClient for 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)

Most upvoted comments

⚠️ 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 TestHost in .NET Core integration tests, e.g.:

public class MyIntegrationTests : IClassFixture<WebApplicationFactory<Startup>>
{
     private WebApplicationFactory<Startup> _factory;

     public MyIntegrationTests(WebApplicationFactory<Startup> factory)
     {
           this._factory = factory;
     }

     [Fact]
     public void SomeTest()
     {
           HttpClient httpClient = this._factory.CreateClient();
           IRestClient restClient = new RestClient(httpClient);

           //or
           IRestClient restClient = new RestClient();
           restClient.HttpClient = _factory.CreateClient();

           //or perhaps something more in line with .NET Core's DI fascilities.
     }
}

@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 HttpClient implementation, I need to apply the RestClient configuration properties to the HttpMessageHandler instance. Each HttpClient instance must use that handler instance, otherwise, the settings won’t get applied. But it means that when instantiating HttpClient, I need to pass the handler as the constructor parameter.

If we allow setting the HttpClient from the outside of RestClient, none of the RestClient settings 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, IRestRequest and IRestResponse.

@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 netstandard SDK is generated using the OpenAPI csharp-netcore generator which uses RestSharp under the hood. I have a rule to only use 3rd party SDKs if they allow our engineers full control over the HTTP requests by using HttpClient, so went with a hybrid solution where we used the models generated by the OpenAPI tooling, but coupled that with a Refit client which lets us use HttpClient.

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 using HttpClient in 2020. I’m glad to see support for it in RestSharp is 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.