httpclient PostAsJsonAsync 在 (n) 条消息后出现错误(端口耗尽?)

httpclient PostAsJsonAsync errors after (n) messages (port exhaustion?)

我在 Azure 中有一个控制台应用程序 (webjob) 运行ning,它在 azure 中调用了一个 API 运行ning。控制台应用程序从 csv 文件发布消息。 当我从本地 (windows 10) 机器 运行 控制台应用程序(使用 api 运行ning 时,它按预期工作(没有错误)。 当我 运行 Azure 中的控制台应用程序(api 运行ning 在 azure 中)它处理大约 980 条消息然后开始报告错误

错误:

我相信由于某种原因,当控制台应用程序在 Azure 中 运行ning 时,它正在耗尽端口,但我不知道如何解决这个问题。 我的文件有 50,000 行,但如前所述,在问题出现之前只达到了大约 980 行。 在另一端,api 将收到的消息发布到 sql 数据库和 azure 事件中心。 PostAsJsonAsync 完成后,如何强制应用程序释放端口? 如何检查端口是否已释放或如何检查端口是否可用? 如果您知道我可能会收到这些错误的任何其他原因,请提供帮助。

这是我的代码:

public async Task<string> PostMessageToAPI(string aMsg)
{
    HttpResponseMessage response = null;
    string strResponse = "";    
    try
    {
        using (var client = new HttpClient())
        {
            client.SetBearerToken("access token here");
            client.BaseAddress = new Uri("MyBaseUrl.com");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            using (response =  await client.PostAsJsonAsync("my/url/goes/here", anEvent.ToLower()))
            {
                if (response != null)
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        throw new HttpRequestException(response.ReasonPhrase);
                    }
                    else
                    {
                        return await response.Content.ReadAsStringAsync();
                    }
                }
            }
        }
    }
    catch (HttpRequestException ex)
    {       
        //  exception message states: "An error occurred while sending the request"
    }   
    return strResponse;           
}

请不要使用 HttpClient 包裹在 using 中。使用静态实例并分享。

public class MyClass()
{
private HttpClient client = null;
public MyClass(){
    client = new HttpClient();
    client.SetBearerToken("access token here");
    client.BaseAddress = new Uri("MyBaseUrl.com");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

}
public async Task<string> PostMessageToAPI(string aMsg)
{
    HttpResponseMessage response = null;
    string strResponse = "";    
    try
    {
        using (var response =  await client.PostAsync("my/url/goes/here", new StringContent(aMsg, Encoding.UTF8, "application/json")))
        {
            if (response != null)
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw new HttpRequestException(response.ReasonPhrase);
                }
                else
                {
                    return await response.Content.ReadAsStringAsync();
                }
            }
        }

    }
    catch (HttpRequestException ex)
    {       
        //  exception message states: "An error occurred while sending the request"
    }   
    return strResponse;           
}
}

您甚至可以共享 Class 的同一个实例,关键是,每次都重新创建和重新打开套接字并不是最佳选择,它最终会导致套接字耗尽,具体取决于您的调用吞吐量。

Source