我在我们的一个基于 Azure App 服务的 API 中遇到了 SNAT 耗尽的问题。
我们的HTTPClient被写成了一个单人,应该只实例化一次(C#.net 4.72)。
public class CSClient : HttpClient
{
private static readonly CSClient Inst = new CSClient();
static CSClient()
{
}
private CSClient() : base()
{
Timeout = TimeSpan.FromSeconds(60);
BaseAddress = new Uri(ConfigurationManager.AppSettings["***.BaseURL"]);
}
public static HttpClient Instance
{
get
{
return Inst;
}
}
}
然后调用
public class ContentRepository : IContentRepository
{
protected HttpClient htc = CSClient.Instance;
public async Task<Content> GetContentAsync(Content ct)
{
using (var req = new HttpRequestMessage(HttpMethod.Get, ConfigurationManager.AppSettings["***.BaseUrl"] + "/api/v2/nodes/" + ct.Id))
{
var response = await htc.SendAsync(req);
if (response.IsSuccessStatusCode)
{
var job = JObject.Parse(await response.Content.ReadAsStringAsync());
var respct = OTtoContent(job["results"]);
return respct;
}
else
{
return null;
}
}
}
}
我不知道为什么会有额外的连接。我的单例服务器正确吗?我还能对httpspclient做什么吗?除了添加资源之外,还能对应用服务做些什么?先谢谢你的帮助。
HttpClient允许多个请求并行到同一个端点(默认情况下)。
所以,一个例子是 HttpClient
一次可以进行多个连接,通过 SendAsync
. 您可以尝试通过以下方式控制连接数 服务点管理器 或 HttpClientHandler.MaxConnectionsPerServer(最大连接数)。(关于 核心技术)属性。
这是一个潜在的解决方案。SNAT与应用服务