105 lines
3.9 KiB
C#
105 lines
3.9 KiB
C#
using System;
|
||
using System.Threading.Tasks;
|
||
using Microsoft.AspNetCore.Hosting;
|
||
using Microsoft.Extensions.Configuration;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using Microsoft.Extensions.Hosting;
|
||
using Microsoft.Extensions.Logging;
|
||
using Serilog;
|
||
using YunDa.Server.ISMSTcp.Extensions;
|
||
using YunDa.Server.ISMSTcp.Services;
|
||
|
||
namespace YunDa.Server.ISMSTcp
|
||
{
|
||
|
||
/// <summary>
|
||
/// 程序入口点
|
||
/// </summary>
|
||
public class Program
|
||
{
|
||
/// <summary>
|
||
/// 主入口方法
|
||
/// </summary>
|
||
/// <param name="args">命令行参数</param>
|
||
/// <returns>退出代码</returns>
|
||
public static async Task<int> Main(string[] args)
|
||
{
|
||
// 创建最小日志记录器用于启动阶段
|
||
Log.Logger = new LoggerConfiguration()
|
||
.MinimumLevel.Information()
|
||
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
|
||
.CreateLogger();
|
||
|
||
try
|
||
{
|
||
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";
|
||
Log.Information("启动 YunDa ISMS TCP Gateway 应用程序 - 环境: {Environment}", environment);
|
||
|
||
// 创建主机构建器
|
||
var host = CreateHostBuilder(args).Build();
|
||
|
||
// 运行应用程序
|
||
await host.RunAsync();
|
||
|
||
Log.Information("YunDa ISMS TCP Gateway 应用程序正常停止");
|
||
return 0;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Fatal(ex, "YunDa ISMS TCP Gateway 应用程序异常终止");
|
||
return 1;
|
||
}
|
||
finally
|
||
{
|
||
// 确保所有日志都被写入
|
||
await Log.CloseAndFlushAsync();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建主机构建器
|
||
/// </summary>
|
||
/// <param name="args">命令行参数</param>
|
||
/// <returns>主机构建器</returns>
|
||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||
Host.CreateDefaultBuilder(args)
|
||
.ConfigureWebHostDefaults(webBuilder =>
|
||
{
|
||
webBuilder.UseStartup<Startup>();
|
||
webBuilder.ConfigureKestrel(options =>
|
||
{
|
||
// 从配置中读取端口
|
||
var config = new ConfigurationBuilder()
|
||
.AddJsonFile("appsettings.json")
|
||
.Build();
|
||
var port = config.GetValue<int>("WebApi:Port", 56670);
|
||
options.ListenAnyIP(port);
|
||
});
|
||
})
|
||
.ConfigureAppConfiguration((context, config) =>
|
||
{
|
||
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
|
||
config.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json",
|
||
optional: true, reloadOnChange: true);
|
||
config.AddEnvironmentVariables();
|
||
config.AddCommandLine(args);
|
||
})
|
||
.ConfigureServices((context, services) =>
|
||
{
|
||
// 添加日志服务
|
||
services.AddLoggingServices(context.Configuration);
|
||
|
||
// 添加ISMS服务
|
||
services.AddISMSServices(context.Configuration);
|
||
|
||
// 添加主机服务(ConsoleInterface 已在 AddISMSServices 中注册)
|
||
services.AddHostedService<ConsoleInterface>();
|
||
|
||
// 注册 WebApi 配置类
|
||
services.Configure<WebApiSettings>(context.Configuration.GetSection("WebApi"));
|
||
})
|
||
.UseSerilog()
|
||
.UseConsoleLifetime();
|
||
}
|
||
}
|