基于所有具有一定界面的服务,动态创建健康检查

发布于 2025-02-09 10:06:39 字数 1088 浏览 1 评论 0原文

我有一系列检查数据的服务,所有服务都在“ idatacheck”接口下。

idatachecks是一个空的接口,它

在命令行应用程序中使用一种方法“ runcheckAsync”:

IEnumerable<IDataCheck>? services = _serviceProvider.GetServices<IDataCheck>();
foreach (IDataCheck? s in services)
{
    DataCheckResult result = await s.RunCheckAsync();
    // all kinds of stuff with the result such as proposing to autofix
}

我现在也想使用healthcheck ui,ref:

不想通过数据检查手动创建健康检查

我 要创建一个HealthCheck,否则它将在UI中仅在一个文本列中产生一长串结果,因为它不支持任何格式。

I have a long set of services that check data, and all under the interface "IDataCheck".

IDataChecks is an empty interface with one method "runCheckAsync"

In a commandline app I do:

IEnumerable<IDataCheck>? services = _serviceProvider.GetServices<IDataCheck>();
foreach (IDataCheck? s in services)
{
    DataCheckResult result = await s.RunCheckAsync();
    // all kinds of stuff with the result such as proposing to autofix
}

I now want to also present the results of these checks in asp.net core healtchecks using also the healthcheck ui, ref:

I do not want to create manually a healthcheck per data check because otherwise the usefulness of having an interface is gone

I also do not want to create one healthcheck only otherwise it will produce a long list of results all in one text only column in the ui since it does not support any formatting.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖伴 2025-02-16 10:06:39

在“ webapplication app = builder.build();”之前,在宗师的评论的帮助下解决了这一点。打电话:

public static IServiceCollection AddDataHealthChecks(this IServiceCollection services)
{
    List<string> dataCheckClassList = new List<string>();
    IEnumerable<ServiceDescriptor>? dataServices = services.Where(x => x.ServiceType == typeof(IDataCheck));        
    foreach (ServiceDescriptor s in dataServices)
    {
        var fullName = s.ImplementationType?.FullName;
        if (fullName is not null)
        {
            dataCheckClassList.Add(fullName);
        }
    }
    foreach(string fullName in dataCheckClassList)
    {
        services
                .AddHealthChecks()
                .AddTypeActivatedCheck<DataQualityHealthCheck>(fullName,
                    failureStatus: HealthStatus.Degraded,
                    tags: new[] { "data" },
                    args: new object[] { fullName });
    }

    return services;
}

然后在Healtcheck本身中:

IDataCheck? service = _serviceProvider
            .GetServices<IDataCheck>().FirstOrDefault(x => (x.ToString() == _dataCheckServiceName));
if (service is not null)
{
   DataCheckResult result = await service.RunCheckAsync();
   // etc
}

Solved this , with help from the comment from Guru, on the line before "WebApplication app = builder.Build();" to call:

public static IServiceCollection AddDataHealthChecks(this IServiceCollection services)
{
    List<string> dataCheckClassList = new List<string>();
    IEnumerable<ServiceDescriptor>? dataServices = services.Where(x => x.ServiceType == typeof(IDataCheck));        
    foreach (ServiceDescriptor s in dataServices)
    {
        var fullName = s.ImplementationType?.FullName;
        if (fullName is not null)
        {
            dataCheckClassList.Add(fullName);
        }
    }
    foreach(string fullName in dataCheckClassList)
    {
        services
                .AddHealthChecks()
                .AddTypeActivatedCheck<DataQualityHealthCheck>(fullName,
                    failureStatus: HealthStatus.Degraded,
                    tags: new[] { "data" },
                    args: new object[] { fullName });
    }

    return services;
}

and then in the healtcheck itself :

IDataCheck? service = _serviceProvider
            .GetServices<IDataCheck>().FirstOrDefault(x => (x.ToString() == _dataCheckServiceName));
if (service is not null)
{
   DataCheckResult result = await service.RunCheckAsync();
   // etc
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文