[英]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.