87 lines
2.9 KiB
C#
87 lines
2.9 KiB
C#
|
|
using System;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
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
|
||
|
|
{
|
||
|
|
Log.Information("Starting YunDa ISMS Server Console Application");
|
||
|
|
|
||
|
|
// 创建主机构建器
|
||
|
|
var host = CreateHostBuilder(args).Build();
|
||
|
|
|
||
|
|
// 运行应用程序
|
||
|
|
await host.RunAsync();
|
||
|
|
|
||
|
|
Log.Information("YunDa ISMS Server Console Application stopped cleanly");
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
catch (Exception ex)
|
||
|
|
{
|
||
|
|
Log.Fatal(ex, "YunDa ISMS Server Console Application terminated unexpectedly");
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
finally
|
||
|
|
{
|
||
|
|
Log.CloseAndFlush();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 创建主机构建器
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="args">命令行参数</param>
|
||
|
|
/// <returns>主机构建器</returns>
|
||
|
|
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||
|
|
Host.CreateDefaultBuilder(args)
|
||
|
|
.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);
|
||
|
|
|
||
|
|
// 添加主机服务
|
||
|
|
services.AddHostedService<ISMSServerApplication>();
|
||
|
|
services.AddHostedService<ConsoleInterface>();
|
||
|
|
})
|
||
|
|
.UseSerilog()
|
||
|
|
.UseConsoleLifetime();
|
||
|
|
}
|
||
|
|
}
|