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
{
///
/// 程序入口点
///
public class Program
{
///
/// 主入口方法
///
/// 命令行参数
/// 退出代码
public static async Task Main(string[] args)
{
// 创建初始日志记录器 - 临时显示Information级别日志以调试WebSocket Bridge
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information,
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
.CreateLogger();
try
{
Log.Information("Starting YunDa ISMS TCP Gateway Application");
// 创建主机构建器
var host = CreateHostBuilder(args).Build();
// 运行应用程序
await host.RunAsync();
Log.Information("YunDa ISMS TCP Gateway Application stopped cleanly");
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "YunDa ISMS TCP Gateway Application terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}
///
/// 创建主机构建器
///
/// 命令行参数
/// 主机构建器
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup();
webBuilder.ConfigureKestrel(options =>
{
// 从配置中读取端口
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var port = config.GetValue("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();
})
.UseSerilog()
.UseConsoleLifetime();
}
}