Healthchecks for SOAP service in asp.net microservice

75 Views Asked by At

I have this setup for a HealthCheck for a WCF SOAP service and for some reason the code in the second delegate is not executed. If I put breakpoint it's not hit also in debug.

HealthCheckConfiguration healthCheckConfiguration = new ();
configureHealthChecks.Invoke(healthCheckConfiguration);
 
services
    .AddHealthChecks()
    .AddUrlGroup(
        (uriOptions) => uriOptions.UseHttpMethod(HttpMethod.Head),
        healthCheckConfiguration.Name,
        healthCheckConfiguration.FailureStatus,
        healthCheckConfiguration.Tags,
        healthCheckConfiguration.Timeout,
        (serviceProvider, httpClient) =>
        {
            var config = serviceProvider.GetRequiredService<IOptionsMonitor<TSettings>>();
            httpClient.BaseAddress = new($"{config.CurrentValue.Endpoint}?wsdl");
        });

The similar delegate in the second sample is hit and is working correctly, but we want to change the request for the HealthCheck from GET to HEAD.

services
.AddHealthChecks()
    .AddUrlGroup(
        (services) =>
        {
            var config = services.GetRequiredService<IOptionsMonitor<TSettings>>();

            return new Uri($"{config.CurrentValue.Endpoint}?wsdl");
        },
        healthCheckConfiguration.Name,
        healthCheckConfiguration.FailureStatus,
        healthCheckConfiguration.Tags,
        healthCheckConfiguration.Timeout);
1

There are 1 best solutions below

0
QI You On

Try a custom check:

public class ServiceCheck : IHealthCheck
{
    public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
    {
        bool result = IsServiceOnline();
        if (result)
            return HealthCheckResult.Healthy();
        return HealthCheckResult.Unhealthy();
    }

    private bool IsServiceOnline()
    {
       //Your custom needs, such as HttpMethod.Head and other things
    }
}

public void ConfigureServices(IServiceCollection services)
{
    services.AddHealthChecks().AddCheck<ServiceCheck>($"{config.CurrentValue.Endpoint}?wsdl");
}