简体   繁体   中英

c# Httpclient singleton with same authentication header on multiple client/user

I have an azure function that calls API and I made my HttpClient as a singleton in the startup Dependency Injection so I can call it on my contractor. My code below calls 2 API with the same Authentication header.

public class MyClass : IMyClass
{
        private readonly HttpClient _httpClient;
        public MyClass(HttpClient httpClient)
        {
            _httpClient = httpClient;
        }

        public void test(string OAuthToken)
        {
            _httpClient.DefaultRequestHeaders.Accept.Clear();
            _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", OAuthToken);

            // 1st API
            string firstApi = $"https://GetSometthingFirst.com/processes?api-version=5.0";
            var GetFirst = _httpClient.GetAsync(firstApi).Result;

             // add delay 5 secs
             Thread.Sleep(5000);


            // 2nd API
            string secondApi = $"https://GetSometthingSecond.com/processes?api-version=5.0";
            var content = new StringContent(GetFirst.ToString(), Encoding.UTF8, "application/json");

            var result = _httpClient.PostAsync(secondApi, content).Result;
        }

}

In the code above I have two API calls which use the same DefaultRequestHeader. Since it's a singleton the function can call by multiple user with different OAthToken as the parameter and share the same instance of HttpClient. Should I need to refresh the Default header like this so the other thread wont be affected?

public void test(string OAuthToken)
        {
            _httpClient.DefaultRequestHeaders.Accept.Clear();
            _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", OAuthToken);

            // 1st API
            string firstApi = $"https://GetSometthingFirst.com/processes?api-version=5.0";
            var GetFirst = _httpClient.GetAsync(firstApi).Result;

            _httpClient.DefaultRequestHeaders.Accept.Clear();
            _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", OAuthToken);

             // add delay 5 secs
             Thread.Sleep(5000)

            // 2nd API
            string secondApi = $"https://GetSometthingSecond.com/processes?api-version=5.0";
            var content = new StringContent(GetFirst.ToString(), Encoding.UTF8, "application/json");

            var result = _httpClient.PostAsync(secondApi, content).Result;
        }

The official documentation suggests you use HttpClientFactory to implement resilient HTTP requests . It makes the management of HttpClient instances easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM