From fd18eaba5e1c086a588509371f91310e7aafff9c Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期三, 08 四月 2026 22:06:59 +0800
Subject: [PATCH] refactor: 升级多个项目目标框架至.NET 8.0
---
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Program.cs | 102 ++++++++++++++++++++++++++++++++++----------------
1 files changed, 69 insertions(+), 33 deletions(-)
diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Program.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Program.cs
index 29d6719..9bd9909 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Program.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Server/Program.cs
@@ -1,8 +1,7 @@
-using System.Reflection;
-using System.Text;
-using Autofac;
+锘縰sing Autofac;
using Autofac.Core;
using Autofac.Extensions.DependencyInjection;
+using Magicodes.ExporterAndImporter.Excel.Utility.TemplateExport;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
@@ -11,6 +10,12 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
+using Serilog;
+using Serilog.Events;
+using System.IO;
+using System.Reflection;
+using System.Text;
+using WIDESEA_Core;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Authorization;
using WIDESEAWCS_Core.BaseServices;
@@ -23,57 +28,85 @@
using WIDESEAWCS_QuartzJob;
using WIDESEAWCS_QuartzJob.QuartzExtensions;
using WIDESEAWCS_QuartzJob.Seed;
+using WIDESEAWCS_RedisService.Extensions;
using WIDESEAWCS_Server.Filter;
using WIDESEAWCS_Server.HostedService;
using WIDESEAWCS_Tasks.SocketServer;
using WIDESEAWCS_WCSServer.Filter;
var builder = WebApplication.CreateBuilder(args);
-// 1、配置服务容器
+// 绗�1閮ㄥ垎锛氫富鏈轰笌鍩虹璁炬柦閰嶇疆
+
+// 浣跨敤 Serilog 浣滀负缁熶竴鏃ュ織鎻愪緵绋嬪簭锛堟浛鎹㈤粯璁� Logging Providers锛�
+builder.Logging.ClearProviders();
+builder.Host.UseSerilog((context, services, loggerConfiguration) =>
+{
+ // 閰嶇疆Serilog鏃ュ織璁板綍鍣�
+ loggerConfiguration
+ .ReadFrom.Configuration(context.Configuration) // 浠庡簲鐢ㄧ▼搴忛厤缃腑璇诲彇Serilog鐩稿叧璁剧疆锛堝appsettings.json锛�
+ .ReadFrom.Services(services) // 浠庝緷璧栨敞鍏ュ鍣ㄤ腑璇诲彇鏈嶅姟閰嶇疆锛屽厑璁稿湪閰嶇疆涓娇鐢ㄥ凡娉ㄥ唽鐨勬湇鍔�
+ .Enrich.FromLogContext() // 鍚敤鏃ュ織涓婁笅鏂囷紝鍙互鍦ㄦ棩蹇椾腑鍖呭惈濡傝姹侷D銆佺敤鎴稩D绛夊姩鎬佸睘鎬�
+ .Enrich.WithProperty("Application", "WCS")
+ // 璁剧疆Microsoft鍛藉悕绌洪棿鐨勬棩蹇楃骇鍒负Information
+ // 杩欐牱鍙互鍑忓皯Microsoft妗嗘灦鏈韩鐨勮缁嗘棩蹇楋紝閬垮厤杩囧鐨凞ebug鏃ュ織
+ .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
+ .WriteTo.Console() // 娣诲姞鎺у埗鍙拌緭鍑烘帴鏀跺櫒锛屾棩蹇楀皢鏄剧ず鍦ㄦ帶鍒跺彴绐楀彛涓�
+ // 娣诲姞鏂囦欢杈撳嚭鎺ユ敹鍣紝灏嗘棩蹇楀啓鍏ユ枃浠剁郴缁�
+ .WriteTo.File(
+ /*Path.Combine(AppContext.BaseDirectory, "Logs", "serilog-.log"),*/ // 鎸囧畾鏃ュ織鏂囦欢鐨勫畬鏁磋矾寰勶細搴旂敤绋嬪簭鐩綍 + "Log"鏂囦欢澶� + "serilog-鏃ユ湡.log"
+ "logs/serilog-.log",
+ rollingInterval: RollingInterval.Day, // 璁剧疆鏃ュ織鏂囦欢鎸夊ぉ婊氬姩锛屾瘡澶╃敓鎴愪竴涓柊鐨勬棩蹇楁枃浠� U1od4UGVsIKZG39S5Yak
+ retainedFileCountLimit: 30, // 鏈�澶氫繚鐣欐渶杩�30澶╃殑鏃ュ織鏂囦欢锛岃秴杩�30澶╃殑鏂囦欢浼氳嚜鍔ㄥ垹闄�
+ shared: true) // 鍏佽澶氫釜杩涚▼鍚屾椂鍐欏叆鍚屼竴涓棩蹇楁枃浠讹紝閫傜敤浜庡瀹炰緥閮ㄧ讲鍦烘櫙
+ .WriteTo.Seq(
+ serverUrl: "http://localhost:5341",
+ apiKey: "U1od4UGVsIKZG39S5Yak", // 濡係eq闇�瑕丄piKey鍒欓厤缃湡瀹炲瘑閽�
+ batchPostingLimit: 1000, // 鎵归噺鍙戦�佹暟閲�
+ period: TimeSpan.FromSeconds(2) // 鍙戦�侀棿闅�
+ );
+});
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()).ConfigureContainer<ContainerBuilder>(builder =>
{
- builder.RegisterModule(new AutofacModuleRegister());//带有接口层的服务注入
+ // 娉ㄥ唽搴旂敤灞� Autofac 妯″潡锛堜粨鍌ㄣ�佹湇鍔°�丄OP 绛夛級
+ builder.RegisterModule(new AutofacModuleRegister());
builder.RegisterModule(new QuartzJobAutofacModuleRegister());
- builder.RegisterModule<AutofacPropertityModuleReg>();//
+ builder.RegisterModule<AutofacPropertityModuleReg>();
}).ConfigureAppConfiguration((hostingContext, config) =>
{
+ // 浣跨敤缁熶竴閰嶇疆鍏ュ彛骞跺彧淇濈暀 appsettings.json
hostingContext.Configuration.ConfigureApplication();
config.Sources.Clear();
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false);
});
builder.ConfigureApplication();
-// 2、配置服务
-builder.Services.AddSingleton(new AppSettings(builder.Configuration));//注册
-builder.Services.AddAllOptionRegister();//读取配置文件
+// 绗�2閮ㄥ垎锛氭湇鍔℃敞鍐�
+builder.Services.AddSingleton(new AppSettings(builder.Configuration)); // 娉ㄥ唽閰嶇疆璁块棶瀵硅薄
+builder.Services.AddAllOptionRegister(); // 娉ㄥ唽 Options 缁戝畾
+builder.Services.AddMemoryCacheSetup(); // 鍐呭瓨缂撳瓨
+builder.Services.AddRedisSetup(builder.Configuration); // Redis 缂撳瓨
+builder.Services.AddHostedService<ApiRouteCacheWarmupHostedService>(); // API璺敱缂撳瓨棰勭儹锛圧edis灏辩华鍚庡姞杞斤級
+builder.Services.AddSqlsugarSetup(); // SqlSugar 鏁版嵁搴撻厤缃�
+builder.Services.AddInitializationHostServiceSetup(); // 搴旂敤鍒濆鍖栫浉鍏冲悗鍙版湇鍔�
+builder.Services.AddHostedService<SeedDataHostedService>(); // 鍚姩鏃剁瀛愭暟鎹垵濮嬪寲
-builder.Services.AddMemoryCacheSetup();//缓存
-builder.Services.AddSqlsugarSetup();//SqlSugar 启动服务
-builder.Services.AddInitializationHostServiceSetup();//应用初始化服务注入
-builder.Services.AddHostedService<SeedDataHostedService>();//初始化数据库
+builder.Services.AddDbSetup(); // 鏁版嵁搴撳熀纭�閰嶇疆
-builder.Services.AddDbSetup();//Db 启动服务
-
-builder.Services.AddScoped<QuartzJobCreateDataTabel>();//任务调度 注入创建QuartzJob数据库表类
-builder.Services.AddHostedService<QuartzJobDataTableHostedService>();//任务调度 映射QuartzJob数据库表
+builder.Services.AddScoped<QuartzJobCreateDataTabel>(); // Quartz 浠诲姟琛ㄥ垱寤烘湇鍔�
+builder.Services.AddHostedService<QuartzJobDataTableHostedService>(); // Quartz 浠诲姟琛ㄥ垵濮嬪寲鍚庡彴鏈嶅姟
builder.Services.AddWebSocketSetup();
-
-builder.Services.AddAutoMapperSetup();
-
+builder.Services.AddMapsterSetup(); // Mapster 瀵硅薄鏄犲皠閰嶇疆
builder.Services.AddCorsSetup();
-
builder.Services.AddMiniProfilerSetup();
-
builder.Services.AddSwaggerSetup();
-
-builder.Services.AddJobSetup();//任务调度 注入反射获取依赖对象
+builder.Services.AddJobSetup(); // Quartz Job 璋冨害閰嶇疆
builder.Services.AddHttpContextSetup();
-builder.Services.AddHostedService<QuartzJobHostedService>();//任务调度 启动服务
+builder.Services.AddHostedService<QuartzJobHostedService>(); // Quartz 鍚姩鎵樼鏈嶅姟
builder.Services.AddSingleton<TcpSocketServer>();
builder.Services.AddHostedService<SocketServerHostedService>();
@@ -84,9 +117,11 @@
options.Filters.Add(typeof(CustomAuthorizeFilter));
});
+builder.Services.AddScoped<HttpClientHelper>();
+
builder.Services.AddAuthorizationSetup();
-builder.Services.AddIpPolicyRateLimitSetup(builder.Configuration);//IPLimit限流 启动服务
+builder.Services.AddIpPolicyRateLimitSetup(builder.Configuration); // IP 闄愭祦閰嶇疆
builder.Services.AddScoped<UseServiceDIAttribute>();
@@ -96,7 +131,7 @@
builder.Services.AddControllers(o =>
{
- o.Filters.Add(typeof(GlobalExceptionsFilter));//全局异常
+ o.Filters.Add(typeof(GlobalExceptionsFilter)); // 鍏ㄥ眬寮傚父杩囨护鍣�
})
.AddNewtonsoftJson(options =>
{
@@ -108,26 +143,26 @@
options.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local;
options.SerializerSettings.Converters.Add(new StringEnumConverter());
});
-// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
+// 浣跨敤 DI 婵�娲� Controller锛岀‘淇濆睘鎬ф敞鍏�/瀹瑰櫒琛屼负涓�鑷�
builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var app = builder.Build();
-// 3、配置中间件
-app.UseMiniProfiler();//性能分析器
-app.ConfigureApplication();//配置文件
-app.UseApplicationSetup();//启动配置
+// 绗�3閮ㄥ垎锛欻TTP 绠¢亾閰嶇疆
+app.UseMiniProfiler(); // 鎬ц兘鍒嗘瀽闈㈡澘
+app.ConfigureApplication(); // 搴旂敤鍏ㄥ眬鍒濆鍖�
+app.UseApplicationSetup(); // 鍏叡涓棿浠跺垵濮嬪寲
app.UseAllServicesMiddle(builder.Services);
app.UseSession();
app.UseSwaggerAuthorized();
-app.UseSwaggerMiddle(() => Assembly.GetExecutingAssembly().GetManifestResourceStream("WIDESEAWCS_Server.index.html") ?? throw new Exception("未找到WIDESEAWCS_Server.index.html文件"));
+app.UseSwaggerMiddle(() => Assembly.GetExecutingAssembly().GetManifestResourceStream("WIDESEAWCS_Server.index.html") ?? throw new Exception("鏈壘鍒� WIDESEAWCS_Server.index.html 璧勬簮鏂囦欢"));
app.UseIpLimitMiddle();
app.UseApiLogMiddleware();
@@ -156,3 +191,4 @@
app.MapControllers();
app.Run();
+
--
Gitblit v1.9.3