Skip to content

rsvilenov/MaintenanceModeMiddleware

Repository files navigation

Maintenance Mode Middleware

build workflow Coverage Status License NuGet

Adds support for maintenance mode in an ASP.NET Core application

Supports ASP.NET Core 3.1 and 5.0

Table of Contents

General

This component consists of a middleware, which does the actual handling of requests, and a control service, which is used to turn the maintenance mode on and off.

Key features:

  • Enter and exit maintenance mode by using an injectable control service.
  • Specify an expiration date on which maintenance mode should turn off automatically.
  • Let certain parts of the site remain accessibe while maintenance mode is on.
  • Let certain users (e.g. admins) still be able to access the entire site.
  • Configure the component globally (in Startup.cs), or...
  • Specify a different configuration every time you put the application in maintenance mode.
  • Customize your maintenance mode response (html, text and json files/data are supported).
  • Don't worry about SEO problems - the component sends response code 503 with a "Retry-After" interval.

Installation

You can view the package page on NuGet.

To install MaintenanceModeMiddleware, run the following command in the Package Manager Console:

PM> Install-Package MaintenanceModeMiddleware

Or use this command with the .NET CLI:

> dotnet add package MaintenanceModeMiddleware

Registration

  • Register the middleware in Startup.cs:
using MaintenanceModeMiddleware.Extensions;

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
    app.UseMaintenance(options =>
    {
        options.BypassUserRole("Admin");
    });

    app.UseEndpoints(endpoints =>
    {        
...
}

ℹ️ Since this is a middleware, the order of registration is important. In order for you to be able to profit from the entire set of features of this middleware, it is highly recommended that you put its registration just before app.UseEndpoints(..);.

  • Register the control service in Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
   ...
   services.AddMaintenance();
}

You can pass options to the control service as well. For example, you can specify that the maintenance state should be preserved after the app has been restarted. By default the state is stored in a json file. If you want to store the state somewhere else, for example in the database, you can pass your own implementation of the IStateStore interface to options.UseStateStore<YourStateStore>().

Basic use

Inject the control service in the controller, from which you want to trigger the maintenance mode:

using MaintenanceModeMiddleware.Services;

private readonly IMaintenanceControlService _maintenanceCtrlSvc;

public AdminController(IMaintenanceControlService maintenanceCtrlSvc)
{
   _maintenanceCtrlSvc = maintenanceCtrlSvc;
}

Then just call its methods from the controller actions:

[HttpPost]
public IActionResult ToggleMaintenanceMode()
{
    var maintenanceState = _maintenanceCtrlSvc.GetState();

    if (maintenanceState.IsMaintenanceOn)
    {
        _maintenanceCtrlSvc.LeaveMaintanence();
    }
    else
    {
        _maintenanceCtrlSvc.EnterMaintanence();
    }

    return RedirectToAction(nameof(Index));
}

ℹ️ Note: The identity area remains accessible in maintenance mode so that you can log in with the users, for which the site should be visible. You can block the access to this area as well. Read how to do that in the full documentation.

Options

All the available options are described in detail here.