来自 MVC 的 windows 服务的健康检查

Healthchecks for windows services from MVC

我有一个 .NET Core MVC 前端,它依赖于两个 Windows 服务,它们 运行 并行。 我想提供健康检查 Windows 服务是否在 startup.cs 中 运行ning MVC项目的。 如何将服务的状态添加到健康检查中?这是我目前所拥有的:

    ServiceController[] windowsservices = ServiceController.GetServices();
    foreach (ServiceController service in windowsservices)
    {
        var serviceStatus = service.ServiceName + "==" + service.Status;
    }

    var connectionString = "myConnectionString";

    services.AddHealthChecks()
        .AddSqlServer(connectionString, failureStatus: HealthStatus.Unhealthy);

   app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
        endpoints.MapHealthChecks("/health");
    });

AspNetCore.HealthChecks.System 包中已经提供了这样的健康检查。将 NuGet 包添加到 Web 项目后,您可以使用 AddWindowsServiceHealthCheck:

注册 Windows 服务健康检查
var healthBuilder=services.AddHealthChecks()
    .AddSqlServer(connectionString, failureStatus: HealthStatus.Unhealthy);

foreach (ServiceController service in windowsservices)
{
    healthBuilder.AddWindowsServiceHealthCheck(service.ServiceName);
}