2

I am using VS2019, netcore 3.1 WebApi Project. I am trying to setup a custom health check for my DB. I see a weird behaviour . The /health uri that I configure shows me a 500 internal server when I browse the endpoint . However, /healthz endpoint gives me the status in json smoothly. My Question is what is happening with the /health endpoint that it returns me a 500 internal server error if I browse the endpoint ?

Image in the link Below on browsing https://localhost:44328/health

Image in the link Below on browsing https://localhost:44328/healthz

My Code for the configuring the endpoints:

 public void Configure(
        IApplicationBuilder app,
        IApiVersionDescriptionProvider versionProvider)
    {
       
        app.UseChaos();
        app.UseQuoteRequestCosmosDB();
        app.UseRouting();
        app.UseAuthentication();
        app.UseAuthorization();
        app.UseAuthorizationByPass();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHealthChecks("/health", new HealthCheckOptions()
            {
                Predicate = _ => true
            });
            endpoints.MapHealthChecks("/healthz", new HealthCheckOptions
            {
                Predicate = _ => true,
                ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
            });
            endpoints.MapControllers();
        });
        app.UseSwagger(versionProvider);
    }

Why is /health not working and /healthz working ?

The ConfigureServices Method:

 public void ConfigureServices(IServiceCollection services)
    {
        ConfigureApplicationDependencies(services);
        ConfigureApplicationServices(services);
        ConfigureTestServices(services);
        services.AddServiceBus(_configuration);
        services.AddCorsPolicies();
        services.AddUrlSegmentApiVersioning();
        services.AddRouting(options =>
        {
            options.AppendTrailingSlash = true;
            options.LowercaseUrls = true;
        });
        services.AddControllers()
                .AddMetrics()
                .AddChaos(_configuration)
                .AddFluentValidation(config => config.RegisterValidatorsFromAssemblyContaining<Startup>());
        services.AddHealthChecks()
                        .AddCheck<CosmosDBHealthCheck>(Constants.HealthChecks.CosmosDBHCName);

        var authzOptions = services.AddAuth(_configuration);

        services.AddSwagger(Constants.Swagger.ApiDescription, authzOptions);
        services.AddHealthChecksUI().AddInMemoryStorage();
        services.AddAppMetricsHealthPublishing();
        services.AddApplicationInsightsTelemetry();
        services.ConfigurePollyPolicies();
    }

The appSettings.json file:

"HealthChecksUI": {
"HealthChecks": [
  {
    "Name": "Health Check - API",
    "Uri": "/health"
  }
],
"Webhooks": [],
"EvaluationTimeinSeconds": 10,
"MinimumSecondsBetweenFailureNotifications": 60

},

Shumail
  • 65
  • 1
  • 6
  • Can you please show how you're registering HealthChecks with the `IServiceCollection`? – Darren Ruane Mar 16 '21 at 11:18
  • 1
    TBH the `UseEndpoints` works fine for me. You should drill down the exception you are getting. Check the logs. – Guru Stron Mar 16 '21 at 11:21
  • @DarrenRuane I have added the registration code snippet. – Shumail Mar 16 '21 at 22:39
  • @GuruStron This is what I am trying to do , I was thinking it could be some serialization related issue , but then the /healthz endpoint is spitting up the JSON perfectly. – Shumail Mar 16 '21 at 22:40
  • I noticed one thing , if I put a / in front of /health , then it is working like : https://localhost:44328/health// instead of this : https://localhost:44328/health Why is it requiring it although in the appSetting.json , I have deinfed the path as : "Uri": "/health" Any clue what I might be doing wrong while defining the paths ? – Shumail Mar 17 '21 at 02:35
  • The reason why `/healthz` works is that you customize the output using the HealthCheckOptions.ResponseWriter option, refer [Customize output](https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-5.0#customize-output). You could try to add `ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse` for the `/health` endpoint. For the 500 error, since you are checking the CosmosDB, I suggest you could check the related method such as: CosmosDBHealthCheck, perhaps the internal server error is related that. – Zhi Lv Mar 17 '21 at 08:07
  • @ZhiLv the healthcheck method CosmosDBHealthCheck has no errors . If you read my comment above , the same /health works if I append a backslash in front like this : localhost:58839/health/ instead of : localhost:58839/health Now my question is , why do I need an additional backslash in front of /health/ ? Any ideas what I could be doing wrong ? – Shumail Mar 17 '21 at 23:34
  • My bad , I actually was upgrading from core 2.1 to 3.1 . I upgraded all the packages and all the middle ware stuff as required. Had to deal with the main program.cs file and do some changes in WebHost.CreateDefaultBuilder (args). It's working now . Thank you all for the guidance. – Shumail Mar 18 '21 at 03:02

0 Answers0