如何解决App服务中SNAT耗尽的问题?

问题描述 投票:0回答:1

我在我们的一个基于 Azure App 服务的 API 中遇到了 SNAT 耗尽的问题。

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做什么吗?除了添加资源之外,还能对应用服务做些什么?先谢谢你的帮助。

c# .net azure azure-web-sites httpclient
1个回答
0
投票

HttpClient允许多个请求并行到同一个端点(默认情况下)。

以下方法是线程安全的:

  1. 取消待处理的请求
  2. DeleteAsync
  3. 列表项目
  4. GetAsync
  5. GetByteArrayAsync
  6. GetStreamAsync
  7. GetStringAsync
  8. PostAsync
  9. PutAsync
  10. SendAsync

所以,一个例子是 HttpClient 一次可以进行多个连接,通过 SendAsync. 您可以尝试通过以下方式控制连接数 服务点管理器HttpClientHandler.MaxConnectionsPerServer(最大连接数)。(关于 核心技术)属性。


0
投票

这是一个潜在的解决方案。SNAT与应用服务

© www.soinside.com 2019 - 2024. All rights reserved.