HttpClient:通常只允许每个套接字地址协议/网络地址/端口使用一种

问题描述

我有一个应用程序(ASP NET .NET Framework),正在向我的API(.net framework Web API)发送HTTP请求(调用),有时我会遇到这样的错误-每个套接字地址只有一种用法(协议/网络地址/端口)通常是允许的。我检查了netstat,发现HttpClient正在打开连接而不是关闭连接,因此每次打开连接时,当连接限制过期时,都会出现该错误

我在开发/测试和生产环境中播种错误。但是在本地运行中找不到错误

代码示例: 这是对API的调用,其中mainClient()是创建httpClient的函数

    internal static void DeleteWorkflowDocument(int workflowId,int tableid)
    {
        HttpResponseMessage responseMessage = mainClient().PostAsync("/api/Workflow/DeleteWorkflowDocument?workflowId=" + workflowId + "&tableid=" + tableid,null).Result;
    }

这是HttpClient函数

 public static HttpClient mainClient()
    {
        try
        {
            HttpClient newMainClient = new HttpClient();
            newMainClient.BaseAddress = new Uri(System.Configuration.ConfigurationManager.AppSettings["APIURI"]);
            newMainClient.DefaultRequestHeaders.Accept.Clear();
            newMainClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            newMainClient.Timeout = TimeSpan.FromMinutes(10);

            if (HttpContext.Current.Request.Cookies["token__"] != null)
            {
                newMainClient.DefaultRequestHeaders.Add("Authorization","Bearer " + HttpContext.Current.Request.Cookies["token__"].Value.ToString());
            }
            return newMainClient;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

here as you see only one refresh is adding a huge count of connections and not closing connections

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)