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
TarmoPikaro
source share