From 96adc295cb04fd135d63d3a907f2732274f90965 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期二, 21 四月 2026 01:11:21 +0800
Subject: [PATCH] feat: 添加MES异步上传辅助服务并重构相关代码

---
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/JobFactory.cs |   48 ++++++++++++++++++++++++++++++++++++------------
 1 files changed, 36 insertions(+), 12 deletions(-)

diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/JobFactory.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/JobFactory.cs
index 224be01..3d6e525 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/JobFactory.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/JobFactory.cs
@@ -19,6 +19,7 @@
 using Quartz.Spi;
 using Quartz;
 using System;
+using System.Collections.Concurrent;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -28,7 +29,7 @@
 namespace WIDESEAWCS_QuartzJob
 {
     /// <summary>
-    /// Job娉ㄥ叆
+    /// Job娉ㄥ叆宸ュ巶锛岀鐞� Job 瀹炰緥鐨� DI 浣滅敤鍩熺敓鍛藉懆鏈�
     /// </summary>
     public class JobFactory : IJobFactory
     {
@@ -38,48 +39,71 @@
         private readonly IServiceProvider _serviceProvider;
 
         /// <summary>
+        /// Job 瀹炰緥涓庡搴旂殑 DI 浣滅敤鍩熸槧灏勶紝鐢ㄤ簬鍦� ReturnJob 鏃舵纭噴鏀捐祫婧�
+        /// </summary>
+        private readonly ConcurrentDictionary<IJob, IServiceScope> _scopes = new();
+
+        /// <summary>
         /// Job娉ㄥ叆
         /// </summary>
-        /// <param name="serviceProvider"></param>
+        /// <param name="serviceProvider">鏈嶅姟鎻愪緵鑰�</param>
         public JobFactory(IServiceProvider serviceProvider)
         {
             _serviceProvider = serviceProvider;
         }
 
         /// <summary>
-        /// 瀹炵幇鎺ュ彛Job
+        /// 鍒涘缓 Job 瀹炰緥锛屽苟涓烘瘡涓� Job 鍒涘缓鐙珛鐨� DI 浣滅敤鍩�
         /// </summary>
-        /// <param name="bundle"></param>
-        /// <param name="scheduler"></param>
-        /// <returns></returns>
+        /// <param name="bundle">瑙﹀彂鍣ㄨЕ鍙戜笂涓嬫枃</param>
+        /// <param name="scheduler">璋冨害鍣ㄥ疄渚�</param>
+        /// <returns>Job 瀹炰緥</returns>
         public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
         {
             try
             {
-                if (App.ExpDateTime != null && (DateTime.Now - App.ExpDateTime.GetValueOrDefault()).Seconds > 0)
+                // 楠岃瘉鏈夋晥鏈燂紝浣跨敤 TotalSeconds 閬垮厤 .Seconds 鍙栨ā瀵艰嚧闂存瓏鎬у垽鏂敊璇�
+                if (App.ExpDateTime != null && (DateTime.Now - App.ExpDateTime.GetValueOrDefault()).TotalSeconds > 0)
                 {
                     throw new InvalidOperationException($"楠岃瘉閿欒");
                 }
 
+                // 涓烘瘡涓� Job 鍒涘缓鐙珛鐨� DI 浣滅敤鍩燂紝纭繚 Scoped 鏈嶅姟姝g‘閲婃斁
                 IServiceScope serviceScope = _serviceProvider.CreateScope();
                 IJob? job = serviceScope.ServiceProvider.GetService(bundle.JobDetail.JobType) as IJob;
+
+                if (job == null)
+                {
+                    // Job 瑙f瀽澶辫触鏃剁珛鍗抽噴鏀句綔鐢ㄥ煙锛岄伩鍏嶆硠婕�
+                    serviceScope.Dispose();
+                    throw new InvalidOperationException($"鏃犳硶瑙f瀽 Job 绫诲瀷: {bundle.JobDetail.JobType.Name}");
+                }
+
+                // 淇濆瓨 scope 寮曠敤锛屼互渚� ReturnJob 鏃堕噴鏀�
+                _scopes[job] = serviceScope;
                 return job;
             }
             catch (Exception ex)
             {
                 Console.Out.WriteLine(ex.ToString());
-                throw new Exception(ex.Message);
+                throw;
             }
         }
 
         /// <summary>
-        /// Job娉ㄥ叆
+        /// 閲婃斁 Job 瀹炰緥鍙婂叾鍏宠仈鐨� DI 浣滅敤鍩�
         /// </summary>
-        /// <param name="job"></param>
+        /// <param name="job">寰呴噴鏀剧殑 Job 瀹炰緥</param>
         public void ReturnJob(IJob job)
         {
-            IDisposable? disposable = job as IDisposable;
-            disposable?.Dispose();
+            // 鍏堥噴鏀惧叧鑱旂殑 DI 浣滅敤鍩燂紙鍖呮嫭鍏朵腑鎵�鏈� Scoped 鏈嶅姟锛�
+            if (_scopes.TryRemove(job, out IServiceScope? scope))
+            {
+                scope.Dispose();
+            }
+
+            // 鍐嶉噴鏀� Job 鏈韩
+            (job as IDisposable)?.Dispose();
         }
     }
 }

--
Gitblit v1.9.3