i have a class in which there is a method wich returns http client like below.
private HttpClient client;
private HttpClient GetClient()
{
if (this.client == null)
{
this.client = new HttpClient
{
BaseAddress = new Uri("api")
};
this.client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
return this.client;
}
i am using this GetClient method in other methods like
using (var client = this.GetClient())
{
var response = await client.GetAsync($"/abc").ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}
this client is not getting null outside the using block.next time when this method hits then client is not null.whats the problem?