Endpoint for ASP.NET Core Health check does not work

I have create a new Razor project for Asp.net core 3.1 using the x-command:

x new razor RazorTest

Next, I want to add a health-check endpoint for Asp.net using the documentation from:
https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-3.1

Per documentation, I modify my Startup.cs:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Funq;
using ServiceStack;
using ServiceStack.Mvc;
using ServiceStack.Configuration;
using TestRazor.ServiceInterface;

namespace TestRazor
{
    public class Startup : ModularStartup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public new void ConfigureServices(IServiceCollection services)
        {
            #if DEBUG
            services.AddMvc(options => options.EnableEndpointRouting = false).AddRazorRuntimeCompilation();
            #else
            services.AddMvc(options => options.EnableEndpointRouting = false);
            #endif

            // NEW CODE
            services.AddHealthChecks();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseServiceStack(new AppHost
            {
                AppSettings = new NetCoreAppSettings(Configuration)
            });

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
           });

            // NEW CODE
            app.UseHealthChecks("/health");
        }
    }

    public class AppHost : AppHostBase
    {
        public AppHost() : base("TestRazor", typeof(MyServices).Assembly) { }

        // Configure your AppHost with the necessary configuration and dependencies your App needs
        public override void Configure(Container container)
        {
            base.SetConfig(new HostConfig
            {
                UseSameSiteCookies = true,
                DebugMode = AppSettings.Get(nameof(HostConfig.DebugMode), HostingEnvironment.IsDevelopment()),
            });

            if (Config.DebugMode)
            {
                Plugins.Add(new HotReloadFeature());
            }

            Plugins.Add(new RazorFormat());
        }
    }
}

After running the project, i get a “Page Not Found” instead of the actual result of the Health Check.

I had another project that was using IWebHostBuilder instead of IHostBuilder where the health check does appear to work.

I am a bit confused right now…

If you don’t want ServiceStack to return the /notfound Razor page for unknown requests you need to register any other ASP.NET Core App Modules before ServiceStack’s, e.g:

app.UseHealthChecks("/health");

app.UseServiceStack(new AppHost
{
    AppSettings = new NetCoreAppSettings(Configuration)
});
2 Likes

I’m going to slap myself.
That was the only position I did not try.

Thanks for the quick reply!

1 Like