调整注入

This commit is contained in:
gcw_MV9p2JJN 2026-02-01 09:43:58 +08:00
parent 3992bb3f9e
commit 3b292ba334
5 changed files with 12 additions and 4 deletions

View File

@ -29,6 +29,6 @@ namespace Infrastructure.Attribute
public enum LifeTime
{
Transient, Scoped, Singleton
Transient, Scoped, Singleton, BackgroundService
}
}

View File

@ -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;

View File

@ -113,7 +113,6 @@ builder.Services.AddLocalization(options => options.ResourcesPath = "");
//PLC进站后台服务注册
//builder.Services.AddHostedService<PlcOutStationService_OP07_01>();
//builder.Services.AddHostedService<PlcIntoStationService_OP70_01>();
builder.Services.AddHostedService<PlcIntoStationService_OP080_1>();
//螺丝枪服务注册
//builder.Services.AddHostedService<PF6ScrewGunService>();

View File

@ -24,7 +24,7 @@ namespace RIZO.Service.PLCBackground.Stations.Into
/// <summary>
/// OP点散热胶GF1500
/// </summary>
[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<OptionsSetting> options) : base(options)

View File

@ -24,7 +24,7 @@ namespace RIZO.Service.PLCBackground.Stations.Into
/// <summary>
/// OP点散热胶GF1500
/// </summary>
[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<OptionsSetting> options) : base(options)