单例 httpClient 到 AppService 的单个实例
Singleton httpClient to single Instance of AppService
首先对我糟糕的英语感到抱歉:'(.
我正在开发一个客户端,用于将 POST 消息发送到 Azure AppService 中托管的 WebAPI。我看了最好的实践是使用单例模式,所以我就这样开发。
public 静态 class UtilHTTP
{
private static readonly ConcurrentDictionary<string, HttpClient> dicClient = new ConcurrentDictionary<string, HttpClient>();
public static string PostSingleton(string url, string contentType, string accept, string rq, Dictionary<string, string> headers)
{
Task<string> response = Fetch(url, contentType, accept, rq, headers);
response.Wait();
return response.Result;
}
private static HttpClient GetdicClient(string url, string contentType, string accept)
{
string key = string.Format("{0}#{1}#{2}", url, contentType, accept);
if (!dicClient.ContainsKey(key))
{
dicClient.GetOrAdd(key, new HttpClient());
if (!string.IsNullOrEmpty(accept))
{
dicClient[key].DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(accept));
}
}
return dicClient[key];
}
private static async Task<string> Fetch(string url, string contentType, string accept, string rq, Dictionary<string, string> headers)
{
//_http.Timeout = new TimeSpan(0,0,6);
HttpContent content;
if (string.IsNullOrEmpty(contentType))
{
content = new StringContent(rq, Encoding.UTF8);
}
else
{
content = new StringContent(rq, Encoding.UTF8, contentType);
}
if (headers != null)
{
foreach (KeyValuePair<string, string> h in headers)
{
content.Headers.Add(h.Key, h.Value);
}
}
HttpResponseMessage response = await GetdicClient(url, contentType, accept).PostAsync(url, content);
string resultContent = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
resultContent = response.ReasonPhrase;
}
return resultContent;
}
}
我在 AppService 中有两个服务实例,但所有请求都发送到同一个实例,所以我无法扩展服务,性能也不是很好。
Insights Performance
代码有问题吗?你认为是服务器端的问题吗?我必须使用其他模式吗?
非常感谢!
乍一看,您正在正确地执行单例 HttpClient
(每个客户端)。我能看到的唯一问题是,如果您在 URL 中使用不同参数多次调用同一个 URL,这些参数将导致创建新客户端。不过我不知道你这样做,所以这里可能没有问题。
您讨论的另一个问题(所有请求都发送到您的应用服务的同一实例)可能与 ARR 亲和力(所谓的“粘性会话”)有关。检查您的应用服务设置(配置 -> 常规)设置)以确保它已关闭:
首先对我糟糕的英语感到抱歉:'(.
我正在开发一个客户端,用于将 POST 消息发送到 Azure AppService 中托管的 WebAPI。我看了最好的实践是使用单例模式,所以我就这样开发。
public 静态 class UtilHTTP {
private static readonly ConcurrentDictionary<string, HttpClient> dicClient = new ConcurrentDictionary<string, HttpClient>();
public static string PostSingleton(string url, string contentType, string accept, string rq, Dictionary<string, string> headers)
{
Task<string> response = Fetch(url, contentType, accept, rq, headers);
response.Wait();
return response.Result;
}
private static HttpClient GetdicClient(string url, string contentType, string accept)
{
string key = string.Format("{0}#{1}#{2}", url, contentType, accept);
if (!dicClient.ContainsKey(key))
{
dicClient.GetOrAdd(key, new HttpClient());
if (!string.IsNullOrEmpty(accept))
{
dicClient[key].DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(accept));
}
}
return dicClient[key];
}
private static async Task<string> Fetch(string url, string contentType, string accept, string rq, Dictionary<string, string> headers)
{
//_http.Timeout = new TimeSpan(0,0,6);
HttpContent content;
if (string.IsNullOrEmpty(contentType))
{
content = new StringContent(rq, Encoding.UTF8);
}
else
{
content = new StringContent(rq, Encoding.UTF8, contentType);
}
if (headers != null)
{
foreach (KeyValuePair<string, string> h in headers)
{
content.Headers.Add(h.Key, h.Value);
}
}
HttpResponseMessage response = await GetdicClient(url, contentType, accept).PostAsync(url, content);
string resultContent = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
resultContent = response.ReasonPhrase;
}
return resultContent;
}
}
我在 AppService 中有两个服务实例,但所有请求都发送到同一个实例,所以我无法扩展服务,性能也不是很好。
Insights Performance
代码有问题吗?你认为是服务器端的问题吗?我必须使用其他模式吗?
非常感谢!
乍一看,您正在正确地执行单例 HttpClient
(每个客户端)。我能看到的唯一问题是,如果您在 URL 中使用不同参数多次调用同一个 URL,这些参数将导致创建新客户端。不过我不知道你这样做,所以这里可能没有问题。
您讨论的另一个问题(所有请求都发送到您的应用服务的同一实例)可能与 ARR 亲和力(所谓的“粘性会话”)有关。检查您的应用服务设置(配置 -> 常规)设置)以确保它已关闭: