fix: 修复 _=Task.Run() 使用abp的ApplicationService提供的LazyServiceProvider创建scope出现嵌套scope创建失败的异常

解决方法:修改为IServiceScopeFactory创建scope的方式
This commit is contained in:
云龙 王 2024-10-21 09:12:04 +08:00
parent 8f1cd74163
commit 4f5f8b4cfa
3 changed files with 21 additions and 4 deletions

View File

@ -22,6 +22,13 @@ namespace SJZY.Track.Application.Services.TimerTaskss;
[ApiExplorerSettings(GroupName = "定时任务")]
public class TrackPullTask : ApplicationService, ISingletonDependency
{
private readonly IServiceScopeFactory _serviceScopeFactory;
public TrackPullTask(IServiceScopeFactory serviceScopeFactory)
{
_serviceScopeFactory = serviceScopeFactory;
}
/// <summary>
/// 定时任务刷新轨迹
/// </summary>
@ -30,7 +37,7 @@ public class TrackPullTask : ApplicationService, ISingletonDependency
[HttpPost("/TimerTask/Refresh")]
public async Task<bool> RefreshForTaskAsync()
{
using var serviceScope = LazyServiceProvider.CreateScope();
using var serviceScope = _serviceScopeFactory.CreateScope();
var provideQueries = serviceScope.ServiceProvider.GetRequiredService<IProvideQueries>();
var configs = await provideQueries.GetAllPullConfigsAsync();

View File

@ -13,6 +13,13 @@ namespace SJZY.Track.Application.Services.TimerTaskss;
[ApiExplorerSettings(GroupName = "定时任务")]
public class TrackPushTask : ApplicationService
{
private readonly IServiceScopeFactory _serviceScopeFactory;
public TrackPushTask(IServiceScopeFactory serviceScopeFactory)
{
_serviceScopeFactory = serviceScopeFactory;
}
/// <summary>
/// 定时任务推送预警(》12小时没有推送的)
/// </summary>
@ -21,7 +28,7 @@ public class TrackPushTask : ApplicationService
[HttpPost("/TimerTask/PushWarn")]
public async Task<string> PushWarnForTaskAsync()
{
using var serviceScope = LazyServiceProvider.CreateScope();
using var serviceScope = _serviceScopeFactory.CreateScope();
var configs = serviceScope.ServiceProvider.GetRequiredService<IOptionsSnapshot<Track2InterfaceRelayOptions>>()
.Value;

View File

@ -16,11 +16,14 @@ public class TrackWarningTask : ApplicationService
{
private readonly IOptionsSnapshot<Track2InterfaceRelayOptions> _options;
private readonly IOrderRepository _orderRepository;
private readonly IServiceScopeFactory _serviceScopeFactory;
public TrackWarningTask(IOrderRepository orderRepository, IOptionsSnapshot<Track2InterfaceRelayOptions> options)
public TrackWarningTask(IOrderRepository orderRepository, IOptionsSnapshot<Track2InterfaceRelayOptions> options,
IServiceScopeFactory serviceScopeFactory)
{
_orderRepository = orderRepository;
_options = options;
_serviceScopeFactory = serviceScopeFactory;
}
/// <summary>
@ -63,7 +66,7 @@ public class TrackWarningTask : ApplicationService
{
try
{
using var serviceScope = LazyServiceProvider.CreateScope();
using var serviceScope = _serviceScopeFactory.CreateScope();
var forewarningNotifyService =
serviceScope.ServiceProvider.GetRequiredService<IForewarningNotifyService>();