ASP.NET Core and Application Offline

 

        Introduction:

 

                 ASP.NET 2.0 introduced a concept of application offline. This mean that when there is App_Offline.htm file in the root of a web application directory then ASP.NET will shut-down the application, unload the application domain from the server, and stop processing any new incoming requests for that application. In ASP.NET Core, there is an open-issue for supporting this feature. As of RC1, this feature is not there. but we can easily workaround this using a simple trick. In this article, I will show you how we can enable app-offline like feature using a custom middleware. But note that we are doing this in application level instead of hosting level. So, we will not shutdown the application(although there is IApplicationLifetime.StopApplication method).   

              Update: Look like the support is coming in RC2  

 

        Description:

 

                   Let create AppOfflineMiddleware class, 

 

    public class AppOfflineMiddleware
    {
        private bool _fileExist;
        private string _fileContents;
        private readonly RequestDelegate _next;

        public AppOfflineMiddleware(RequestDelegate next, IHostingEnvironment env)
        {
            if (next == null)
            {
                throw new ArgumentNullException(nameof(next));
            }
            _next = next;
            var filePath = env.WebRootPath + "\\App_Offline.htm";
            _fileExist = File.Exists(filePath);
            if (_fileExist)
            {
                _fileContents = File.ReadAllText(filePath);
            }
        }

        public async Task Invoke(HttpContext context)
        {
            if (_fileExist)
            {
                context.Response.ContentType = "text/html";
                await context.Response.WriteAsync(_fileContents);
                return;
            }
            await _next(context);
        }
    }

                    In the above middleware, we are checking whether the file exist or not in constructor(means we are doing this just once in app startup time). If you want, you can do the same in every request in Invoke method(then there will be at-least one additional IO performance overhead with each request). You can also add a timer that periodically check and set _fileExist/_FileContents variables.

                    Now we need an extension method,

 

    public static class AppOfflineExtensions
    {
        public static IApplicationBuilder UseAppOffline(this IApplicationBuilder app)
        {
            return app.UseMiddleware<AppOfflineMiddleware>();
        }
    }

 

                    Now just register this class as a first middlware,

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseAppOffline();
            .........................
            .........................

   

 

        Summary:

 

                    In this article, I showed you how easily we can add application offline like feature using a custom middleware. You can find the source at github.

No Comments