One thing that I understood the best practice in using HttpClient
is to use a single instance of it in my project (e.g, Here)
Now I have an ASP.NET MVC project where I am using HttpClient
to consume a REST API. I created a client like below:
public class MyClient {
HttpClient client = new HttpClient();
public MyClient() {
client.BaseAddress = new Uri("http://localhost.fiddler/1/");
//
}
public async Task<SubscriptionResponse> Subscribe(SubscriptionRequest request, string serviceId) {
client.DefaultRequestHeaders.Remove("X-RequestHeader");
client.DefaultRequestHeaders.Add("X-RequestHeader", $"request ServiceId=\"{serviceId}\"");
SubscriptionResponse subscriptionResp = null;
HttpResponseMessage response = await client
.PostAsJsonAsync("subscriptions/index.php", subscription);
if (response.IsSuccessStatusCode)
{
subscriptionResp = await response.Content.ReadAsAsync<SubscriptionResponse>();
}
return subscriptionResp;
}
}
So in order to use a single instance of HttpClient
I am going to use a dependency container to inject a singleton instance of MyClient
in my controllers. And I will use it in actions like below:
public MyClient Client {get; set;} // Will be injected by a dependency container
public async Task<ActionResult> CallApi(SubscriptionRequest req, string serviceId) {
await Client.Subscribe(request, serviceId);
return View();
}
Now my question is that am I using HttpClient
safely? I don't have any experience regarding code thread safety and my emotions are telling me that there is something wrong here :D
I am specifically talking about this section in Subscribe
method:
client.DefaultRequestHeaders.Remove("X-RequestHeader");
client.DefaultRequestHeaders.Add("X-RequestHeader", $"request ServiceId=\"{serviceId}\"");
UPDATE I am concerned about this as multiple threads may call this method at the same time.