Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relative paths for the health-check UI only appear to be working on local iis express or Kestrel #1420

Open
DanielPollackDev opened this issue Aug 29, 2022 · 3 comments
Labels

Comments

@DanielPollackDev
Copy link

Relative paths for the health-check UI only appear to be working on local iis express or Kestrel when run from Visual Studio. I am not able to get this to work correctly in a deployed environment. When deployed, the UI comes up, but displays an error message stating: Could not get endpoint uri from configuration. I am using .net6. My appsettings.json config section looks like this:
"HealthChecksUI": {
"HealthChecks": [
{
"Name": "Configuration-Service",
"Uri": "/healthz"
}
],
"Webhooks": [
{
"Name": "",
"Uri": "",
"Payload": "",
"RestoredPayload": ""
}
]
},

My program.cs file looks like this:

...
builder.Services.AddHealthChecks()
...
builder.Services.AddHealthChecksUI().AddInMemoryStorage();
...
app.MapHealthChecks("/healthz", new HealthCheckOptions
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
...
app.UseHttpsRedirection();
app.MapHealthChecksUI();
app.UseRouting();
app.MapControllers();
app.Run();

@Khaos66
Copy link

Khaos66 commented Oct 22, 2022

Had this issue as well. Here is my workaround:

Add package Microsoft.Web.Administration

public static class IisHelper
{
    public static string? GetIisUrl(IWebHostEnvironment env)
    {
        try
        {
            using ServerManager serverManager = new ServerManager();
            var site = serverManager.Sites.FirstOrDefault(s => s.Name == env.ApplicationName);
            if (site?.Bindings?.Count > 0)
            {
                var binding = site.Bindings.First();
                var host = binding.Host;
                if (string.IsNullOrWhiteSpace(host))
                {
                    if (binding.EndPoint.Address != IPAddress.Any)
                    {
                        host = binding.EndPoint.Address.ToString();
                    }
                    else
                    {
                        host = Environment.MachineName;
                    }
                }
                var url = $"{binding.Protocol}://{host}:{binding.EndPoint.Port}";
                Log.Information("IIS serving url: {Url}", url);
                return url;
            }
        }
        catch(Exception ex)
        {
            Log.Warning(ex, "Unable to load IIS Url");
        }
        return null;
    }
}

Call it like this:

// Health Checks
string? iisUrl = IisHelper.GetIisUrl(builder.Environment);
builder.Services
        .AddHealthChecksUI(setup => setup.AddHealthCheckEndpoint("SWSIdentity", (iisUrl ?? string.Empty) + "/healthz"))
        .AddInMemoryStorage()
        .Services
            .AddHealthChecks()
            // ...

@darraghjones
Copy link

I think this issue might be related aspnet/Hosting#956

@danmcmyler-lancs
Copy link

danmcmyler-lancs commented Jul 2, 2024

After being inspired by @Khaos66 this worked for me..

 public static class ApplicationUrlHelper
 {
     public static string GetApplicationBaseUrl(IConfiguration configuration)
     {
         var options = configuration.GetSection("ApplicationUrl").Get<ApplicationUrlOptions>();

         if (string.IsNullOrEmpty(options?.BaseUrl))
         {
             // Fallback to environment variable if configuration is not set
             var aspNetCoreUrls = Environment.GetEnvironmentVariable("ASPNETCORE_URLS");
             if (!string.IsNullOrEmpty(aspNetCoreUrls))
             {
                 var urls = aspNetCoreUrls.Split(';');
                 return urls[0]; // Use the first URL
             }

             // If all else fails, use a default or log a warning
             Log.Warning("ApplicationUrlHelper: Defaulting to localhost:4200");
             return "http://localhost:4200";

         }

         return options.BaseUrl;
     }
 }

//Program.cs

builder.Services.Configure<ApplicationUrlOptions>(builder.Configuration.GetSection("ApplicationUrl"));

string baseUrl = ApplicationUrlHelper.GetApplicationBaseUrl(configuration);

 services.AddHealthChecksUI(setupSettings: setup =>
 {
     setup.AddHealthCheckEndpoint("All", $"{baseUrl}/health/all");
     setup.AddHealthCheckEndpoint("External API Status", $"{baseUrl}/health/ext-api-status");
 }).AddInMemoryStorage();

//Apsettings.json

  "ApplicationUrl": {
    "BaseUrl": "https://www.yoursite.com"
  },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants