我们正在为HttpClient创建一个包装器.因为我们将遵循
https://github.com/mspnp/performance-optimization的性能优化指南.我们希望避免反模式 – 该文档中提到的不正确的实例化.我将此指南提交给我的团队使用静态HttpClient.我得到的反馈是线程安全性.每个请求都有一个包含用户声明的标头.由于我有一个静态的HttpClient,它是否是线程安全的?如果我们同时有多个请求命中代码(例如GET),那么设置标题是否会出现竞争条件?我们的实施如下.
public class HttpClientHelper{ private static readonly HttpClient _HttpClient; static HttpClientHelper() { HttpClient = new HttpClient(); HttpClient.Timeout = TimeSpan.FromMinutes(SOME_CONFIG_VALUE); } public async Task<HttpResponseMessage> CallHttpClientPostAsync(string requestUri,HttpContent requestBody) { AddHttpRequestHeader(httpClient); var response = await httpClient.PostAsync(requestUri,requestBody); //Potential thread synchronization issue??? return response; } public HttpResponseMessage CallHttpClientGet(string requestUri) { AddHttpRequestHeader(httpClient); var response = httpClient.GetAsync(requestUri).Result; //Potential thread synchronization issue??? return response; } private void AddHttpRequestHeader(HttpClient client) { string HeaderName = "CorrelationId"; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(Properties.Settings.Default.HttpClientAuthHeaderScheme,GetTokenFromClaims()); //Race condition??? if (client.DefaultRequestHeaders.Contains(HeaderName)) client.DefaultRequestHeaders.Remove(HeaderName); client.DefaultRequestHeaders.Add(HeaderName,Trace.CorrelationManager.ActivityId.ToString()); }
}
解决方法
你的团队是正确的,这远非线程安全.考虑这种情况:
>线程A将CorrelationId标头设置为“foo”.
>线程B将CorrelationId标头设置为“bar”.
>线程A发送请求,其中包含线程B的CorrelationId.
更好的方法是使用CallXXX方法创建新的HttpRequestMessage对象,并在其上设置标题,并使用HttpClient.SendAsync进行调用.
请记住,重新使用HttpClient实例仅在您对同一主机进行多次调用时才有用.