Vadim Smirnov
Vadim Smirnov

Reputation: 21

How to make Health Checks in NET Aspire right way?

I am try Aspire and get very confused about Health Checks. All blogs and articles copy same code from NET Aspire documentation what just not enought.

NET Aspire documentation has two case:

  1. Simplified few lines of code about "How to make Health Check for single Process"
  2. Aspire-Sample github project with few pages of code about anti-hacker measurement

What I am wanted? A way to show MyProcess state in NET Aspire Dashboard similar to Redis cache process in Net Aspire App template sample.

I am decompiled AddRedis() and found what they register Health Check within AppHost and try to connect to database.

Unfortunate I am can not consider this approach as universal or preferable because not all situation can be checked from AppHost. For example: access to resources (relative path file) from dockered MyProcess.

How to do it right way? May be create first Health Check in MyProcess and second Health Check in Apphost where call http://myprocess/health ?

Answer:

  1. It's not obvious from the NET Aspire documentation, but the health checks in Projects and Dashboard are completely unrelated. The dashboard only shows health checks from the AppHost.
  2. Fortunately, we can transmit health checks from external resources using the AddUrlGroup(...) extension. Something like
    builder.Services
        .AddHealthChecks()
        .AddUrlGroup(new Uri("http://service"), "HealthPoint1")
    var service = builder
        .AddProject<Projects.X>("ProjectX")
        .WithHealthCheck("HealthPoint1")
  1. Unfortunately AddUrlGroup(...) it is not convenient in NET Aspire because of the dynamic distribution of endpoints.
  2. Fortunately, in .NET Aspire 9.0 we can use a new extension - with Httpshealthcheck(...) or its Http version. Something like
    var service = builder
        .AddProject<Projects.X>("ProjectX")
        .WithHttpsEndpoint(name: "X")
        .WithHttpsHealthCheck("/health", 200, "HealthPoint1");
  1. Unfortunately, again, with limitations. Httpshealthcheck(...) checks only the response code and does not transmit any other information, such as a text message. And the name of the health check in the Dashboard is very... auto-generated. But it's much better than nothing.

Upvotes: 0

Views: 124

Answers (0)

Related Questions