From 3b292ba334be5cd66ff226012c9d7814f3ffae79 Mon Sep 17 00:00:00 2001 From: gcw_MV9p2JJN Date: Sun, 1 Feb 2026 09:43:58 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=B3=A8=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Infrastructure/Attribute/AppServiceAttribute.cs | 2 +- Infrastructure/WebExtensions/AppServiceExtensions.cs | 9 +++++++++ RIZO.Admin.WebApi/Program.cs | 1 - .../Stations/Into/PlcIntoStationService_OP100.cs | 2 +- .../Stations/Into/PlcIntoStationService_OP102.cs | 2 +- 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Infrastructure/Attribute/AppServiceAttribute.cs b/Infrastructure/Attribute/AppServiceAttribute.cs index f9ccd02..a33bb88 100644 --- a/Infrastructure/Attribute/AppServiceAttribute.cs +++ b/Infrastructure/Attribute/AppServiceAttribute.cs @@ -29,6 +29,6 @@ namespace Infrastructure.Attribute public enum LifeTime { - Transient, Scoped, Singleton + Transient, Scoped, Singleton, BackgroundService } } diff --git a/Infrastructure/WebExtensions/AppServiceExtensions.cs b/Infrastructure/WebExtensions/AppServiceExtensions.cs index ce694fa..b84d7fe 100644 --- a/Infrastructure/WebExtensions/AppServiceExtensions.cs +++ b/Infrastructure/WebExtensions/AppServiceExtensions.cs @@ -60,6 +60,15 @@ namespace Infrastructure case LifeTime.Transient: services.AddTransient(serviceType, type); break; + case LifeTime.BackgroundService: + // 使用反射调用 AddHostedService + var addHostedServiceMethod = typeof(ServiceCollectionHostedServiceExtensions) + .GetMethods() + .Where(m => m.Name == "AddHostedService" && m.IsGenericMethod) + .First() + .MakeGenericMethod(type); + addHostedServiceMethod.Invoke(null, new object[] { services }); + break; default: services.AddTransient(serviceType, type); break; diff --git a/RIZO.Admin.WebApi/Program.cs b/RIZO.Admin.WebApi/Program.cs index a792a19..509e580 100644 --- a/RIZO.Admin.WebApi/Program.cs +++ b/RIZO.Admin.WebApi/Program.cs @@ -113,7 +113,6 @@ builder.Services.AddLocalization(options => options.ResourcesPath = ""); //PLC进站后台服务注册 //builder.Services.AddHostedService(); //builder.Services.AddHostedService(); -builder.Services.AddHostedService(); //螺丝枪服务注册 //builder.Services.AddHostedService(); diff --git a/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_OP100.cs b/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_OP100.cs index f5f7e21..56c61aa 100644 --- a/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_OP100.cs +++ b/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_OP100.cs @@ -24,7 +24,7 @@ namespace RIZO.Service.PLCBackground.Stations.Into /// /// OP点散热胶GF1500 /// - [AppService(ServiceType = typeof(PlcIntoStationService_OP100), ServiceLifetime = LifeTime.Singleton)] + [AppService(ServiceType = typeof(PlcIntoStationService_OP100), ServiceLifetime = LifeTime.BackgroundService)] public class PlcIntoStationService_OP100 : PlcIntoStationService_Common { public PlcIntoStationService_OP100(IOptions options) : base(options) diff --git a/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_OP102.cs b/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_OP102.cs index 384fcb7..2927a35 100644 --- a/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_OP102.cs +++ b/RIZO.Service/PLCBackground/Stations/Into/PlcIntoStationService_OP102.cs @@ -24,7 +24,7 @@ namespace RIZO.Service.PLCBackground.Stations.Into /// /// OP点散热胶GF1500 /// - [AppService(ServiceType = typeof(PlcIntoStationService_OP102), ServiceLifetime = LifeTime.Singleton)] + [AppService(ServiceType = typeof(PlcIntoStationService_OP102), ServiceLifetime = LifeTime.BackgroundService)] public class PlcIntoStationService_OP102 : PlcIntoStationService_Common { public PlcIntoStationService_OP102(IOptions options) : base(options)