AspNetCore.Docs/aspnetcore/hosting/windows-service/sample/Program.cs

105 lines
2.8 KiB
C#

#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<Startup>()
.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<Startup>()
.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<Startup>()
.UseApplicationInsights()
.Build();
if (isService)
{
host.RunAsCustomService();
}
else
{
host.Run();
}
}
#endregion
#endif
}
}