Last active
August 4, 2020 15:01
-
-
Save mapfel/7fd93997bc2455b4cd9df8949d883716 to your computer and use it in GitHub Desktop.
ASP.NET Core - Run vs. RunAsService
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://dotnetthoughts.net/how-to-host-your-aspnet-core-in-a-windows-service/ | |
public static void Main(string[] args) | |
{ | |
var host = new WebHostBuilder() | |
.UseIISIntegration() | |
.UseKestrel() | |
.UseContentRoot(@"Path\To\Content\Root") | |
.UseStartup<Startup>() | |
.Build(); | |
if (Debugger.IsAttached || args.Contains("--debug")) | |
{ | |
host.Run(); | |
} | |
else | |
{ | |
host.RunAsService(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.c-sharpcorner.com/article/host-an-asp-net-core-application-as-a-windows-service/ | |
public static void Main(string[] args) | |
{ | |
var pathToExe = Process.GetCurrentProcess().MainModule.FileName; | |
var pathToContentRoot = Path.GetDirectoryName(pathToExe); | |
IWebHost host; | |
if (Debugger.IsAttached || args.Contains("console")) | |
{ | |
Debugger.Launch(); // benefit of that? | |
host = WebHost.CreateDefaultBuilder() | |
.UseContentRoot(Directory.GetCurrentDirectory()) | |
.UseStartup<Startup>() | |
.Build(); | |
host.Run(); | |
} | |
else | |
{ | |
host = WebHost.CreateDefaultBuilder(args) | |
.UseContentRoot(pathToContentRoot) | |
.UseStartup<Startup>() | |
.Build(); | |
host.RunAsService(); | |
} | |
} | |
// start with: dotnet run console |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/aspnet/Hosting/issues/1338 | |
public static void Main(string[] args) | |
{ | |
var config = new ConfigurationBuilder() | |
.SetBasePath(Directory.GetCurrentDirectory()) | |
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) // necessary to include? | |
.AddJsonFile("hosting.json", optional: false, reloadOnChange: true) // what is the deeper meaning of optional? | |
.AddCommandLine(args) | |
.Build(); | |
if (Debugger.IsAttached || args.Contains("--console")) | |
BuildWebHost(config, args).Run(); | |
else | |
BuildServiceWebHost(config, args).RunAsService(); | |
} | |
public static IWebHost BuildWebHost(IConfigurationRoot config, string[] args) | |
{ | |
return WebHost.CreateDefaultBuilder(args) | |
.UseConfiguration(config) | |
.UseStartup<Startup>() | |
.Build(); | |
} | |
public static IWebHost BuildServiceWebHost(IConfigurationRoot config, string[] args) | |
{ | |
var pathToExe = Process.GetCurrentProcess().MainModule.FileName; | |
var pathToContentRoot = Path.GetDirectoryName(pathToExe); | |
var webHostArgs = args.Where(arg => arg != "--console").ToArray(); // nice filtering! | |
return WebHost.CreateDefaultBuilder(webHostArgs) | |
.UseConfiguration(config) | |
.UseContentRoot(pathToContentRoot) | |
.UseStartup<Startup>() | |
.Build(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment