简体   繁体   English

HttpClient 在 GetAsync 上挂起 ERR_CONNECTION_REFUSED

[英]HttpClient Hangs on GetAsync for ERR_CONNECTION_REFUSED

I am attempting to get the status of a website in a .NET Core 3.1 worker service await client.GetAsync is only hanging on ERR_CONNECTION_REFUSED from the host我正在尝试在 .NET Core 3.1 工作人员服务await client.GetAsync仅挂在主机的 ERR_CONNECTION_REFUSED 上

Program.cs程序.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
               .UseWindowsService()
               .ConfigureServices((hostContext, services) =>
                    {
                        services.AddHostedService<Worker>();
                        services.AddHttpClient();
                    });

Worker Constructor工人构造函数

   public Worker(ILogger<Worker> logger, IConfiguration configuration, IHttpClientFactory clientFactory)
    {
        _logger = logger;
        _configuration = configuration;
        _clientFactory = clientFactory;
    }

And later in the worker in the ExecuteAsync method.稍后在 ExecuteAsync 方法中的 worker 中。

var client = _clientFactory.CreateClient();
var Siteresult = await client.GetAsync(_siteAdress);

ERR_CONNECTION_REFUSED from the server is the only way we can get this failing.来自服务器的 ERR_CONNECTION_REFUSED 是我们可以让这个失败的唯一方法。 The worker does not recover, We replicate this by turning off the site in IIS (when we turn off the app pool we get a helpful 503 and works well) we browse to the site in chrome and get ERR_CONNECTION_REFUSED but the worker just hangs.工作人员没有恢复,我们通过关闭 IIS 中的站点来复制这一点(当我们关闭应用程序池时,我们会得到一个有用的 503 并且运行良好)我们在 chrome 中浏览到该站点并获得 ERR_CONNECTION_REFUSED 但工作人员只是挂起。

In case anyone else is wondering, as per the documentation documentation using try-catch is required with an asynchronous call如果其他人想知道,根据使用 try-catch 的文档文档,异步调用需要

        bool siteDown=false;

        try
        {
            HttpResponseMessage response = await client.GetAsync(_siteAdress);
            response.EnsureSuccessStatusCode();

        }  
        catch(HttpRequestException e)
        {
            siteDown = true; 
            _logger.LogError("Message :{0} ", e.Message);

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ERR_CONNECTION_REFUSED - .Net Core 1.0.1 - ERR_CONNECTION_REFUSED - .Net Core 1.0.1 MVC应用程序文件结果在pdf上获得ERR_CONNECTION_REFUSED - MVC Application Fileresult get ERR_CONNECTION_REFUSED on pdf Plesk Linux ASP.NET Core ERR_CONNECTION_REFUSED - Plesk Linux ASP.NET Core ERR_CONNECTION_REFUSED HTTP 端点不适用于 Elsa ERR_CONNECTION_REFUSED - HTTP Endpoint not working with Elsa ERR_CONNECTION_REFUSED HttpClient.GetAsync() 挂起直到超时 - HttpClient.GetAsync() hangs until timeout ERR_CONNECTION_REFUSED 当我需要在 ASP.NET Core 中上传图片时 - ERR_CONNECTION_REFUSED When i Need to Upload Picture in ASP.NET Core 当没有互联网连接时,HttpClient.GetAsync(...)“死锁” - HttpClient.GetAsync(…) “deadlock” when there is no internet connection 我的docker-compose asp.net核心项目未正确运行,我得到ERR_CONNECTION_REFUSED - My docker-compose asp.net core project is not properly running, I get ERR_CONNECTION_REFUSED HttpClient.GetAsync挂起,然后Internet在执行过程中消失 - HttpClient.GetAsync hangs then internet disappears during its execution Xamarin 表单 HttpClient GetAsync - Xamarin Forms HttpClient GetAsync
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM