IHttpClientFactory intermittent error: An error occurred while sending the request with Azure Function Apps

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 17:53:45

问题


The error below occurs intermittently when _httpClientFactory.CreateClient() is used to send request to external resources.

System.Net.Http.HttpRequestException : An error occurred while sending the request. ---> System.IO.IOException : The server returned an invalid or unrecognized response. at async System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request,CancellationToken cancellationToken) End of inner exception at async System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request,CancellationToken cancellationToken) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection,HttpRequestMessage request,Boolean doRequestAuth,CancellationToken cancellationToken) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request,Boolean doRequestAuth,CancellationToken cancellationToken)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request,CancellationToken cancellationToken) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask,HttpRequestMessage request,CancellationTokenSource cts,Boolean disposeCts) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async 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();
    }
}

Any solution?

Update

static HttpClient has issues with DNS. This is why I try to avoid static if possible and if using IHttpClientFactory can resolve all issues.

But I am not sure if my issue is caused by DNS or something else.

Would a typed httpclient (or wrapper around httpclient) fix this issue? It is intermittent so it is not easy to reproduce.

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

Update 2

Please see the updated exception at the top.

来源:https://stackoverflow.com/questions/55846379/ihttpclientfactory-intermittent-error-an-error-occurred-while-sending-the-reque

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!