Net核心Web应用程序中不同端口上的运行状况检查 [英] Healthchecks on different port in Net core Web application
问题描述
我有一个带有API的标准Net核心Web应用程序.我正在尝试实施运行状况检查,但希望它在其他端口上运行.
I have a standard Net core web application with an API. I'm trying to implement healthcheck, but want it to run on a different port.
在配置"中:
app.UseHealthChecks("/health", 8000);
在ConfigureServices中
In ConfigureServices
services.AddHealthChecks()
默认情况下,可以通过以下方式访问api: https://localhost:44322/api/values
By default, the api is accessible via: https://localhost:44322/api/values
然后我希望可以通过以下方式访问运行状况检查: https://localhost:8000/health
And I then expect the healthcheck to be accessible via: https://localhost:8000/health
如果我移除端口,则恢复正常.我是否需要其他配置才能将应用程序设置为在不同端口上运行?
The healh works good if I remove the port. Do I need some other configuration to setup the application to run on different ports?
推荐答案
根据要求,我正在创建一个答案,以便将来的读者受益.
As requested, I'm creating an answer so future readers can benefit from.
最初的问题是因为您没有在应用程序中注册运行状况检查端口 8000
.有两种方法可以做到这一点.
The initial problem was because you were not registering the health check port 8000
in your app. There are two ways to do this.
By setting the port in
launchSettings.json
which is picked up by the Environment Variables Configuration Provider
或者按照我在注释中的建议,在 Program.cs
中调用 UseUrls
并直接在其中指定HealthCheck端口以及您的应用程序端口.像这样的东西:
Or as I suggested in the comments, call UseUrls
in Program.cs
and specify the HealthCheck port directly there, along with the ones for your app. Something like this:
return new WebHostBuilder()
.UseConfiguration(config)
.UseUrls("http://localhost:5000/;http://localhost:8000")
.UseKestrel()
.UseStartup(startupType)
.Build();
其中 5000
是您的常规App端口,而 8000
是HealthCheck端口.
Where 5000
is your normal App port, and 8000
is the HealthCheck one.
然后在 Startup.cs
的 Configure
中,直接使用端口:
Then inside Configure
in Startup.cs
you use the port directly:
app.UseHealthChecks("/health", port: 8000);
You can find all of this and more in the official docs: Health checks in ASP.NET Core - Filtering by Port
这篇关于Net核心Web应用程序中不同端口上的运行状况检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!