IHttpClientFactory间歇性错误:通过Azure Function应用发送请求时发生错误

时间:2019-05-01 10:16:45

标签: c# azure asp.net-core azure-functions asp.net-core-webapi

使用_httpClientFactory.CreateClient()向外部资源发送请求时,间歇性地出现以下错误。

  

System.Net.Http.HttpRequestException:发送时发生错误   请求。 ---> System.IO.IOException:服务器返回一个   无效或无法识别的响应。在异步   System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage   请求,CancellationToken取消Token)内部结束   异步异常   System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage   请求,CancellationToken取消令牌)位于   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()在   异步的   System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection   连接,HttpRequestMessage请求,布尔   doRequestAuth,CancellationToken cancelledToken)   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()在   异步的   System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage   request,Boolean doRequestAuth,CancellationToken cancelledToken)
  在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  在异步System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage   请求,CancellationToken取消令牌)位于   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()在   异步System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage   请求,CancellationToken取消令牌)位于   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()在   异步的   Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage   请求,CancellationToken取消令牌)位于   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()在   异步的   Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage   请求,CancellationToken取消令牌)位于   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()在   异步System.Net.Http.HttpClient.FinishSendAsyncBuffered(任务1   sendTask,HttpRequestMessage请求,CancellationTokenSource   cts,Boolean disposeCts)   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()在   异步的   IdentityModel.Client.HttpClientTokenRequestExtensions.RequestTokenAsync(??)

 public class WebHttpClient {

        private readonly IHttpClientFactory _httpClientFactory;

        public WebHttpClient(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }

        public async Task<string> WebRequestAsync(string url)
        {
            return await _httpClientFactory.CreateClient().WebRequestAsync(url);  //intermittent error: An error occurred while sending the request.
        }

    }

    [assembly: WebJobsStartup(typeof(Startup))]
public sealed class Startup : IWebJobsStartup
{
    public void Configure(IWebJobsBuilder webJobsBuilder)
    {
        webJobsBuilder.Services.AddHttpClient();
    }
}

有解决方案吗?

仅供参考

静态HttpClient的DNS存在问题。这就是为什么我尽可能避免使用static,并且使用IHttpClientFactory可以解决所有问题的原因。

但是我不确定我的问题是由DNS还是其他原因引起的。

类型化的httpclient(或围绕httpclient的包装)是否可以解决此问题?它是断断续续的,因此不容易复制。

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.2#typed-clients

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.2#httpclient-and-lifetime-management

0 个答案:

没有答案