简体   繁体   English

在多个客户端/用户上具有相同身份验证标头的c#Httpclient单例

[英]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. 我有一个调用API的azure函数,我在启动Dependency Injection中将我的HttpClient做为单例,因此可以在我的承包商上调用它。 My code below calls 2 API with the same Authentication header. 我下面的代码使用相同的Authentication标头调用2个API。

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. 在上面的代码中,我有两个API调用,它们使用相同的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. 由于它是单例,因此该函数可以由具有不同OAthToken作为参数的多个用户调用,并共享相同的HttpClient实例。 Should I need to refresh the Default header like this so the other thread wont be affected? 我是否需要像这样刷新Default标头,以便其他线程不会受到影响?

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 . 官方文档建议您使用HttpClientFactory来实现弹性HTTP请求 It makes the management of HttpClient instances easier. 它使HttpClient实例的管理更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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