NotDeadYetContributors / NotDeadYet

.NET application health checking made easy
72 stars 15 forks source link

Web Api 2 Health Check fails when api hosted as sub application #19

Open CraigRice opened 6 years ago

CraigRice commented 6 years ago

Hi,

I found an issue running the web api 2 health check when the site is hosted in a sub directory / application. The health check does not show. So this is the current fix for it:

Register like:

var healthChecker = ServiceLocator.Resolve<IHealthChecker>(); config.RegisterHealthCheckRoute(healthChecker);

Which calls this:

internal class ExactMatchConstraint : IHttpRouteConstraint { public bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary<string, object> values, HttpRouteDirection routeDirection) { return string.CompareOrdinal(route.RouteTemplate.Trim('/'), request.RequestUri.AbsolutePath.Trim('/')) == 0; } }

internal static class ConfigExtensions
{
    public static void RegisterHealthCheckRoute(this HttpConfiguration config, IHealthChecker healthChecker, string routeUrl = "healthcheck")
    {
        var defaults = new HttpRouteValueDictionary();
        config.Routes.MapHttpRoute("healthcheck", routeUrl, defaults, new ExactMatchConstraint(), new HealthCheckMessageHandler(healthChecker));
    }
}
akatakritos commented 6 years ago

I hit this too, and worked around this by registering the route myself and removing the constraint:

// instead of this
config.RegisterHealthCheckRoute(healthChecker);

// do this
config.Routes.MapHttpRoute(
                name: "healthcheck", 
                routeTemplate: "healthcheck",
                defaults: null,
                constraints: null,
                handler: new HealthCheckMessageHandler(healthChecker));