#define HandleStopStart // or ServiceOnly ServiceOrConsole using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting.WindowsServices; using System.Diagnostics; using System.IO; using System.Linq; namespace AspNetCoreService { public class Program { #if ServiceOnly #region ServiceOnly public static void Main(string[] args) { var pathToExe = Process.GetCurrentProcess().MainModule.FileName; var pathToContentRoot = Path.GetDirectoryName(pathToExe); var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(pathToContentRoot) .UseIISIntegration() .UseStartup() .UseApplicationInsights() .Build(); host.RunAsService(); } #endregion #endif #if ServiceOrConsole #region ServiceOrConsole public static void Main(string[] args) { bool isService = true; if (Debugger.IsAttached || args.Contains("--console")) { isService = false; } var pathToContentRoot = Directory.GetCurrentDirectory(); if (isService) { var pathToExe = Process.GetCurrentProcess().MainModule.FileName; pathToContentRoot = Path.GetDirectoryName(pathToExe); } var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(pathToContentRoot) .UseIISIntegration() .UseStartup() .UseApplicationInsights() .Build(); if (isService) { host.RunAsService(); } else { host.Run(); } } #endregion #endif #if HandleStopStart #region HandleStopStart public static void Main(string[] args) { bool isService = true; if (Debugger.IsAttached || args.Contains("--console")) { isService = false; } var pathToContentRoot = Directory.GetCurrentDirectory(); if (isService) { var pathToExe = Process.GetCurrentProcess().MainModule.FileName; pathToContentRoot = Path.GetDirectoryName(pathToExe); } var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(pathToContentRoot) .UseIISIntegration() .UseStartup() .UseApplicationInsights() .Build(); if (isService) { host.RunAsCustomService(); } else { host.Run(); } } #endregion #endif } }