Installation
Install AlmightyShogun.AspNet.MaintenanceMode in the ASP.NET Core application that needs to block traffic during maintenance windows. The package targets net10.0 and requires the ASP.NET Core shared framework.
dotnet add package AlmightyShogun.AspNet.MaintenanceModeDependencies
Framework references
Microsoft.AspNetCore.App— middleware, HTTP, and hosting APIs.
Project references
AlmightyShogun.AspNet.Core— supplies the shared error response writer, so a blocked request returns the same error body as every other error in the stack.AlmightyShogun.Utils— binds and validates theMaintenancesection at startup.
Startup Registration
AddMaintenanceMode binds the optional Maintenance section and registers IMaintenanceService, which application code uses to turn maintenance on and off. UseMaintenanceMode adds the middleware that blocks traffic while a window is active, writing each blocked request through AddHttpErrorResponseWriter.
WARNING
UseMaintenanceMode must come before endpoint mapping, and after any middleware that has to run while the application is offline. AddHttpErrorResponseWriter is not optional: the middleware takes IHttpErrorResponseWriter as a constructor dependency, so without it the first blocked request fails to resolve.
using AlmightyShogun.AspNet.Core;
using AlmightyShogun.AspNet.MaintenanceMode;
builder.Services
.AddHttpErrorResponseWriter()
.AddMaintenanceMode(builder.Configuration);
WebApplication app = builder.Build();
app.UseMaintenanceMode();