2025-07-16 09:20:13 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2025-07-31 18:51:24 +08:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2025-07-16 09:20:13 +08:00
|
|
|
|
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()
|
|
|
|
|
|
.WriteTo.Console(restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Error)
|
|
|
|
|
|
.CreateLogger();
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-31 18:51:24 +08:00
|
|
|
|
Log.Information("Starting YunDa ISMS TCP Gateway Application");
|
2025-07-16 09:20:13 +08:00
|
|
|
|
|
|
|
|
|
|
// 创建主机构建器
|
|
|
|
|
|
var host = CreateHostBuilder(args).Build();
|
|
|
|
|
|
|
|
|
|
|
|
// 运行应用程序
|
|
|
|
|
|
await host.RunAsync();
|
|
|
|
|
|
|
2025-07-31 18:51:24 +08:00
|
|
|
|
Log.Information("YunDa ISMS TCP Gateway Application stopped cleanly");
|
2025-07-16 09:20:13 +08:00
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-07-31 18:51:24 +08:00
|
|
|
|
Log.Fatal(ex, "YunDa ISMS TCP Gateway Application terminated unexpectedly");
|
2025-07-16 09:20:13 +08:00
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
Log.CloseAndFlush();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 创建主机构建器
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="args">命令行参数</param>
|
|
|
|
|
|
/// <returns>主机构建器</returns>
|
|
|
|
|
|
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
|
|
|
|
|
Host.CreateDefaultBuilder(args)
|
2025-07-31 18:51:24 +08:00
|
|
|
|
.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);
|
|
|
|
|
|
});
|
|
|
|
|
|
})
|
2025-07-16 09:20:13 +08:00
|
|
|
|
.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);
|
|
|
|
|
|
|
2025-07-31 18:51:24 +08:00
|
|
|
|
// 添加主机服务(ConsoleInterface 已在 AddISMSServices 中注册)
|
2025-07-16 09:20:13 +08:00
|
|
|
|
services.AddHostedService<ConsoleInterface>();
|
|
|
|
|
|
})
|
|
|
|
|
|
.UseSerilog()
|
|
|
|
|
|
.UseConsoleLifetime();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|