2

I'm wanting to add some health checks to my .net 6 worker project but I'm not sure how.

This is the sort of code I would like to add from the old style Startup.cs class

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapHealthChecks("/hc", new HealthCheckOptions()
        {
            Predicate = _ => true,
            ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
        });
        endpoints.MapHealthChecks("/liveness", new HealthCheckOptions
        {
            Predicate = r => r.Name.Contains("self")
        });
    });
}

In a .net 6 web application it looks like you would do something like this...

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.UseRouting();
app.UseEndpoints(endpoints =>
{
    endpoints.MapHealthChecks("/hc", new HealthCheckOptions()
    {
        Predicate = _ => true,
        ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
    });
    endpoints.MapHealthChecks("/liveness", new HealthCheckOptions
    {
        Predicate = r => r.Name.Contains("self")
    });
});

app.Run();

However WebApplication.CreateBuilder(args) is not available in a worker template. Instead you get this...

IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices(services => { services.AddHostedService<Worker>(); })
    .Build();

await host.RunAsync();

It looks like it should be possible as there is an amalgamation of approaches in the eShopOnContainers reference app here

Konzy262
  • 2,747
  • 6
  • 42
  • 71
  • Do worker services have a concept of middleware? I think of middleware being part of the request/response pipeline of a web app. I'm curious if this is possible – Jonesopolis Jun 06 '22 at 18:37
  • 1
    See https://stackoverflow.com/questions/65165528/is-microsoft-net-sdk-worker-compatible-with-an-api-project . There's nothing wrong with having using the Web template just for a worker service. The Worker SDK is essentialy the regular .NET SDK with pre-added `Microsoft.Extensions.Hosting` nuget package. – Hank Jun 06 '22 at 18:42

1 Answers1

5

To set the context, if you want to expose the /hc endpoint, you need something that listens to (and replies to) HTTP requests on a /hc path. In other words, we need to add an HTTP-listener (a web host in .NET parlance) to the worker.

So, lets do that.

First, we need to configure our SDK project to use the Web SDK. Change your project type from:

<Project Sdk="Microsoft.NET.Sdk.Worker">

To:

<Project Sdk="Microsoft.NET.Sdk.Web">

Next, instead of a Host, we can use WebApplication:

var builder = WebApplication.CreateBuilder(args);

Then, we can add the two services we want (HealthCheck + Worker):

builder.Services.AddHealthChecks();
builder.Services.AddHostedService<Worker>();

Then we can map the endpoints:

var app = builder.Build();
    
app.UseRouting();
app.UseEndpoints(endpoints =>
{
   endpoints.MapHealthChecks("/hc");
});

And finally run the application:

app.Run();

I found https://learn.microsoft.com/en-us/aspnet/core/migration/50-to-60-samples?view=aspnetcore-6.0 a great resource to see how to convert some 3.1-style code to 6.0 style code.

omajid
  • 14,165
  • 4
  • 47
  • 64