Skip to content

Instantly share code, notes, and snippets.

@alanta
Created May 14, 2020 09:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanta/e38948ddd742335237ae85914e4d935d to your computer and use it in GitHub Desktop.
Save alanta/e38948ddd742335237ae85914e4d935d to your computer and use it in GitHub Desktop.
Azure Web Apps with Always On enabled poll your web app every 5 minutes. If there's nothing listening on that url this results in a 404 response which shows up as an error in monitoring.
public static class AlwaysOn
{
/// <summary>
/// Prevent 404 on the application root URL. Azure Web Apps with Always On enabled invoke the root of the application every 5 minutes
/// to keep your web app awake. If you run an API-only app you likely have nothing listening to /, which results in a 404 response
/// that shows up in monitoring. This endpoint returns 200 with content Ok only for the always on check.
/// </summary>
/// <param name="builder">The application builder.</param>
/// <returns>The application builder.</returns>
public static IApplicationBuilder UseAlwaysOn(this IApplicationBuilder builder)
{
builder.Use(next => async context =>
{
if (context.Request.Path == "/" && context.Request.Headers.TryGetValue("user-agent", out var value) && value.Equals("AlwaysOn"))
{
await context.Response.WriteAsync("Ok");
return;
}
await next(context);
});
return builder;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment