59

How can I restart an app in asp.net core programmatically?

I want to clear cache and cause the application to re-enter the startup.

4
  • Just to understand better, why do you need this? Mar 21, 2016 at 8:07
  • 4
    I think the most common scenario for me is manipulating settings for the app via some administrative view. The settings are often things initiated on startup.
    – Jeff Putz
    Sep 14, 2016 at 22:36
  • 3
    another usage I can think of is to publish a new version and restart
    – Frank Rem
    Dec 9, 2017 at 11:39
  • 3
    To clear static cache? This seems like a totally legit requirement to me.
    – iGanja
    Apr 25, 2019 at 19:49

6 Answers 6

40

Before you read my answer: This solution is going to stop the app and cause the application to re-enter the startup in the next request.

.NET Core 2 There may come a time when you wish to force your ASP.Net Core 2 site to recycle programmatically. Even in MVC/WebForms days this wasn't necessarily a recommended practice but alas, there is a way. ASP.Net Core 2 allows for the injection of an IApplicationLifetime object that will let you do a few handy things. First, it will let you register events for Startup, Shutting Down and Shutdown similar to what might have been available via a Global.asax back in the day. But, it also exposes a method to allow you to shutdown the site (without a hack!). You'll need to inject this into your site, then simply call it. Below is an example of a controller with a route that will shutdown a site.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace MySite.Controllers
{
    public class WebServicesController : Controller
    {
        private IApplicationLifetime ApplicationLifetime { get; set; }

        public WebServicesController(IApplicationLifetime appLifetime)
        {
            ApplicationLifetime = appLifetime;
        }

        public async Task ShutdownSite()
        {
            ApplicationLifetime.StopApplication();
            return "Done";
        }

    }
}

Source: http://www.blakepell.com/asp-net-core-ability-to-restart-your-site-programatically-updated-for-2-0

4
  • I'm doing just like you said but it doesn't shutdown !!
    – niceman
    May 2, 2018 at 11:38
  • 1
    It stops the app but, after the first request the app comes up again. This is just for restarting the application not to shut it down forever. May 3, 2018 at 5:56
  • 5
    I tried this, and it stops the application and does NOT restart it (which I kind of expected, based on the method name)
    – Mani5556
    Nov 13, 2019 at 20:33
  • 10
    I'm down voting because ApplicationLifetime.StopApplication(); just stops the application, it doesn't restart it Apr 9, 2020 at 13:15
35

Update: Mirask's answer is more correct for .NET Core 2.

In Program.cs you will see the call to host.Run(). This method has an overload which accepts a System.Threading.CancellationToken. This is what I am doing:

public class Program {

    private static CancellationTokenSource cancelTokenSource = new System.Threading.CancellationTokenSource();

    public static void Main(string[] args) {

        var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

        host.Run(cancelTokenSource.Token);
    }

    public static void Shutdown() {
        cancelTokenSource.Cancel();
    }
}

Then, in my Controller I can call Program.Shutdown() and after a few seconds the application dies. If it is behind IIS, another request will automatically start the application.

8
  • This is absolutely perfect, thank you. In regards to handling this when hosted by IIS, this is much better than adding a reference to Microsoft.Web.Administration or tinkering with the web.config file to trick an IIS recycle. Jun 29, 2017 at 14:53
  • 8
    Small update. I found that the method Run(token) no longer is availible to me in .NET Core 2.0. I used the following line to work around this: .RunAsync(cancellationTokenSource.Token).GetAwaiter().GetResult(); Nov 6, 2017 at 10:16
  • Thanks, I was wondering how to do that since the update. You should make this an answer.
    – Chet
    Nov 6, 2017 at 11:37
  • @Schwarzie2478, @Chet did you try to use IApplicationLifetime.ApplicationStopping - it looks like it would be enough to call Cancel on it to start shutdown. Dec 8, 2017 at 13:49
  • I just tested this and you could indeed use IApplicationLifetime.StopApplication(). Do you want to add this as an answer?
    – Chet
    Dec 13, 2017 at 15:57
24

ASP.NET Core 3+

Since the accepted answer is using IApplicationLifetime which became obsolete in ASP.NET Core 3 onwards, the new recommended way is to use IHostApplicationLifetime which is located in the Microsoft.Extensions.Hosting namespace.

In my Blazor application, I can use following code:

@inject IHostApplicationLifetime AppLifetime

<button @onclick="() => AppLifetime.StopApplication()">Restart</button>
3
  • Hi @WΩLLE is it possible to do it from the backend ? i tried but it give me an NullReference exception ... maybe i did it wrong... my app is using too much cache and i want to restart the app because of that but dont know how to release the cache by code... is it possible to use your solution?
    – Khalid Ab
    Jul 5, 2021 at 15:21
  • Ths shuts down the app but the question was how to restart it and that, is not accomplished with this. In fact, I don't even think is possible because once the app is down, it looses the connection with the client, showing an error. Only a new request will restart the app.
    – Hugo A.
    Apr 14, 2022 at 13:51
  • @HugoA. Do you want to restart the application and keep all connections alive? This sounds like an impossible requirement.
    – ˈvɔlə
    Apr 14, 2022 at 16:00
5

For .NET Core 2.2 you can use following code:

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using System.Threading;

namespace BuildMonitor
{
    public class Program
    {
        private static CancellationTokenSource cancelTokenSource = new System.Threading.CancellationTokenSource();

        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();
            host.RunAsync(cancelTokenSource.Token).GetAwaiter().GetResult();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();

        public static void Shutdown()
        {
            cancelTokenSource.Cancel();
        }
    }
}

And server shutdown could be placed for example behind some web page:

using System;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace BuildMonitor.Pages
{
    public class StopServerModel : PageModel
    {
        public void OnGet()
        {
            Console.WriteLine("Forcing server shutdown.");
            Program.Shutdown();
        }
    }
}

stopServer.bat could be for example like this:

@echo off
rem curl http://localhost:5000/StopServer >nul 2>&1
powershell.exe -Command (new-object System.Net.WebClient).DownloadString('http://localhost:5000/StopServer') >nul
exit /b 0
2
  • RunAsync method expects a CancellationToken object instead of CancellationTokenSource, so this code doesn't work for me Jun 13, 2019 at 17:38
  • it focuses into token ? cancelTokenSource.Token ? Jun 14, 2019 at 20:45
4

None of the solutions above did what I wanted. So that is what I came up with:

 public class Program
{
    private static CancellationTokenSource cts = new CancellationTokenSource();
    private static string[] _args;
    private static bool _restartRequest;

    public static async Task Main(string[] args)
    {
        _args = args;

        await StartServer();
        while (_restartRequest)
        {
            _restartRequest = false;
            Console.WriteLine("Restarting App");
            await StartServer();
        }
    }

    public static void Restart()
    {
        _restartRequest = true;
        cts.Cancel();
    }

    private static async Task StartServer()
    {
        try
        {
            cts = new CancellationTokenSource();
            await CreateHostBuilder(_args).RunConsoleAsync(cts.Token);
        }
        catch (OperationCanceledException e)
        {
            Console.WriteLine(e);
        }
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}
1
  • I used your code and it works practically. Also I called StopApplication before Restart method. Did you use it in production environment ? Nov 3, 2021 at 10:19
2

If you need this just for a development scenario then you can use dotnet-watch(for dotnet) or dnx-watch(for dnx).

If you want your application to restart in production, then you have to implement something similar to what the watcher does. You need an external process to kill and restart the process. Or you need your app to launch an instance of itself and then kill itself. Unfortunately, there's nothing out of the box for this.

1
  • 5
    It would be a hack, but can the app programmatically change a config file that would trigger an app restart?
    – Jeff Putz
    Sep 15, 2016 at 0:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.