【问题标题】:.NET HttpClient sendAsync() Timeout on second request specifically.NET HttpClient sendAsync() 特别是第二个请求超时
【发布时间】:2020-01-21 10:37:30
【问题描述】:

我一直在 ASP.NET 上开发一个类似代理的控制器,它从传入的请求中获取 URL,并使用 HttpClient 调用 URL,然后返回文件。

这个类似代理的控制器是这样工作的:

  • 执行一个请求时工作正常
  • 在执行多个请求时工作正常,这些响应的内容类型为“application/json”和“xml”
  • 在执行 4 个请求时,第二个请求的请求超时和任务取消,这些响应的内容类型为“image/png”。如下:

(这4个请求几乎同时发送到控制器)。

  • 请求 1 - 从 0 秒开始,在 1 秒结束
  • 请求 2 - 从 1 秒开始,在 11:00 超时并抛出异常(超时为 10 秒),服务器挂起直到超时
  • 请求 3 - 11 秒开始,12 秒结束
  • 请求 4 - 12 秒开始,13 秒结束

请忽略POST情况

控制器

public async Task<FileStreamResult> Proxy(string method, string url, string data = "")
{
    myHttpClient client = new myHttpClient();
    Tuple<MemoryStream,string> result = await client.Proxy(this.Request, method, url).ConfigureAwait(false);
    return File(result.Item1,result.Item2); //file stream and file type
}

HttpClient

public class myHttpClient
{
    private static HttpClient _httpClient;
    static myHttpClient()
    {
        if (_httpClient == null) {
            _httpClient = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
            _httpClient.Timeout = TimeSpan.FromSeconds(7);
            Log ocLog = new Log();
            ocLog.Write("init client " + ServicePointManager.DefaultConnectionLimit ,false);
        }
    }
    public async Task<Tuple<MemoryStream, string>> Proxy(HttpRequestBase contextRequest, string method, string url)
    {
        var request = new HttpRequestMessage();
        request.RequestUri = new Uri("http://localhost" + url);
        if (method == "GET")
        {
            request.Method = HttpMethod.Get;
            request.Content = null;
        }
        else if (method == "POST")
        {
            request.Method = HttpMethod.Post;
        }

        //copy request header from context.Request
        foreach (string headerName in contextRequest.Headers)
        {
            string[] headerValues = contextRequest.Headers.GetValues(headerName);
            if (!request.Headers.TryAddWithoutValidation(headerName, headerValues))
            {
                request.Content.Headers.TryAddWithoutValidation(headerName, headerValues);
            }
        }

        try
        {
            using (HttpResponseMessage response = await _httpClient.SendAsync(request).ConfigureAwait(false))
            {
                if (response.IsSuccessStatusCode)
                {
                    var contentBytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
                    MemoryStream ms = new MemoryStream(contentBytes);
                    return new Tuple<MemoryStream, string>(ms, response.Content.Headers.ContentType.ToString());
                }
                else
                {
                    return null;
                }

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

我尝试了什么

  1. 检查 ServicePointManager.DefaultConnectionLimit。它非常大。
  2. 使用 System.net.WebRequest 库代替 HttpClient,还是一样。

【问题讨论】:

  • 当您的 Web 应用程序执行异步代理控制器时,听起来好像有什么东西被锁定了。您是否尝试过查看转储文件?
  • 不,我对转储文件一无所知。我刚刚为我的代理控制器尝试了另一个图像 api,所有请求都有效。但是,当代理调用“localhost/otherWebsiteOnIIS/xxxx.fcgi?querystring”时,它不适用于某些请求。
  • 这个问题似乎是localhost向localhost本身发送请求过于频繁

标签: asp.net iis httpclient


【解决方案1】:

通过将应用程序池的“最大进程工作人员”设置为 3 解决了这个问题。它成功了

【讨论】:

    猜你喜欢
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多