How to restart asp.net software application?

How can I restart the application in the asp.net core programmatically?

I want to clear the cache and force the application to re-enter the launch.

+29
asp.net-core
source share
5 answers

.NET Core 2

There may come a time when you want to force-restart your ASP.Net Core 2 site programmatically. Even in the days of MVC / WebForms, this was not the recommended practice, but alas, there is a way. ASP.Net Core 2 allows IApplicationLifetime to IApplicationLifetime an IApplicationLifetime object, which allows you to do a few useful things. Firstly, it will allow you to log events for starting, shutting down and shutting down, similar to those that could be accessed through Global.asax that day. But it also provides a method that allows you to close the site (without hacking!). You will need to paste this into your website and then just call. Below is an example of a controller with a route that will disable the 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-programatics-updated-for-2-0

+28
source share

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

In Program.cs, you will see a call to host.Run() . This method has an overload that accepts System.Threading.CancellationToken . This is what I do:

 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 launch the application.

+23
source share

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

If you want your application to restart in production, you need to implement something similar to what the observer does. You need an external process to kill and restart the process. Or do you need an application to launch an instance of yourself, and then kill yourself. Unfortunately, there is nothing to do.

+3
source share

For .NET Core 2.2, you can use the 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(); } } } 

A server shutdown can be placed, for example, behind some kind of 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(); } } } 

For example, stopServer.bat could be 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 
+3
source share

None of the above worked for me. I am stuck after the first failed launch. Perhaps because my asp.net core 2.0 application is hosted inside IIS. I had to install AppPool "AlwaysRunning" and put the migrations in a try - catch block; Now that the SQL server is down, the application closes and each query starts the application again.

 public void Configure(IApplicationBuilder app, IHostingEnvironment env) { // Some middleware stuff, etc... try { // ... db migrations and other stuff that can break startup } catch (Exception ex) { // log exception Environment.Exit(-1); } } 
0
source share

All Articles