|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Runtime.InteropServices;
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
namespace DockerStudyApi
|
|
|
{
|
|
|
public class Program
|
|
|
{
|
|
|
public static void Main(string[] args)
|
|
|
{
|
|
|
CreateHostBuilder(args).Build().Run();
|
|
|
}
|
|
|
|
|
|
public static IHostBuilder CreateHostBuilder(string[] args)
|
|
|
{
|
|
|
var hostBuilder = Host.CreateDefaultBuilder(args);
|
|
|
|
|
|
//根据操作系统设置服务
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
{
|
|
|
hostBuilder.UseWindowsService(config =>
|
|
|
{
|
|
|
//设置服务名
|
|
|
//config.ServiceName = "";
|
|
|
|
|
|
/* 注意:
|
|
|
* 一、若应用不作为Windows服务运行,则.UseWindowsService()不启使用
|
|
|
* 二、应用作为Windows服务运行时,.UseWindowsService()方法做了如下操作:
|
|
|
1、将主机生存期设置为 WindowsServiceLifetime。
|
|
|
2、将内容根设置为 AppContext.BaseDirectory。通过为 Windows 服务调用 GetCurrentDirectory 返回的当前工作目录是 C:\WINDOWS\system32 文件夹
|
|
|
3、为事件日志启用日志记录:
|
|
|
应用名称用作默认源名称。
|
|
|
对于基于 ASP.NET Core 模板且调用 CreateDefaultBuilder 生成主机的应用,默认日志级别为“警告”或更高级别。
|
|
|
在 appsettings.json/appsettings.{Environment}.json 或其他配置提供程序中,使用 Logging:EventLog:LogLevel:Default 键覆盖默认日志级别。
|
|
|
只有管理员可以创建新的事件源。 无法使用应用程序名称创建事件源时,应用程序源将记录一条警告,并禁用事件源。
|
|
|
*/
|
|
|
});
|
|
|
}
|
|
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
|
|
{
|
|
|
hostBuilder.UseSystemd();
|
|
|
}
|
|
|
|
|
|
hostBuilder
|
|
|
.ConfigureAppConfiguration((hostingContext, config) =>
|
|
|
{
|
|
|
// 作为windos服务运行时,.UseWindowsService()方法,将 ContentRootPath 设置为 AppContext.BaseDirectory
|
|
|
|
|
|
/* 通过调用 CreateDefaultBuilder during host construction,从应用的内容根加载应用的默认设置文件 appsettings.json 和 appsettings.{Environment}.json
|
|
|
ConfigureAppConfiguration 中的开发人员代码加载的其他设置文件,无需调用 SetBasePath。
|
|
|
在下面的示例中,custom_settings.json 文件位于应用的内容根,加载它时未显式设置基本路径:
|
|
|
config.AddJsonFile("custom_settings.json");
|
|
|
*/
|
|
|
|
|
|
// config.AddJsonFile("appsettings.json", false, true);
|
|
|
})
|
|
|
.ConfigureWebHostDefaults(webBuilder =>
|
|
|
{
|
|
|
webBuilder.UseStartup<Startup>();
|
|
|
});
|
|
|
|
|
|
return hostBuilder;
|
|
|
}
|
|
|
}
|
|
|
}
|