1

Currently we are using RestClient from RestSharp library like below.

var client = new RestClient("<some url>");
var request = new RestRequest("", "GET");
request.AddHeader("content-type", RestClientModel.ContentType);
request.AddParameter("redirect_uri", RedirectUrl);
RestResponse response = await client.ExecuteAsync(request);
return response;

We are facing performance issues while using RestClient. That's the reason we want to move to HttpClient. So, we need help on the following points.

  1. Using HttpClient instance in RestClient constructor which will be injected in respective class which will configure in startup.cs.
  2. What is alternate for AddParameter in HttpClient? As this doesn't seems to be equivalent to adding headers in HttpClient.

Any help on this appreciated.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
XamDev
  • 3,377
  • 12
  • 58
  • 97
  • `AddParameter` should be referring to querystring parameters. – David L May 25 '23 at 17:06
  • What, specifically, are you unsure of on the first point? https://learn.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests is a good starting point. – StriplingWarrior May 25 '23 at 18:06
  • @StriplingWarrior That one of the solution we are thinking by using HttpClient inside RestClient instead of re-writing whole thing again in HttpClient. By doing so, still it will have performance issues? – XamDev May 25 '23 at 18:15
  • It depends on a few things. If your performance issues are from socket exhaustion, and you're using a version of RestSharp that uses HttpClient under the hood, you can probably solve those performance problems either by [using singleton instances of RestClient](https://stackoverflow.com/a/71413340/120955) or by [injecting HttpClient instances](https://restsharp.dev/usage.html#reusing-httpclient) that you get from an IHttpClientFactory, or even by simply [passing `true` as the `useClientFactory` parameter](https://restsharp.dev/usage.html#simple-factory) when you create your client. – StriplingWarrior May 26 '23 at 17:01

2 Answers2

1

For GET request AddParameter adds query string parameter. Quote from docs:

var client = new RestClient("https://search.me");
var request = new RestRequest("search")
    .AddParameter("foo", "bar");
var response = await client.GetAsync<SearchResponse>(request);

It will send a GET request to https://search.me/search?foo=bar".

You can try something like:

Dictionary<string, string> queryParams = new()
{
    { "redirect_uri", RedirectUrl }
};
var query= await new FormUrlEncodedContent(queryParams).ReadAsStringAsync();

var response = await client.GetAsync($"{someUrl}?{query}")
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
1

If you inject an HttpClient to the RestClient then your second question is pointless.

You can use the exact same code to add a parameter. You don't need to port that.

var httpClient = new HttpClient() { BaseAddress = new Uri("https://httpstat.us/") };
var client = new RestClient(httpClient);
var request = new RestRequest("200", Method.Get);
request.AddParameter("delay", "1000");

Console.WriteLine(client.BuildUri(request).AbsoluteUri);    

This will print: https://httpstat.us/200?delay=1000


Dotnet Fiddle link

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • Thanks for quick help. If I use HttpCLient inside RestClient will it solve my socket exhaustion and performance issues? I will be configuring HttpClient in startup.cs like builder.Services.AddHttpClient() and injecting its dependency in RestClientService class and using in subsequent methods wherever RestClient is used. – XamDev May 26 '23 at 09:56
  • @XamDev The `RestClient` is just a thin wrapper around `HttpClient`. With this approach you have more control over the underlying `HttpClient`. If you don't provide an instance then it will create [on your behalf](https://github.com/restsharp/RestSharp/blob/cc44c361d4aa7caa5b868d4440820cd2e382a7ef/src/RestSharp/RestClient.cs#L77) – Peter Csala May 26 '23 at 10:41
  • Agree. We are seeing issue's while performance test done for concurrent users for RestClient. That's why we are thinking to use HttpClient instance in RestClient the approach which I explained in previous replies. – XamDev May 26 '23 at 11:58
  • 1
    @XamDev Quite frankly I don't think that it will solve your socket exhaustion problem but you should give it a try and see how does it behave in the wild :) – Peter Csala May 26 '23 at 12:16
  • 1
    It could solve the socket exhaustion problem if the HttpClient instances are managed correctly. Creating new HttpClients for every call is a known source of socket exhaustion, and newer versions of RestClient use HttpClient and (by default) create a new instance of HttpClient per RestClient instance. – StriplingWarrior May 30 '23 at 23:01