I have a call with a static method in my web application. This method uses a singleton instance of a different class. This singleton instance is basically an HttpClient to make some api requests.
Currently I don't have any mechanism to automate multi-user testing on this. Are there any consequences of this approach? I know a usual static method is thread safe if we are not using static variables inside it. But I am not sure how it behaves in case of singleton.
public static class API
{
private static System.Net.Http.HttpClient httpClient;
public static System.Net.Http.HttpClient Instance
{
get
{
return httpClient ?? (httpClient = new System.Net.Http.HttpClient());
}
}
}
public static async Task<string> GetData(string id)
{
HttpResponseMessage response = await
API.Instance.GetAsync(string.Format(requestURL, id));
response.EnsureSuccessStatusCode();
// return URI of the created resource.
return await response.Content.ReadAsStringAsync();
}