As I get it in RC2 there\'s a support for hosting applications within Windows Services. I tried to test it on a simple web api project (using .NET Framework 4.6.1).
Updated for what I Use for .netcore 2.1, using as much of the stock template as possible, which includes reading configuration from the appsettings.json, command args, host filtering etc, as implemented inside the helper methods from the .netcore team. A lot of these settings are thus controlled via config files or command args. check microsoft's documentation on what he stock template provides and how to override settings.
Please note that I have seen the service fail to start if you define an endpoint with https and the default development certificate. It may simply be a matter of specifying a pfx via configuration. If anybody wants to improve upon this, feel free to do so.
anyway here is the stock template code I use in program.cs
public class Program
{
public static void Main(string[] args)
{
if (args.Contains("--service"))
{
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Directory.SetCurrentDirectory(path); // need this because WebHost.CreateDefaultBuilder queries current directory to look for config files and content root folder. service start up sets this to win32's folder.
var host = CreateWebHostBuilder(args).Build();
host.RunAsService();
}
else
{
CreateWebHostBuilder(args).Build().Run();
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup();
}