From 9321c44752cd4e01021bc463c3af125805b30dd0 Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Thu, 3 Aug 2023 15:48:24 +0800 Subject: [PATCH] Use `IServiceScopeFactory` on UOW completed --- .github/workflows/publish.yml | 1 + .../WeChatPay/PaidWeChatPayEventHandler.cs | 14 ++++++---- .../WeChatPayPaymentServiceProvider.cs | 28 +++++++++++++------ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 02a5497..0ccef00 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -4,6 +4,7 @@ on: branches: - master - main + - release jobs: publish: runs-on: ubuntu-20.04 diff --git a/modules/EasyAbp.PaymentService.WeChatPay/src/EasyAbp.PaymentService.WeChatPay.Domain/EasyAbp/PaymentService/WeChatPay/PaidWeChatPayEventHandler.cs b/modules/EasyAbp.PaymentService.WeChatPay/src/EasyAbp.PaymentService.WeChatPay.Domain/EasyAbp/PaymentService/WeChatPay/PaidWeChatPayEventHandler.cs index bbcc39c..33786ed 100644 --- a/modules/EasyAbp.PaymentService.WeChatPay/src/EasyAbp.PaymentService.WeChatPay.Domain/EasyAbp/PaymentService/WeChatPay/PaidWeChatPayEventHandler.cs +++ b/modules/EasyAbp.PaymentService.WeChatPay/src/EasyAbp.PaymentService.WeChatPay.Domain/EasyAbp/PaymentService/WeChatPay/PaidWeChatPayEventHandler.cs @@ -20,7 +20,7 @@ public class PaidWeChatPayEventHandler : IWeChatPayEventHandler, ITransientDepen public WeChatHandlerType Type => WeChatHandlerType.Paid; private readonly IDataFilter _dataFilter; - private readonly IServiceProvider _serviceProvider; + private readonly IServiceScopeFactory _serviceScopeFactory; private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly IBackgroundJobManager _backgroundJobManager; private readonly IPaymentManager _paymentManager; @@ -29,7 +29,7 @@ public class PaidWeChatPayEventHandler : IWeChatPayEventHandler, ITransientDepen public PaidWeChatPayEventHandler( IDataFilter dataFilter, - IServiceProvider serviceProvider, + IServiceScopeFactory serviceScopeFactory, IUnitOfWorkManager unitOfWorkManager, IBackgroundJobManager backgroundJobManager, IPaymentManager paymentManager, @@ -37,7 +37,7 @@ public PaidWeChatPayEventHandler( IPaymentRepository paymentRepository) { _dataFilter = dataFilter; - _serviceProvider = serviceProvider; + _serviceScopeFactory = serviceScopeFactory; _unitOfWorkManager = unitOfWorkManager; _backgroundJobManager = backgroundJobManager; _paymentManager = paymentManager; @@ -104,15 +104,17 @@ public virtual async Task HandleAsync(WeChatPayEven _unitOfWorkManager.Current.OnCompleted(async () => { - await _backgroundJobManager.EnqueueAsync(args); + using var scope = _serviceScopeFactory.CreateScope(); + var backgroundJobManager = scope.ServiceProvider.GetRequiredService(); + await backgroundJobManager.EnqueueAsync(args); }); } else { _unitOfWorkManager.Current.OnCompleted(async () => { - var job = _serviceProvider.GetRequiredService(); - + using var scope = _serviceScopeFactory.CreateScope(); + var job = scope.ServiceProvider.GetRequiredService(); await job.ExecuteAsync(args); }); } diff --git a/modules/EasyAbp.PaymentService.WeChatPay/src/EasyAbp.PaymentService.WeChatPay.Domain/EasyAbp/PaymentService/WeChatPay/WeChatPayPaymentServiceProvider.cs b/modules/EasyAbp.PaymentService.WeChatPay/src/EasyAbp.PaymentService.WeChatPay.Domain/EasyAbp/PaymentService/WeChatPay/WeChatPayPaymentServiceProvider.cs index d757b4e..38bfe5b 100644 --- a/modules/EasyAbp.PaymentService.WeChatPay/src/EasyAbp.PaymentService.WeChatPay.Domain/EasyAbp/PaymentService/WeChatPay/WeChatPayPaymentServiceProvider.cs +++ b/modules/EasyAbp.PaymentService.WeChatPay/src/EasyAbp.PaymentService.WeChatPay.Domain/EasyAbp/PaymentService/WeChatPay/WeChatPayPaymentServiceProvider.cs @@ -23,7 +23,6 @@ namespace EasyAbp.PaymentService.WeChatPay public class WeChatPayPaymentServiceProvider : PaymentServiceProvider { private readonly ISettingProvider _settingProvider; - private readonly IServiceProvider _serviceProvider; private readonly IGuidGenerator _guidGenerator; private readonly ICurrentTenant _currentTenant; private readonly IUnitOfWorkManager _unitOfWorkManager; @@ -33,6 +32,7 @@ public class WeChatPayPaymentServiceProvider : PaymentServiceProvider private readonly IPaymentRecordRepository _paymentRecordRepository; private readonly IPaymentOpenIdProvider _paymentOpenIdProvider; private readonly IPaymentRepository _paymentRepository; + private readonly IServiceScopeFactory _serviceScopeFactory; private readonly IAbpWeChatPayOptionsProvider _abpWeChatPayOptionsProvider; private readonly IAbpWeChatPayServiceFactory _abpWeChatPayServiceFactory; @@ -40,7 +40,6 @@ public class WeChatPayPaymentServiceProvider : PaymentServiceProvider public WeChatPayPaymentServiceProvider( ISettingProvider settingProvider, - IServiceProvider serviceProvider, IGuidGenerator guidGenerator, ICurrentTenant currentTenant, IUnitOfWorkManager unitOfWorkManager, @@ -50,11 +49,11 @@ public WeChatPayPaymentServiceProvider( IPaymentRecordRepository paymentRecordRepository, IPaymentOpenIdProvider paymentOpenIdProvider, IPaymentRepository paymentRepository, + IServiceScopeFactory serviceScopeFactory, IAbpWeChatPayOptionsProvider abpWeChatPayOptionsProvider, IAbpWeChatPayServiceFactory abpWeChatPayServiceFactory) { _settingProvider = settingProvider; - _serviceProvider = serviceProvider; _guidGenerator = guidGenerator; _currentTenant = currentTenant; _unitOfWorkManager = unitOfWorkManager; @@ -64,6 +63,7 @@ public WeChatPayPaymentServiceProvider( _paymentRecordRepository = paymentRecordRepository; _paymentOpenIdProvider = paymentOpenIdProvider; _paymentRepository = paymentRepository; + _serviceScopeFactory = serviceScopeFactory; _abpWeChatPayOptionsProvider = abpWeChatPayOptionsProvider; _abpWeChatPayServiceFactory = abpWeChatPayServiceFactory; } @@ -176,14 +176,19 @@ public override async Task OnCancelStartedAsync(Payment payment) // Enqueue an empty job to ensure the background job worker is alive. await _backgroundJobManager.EnqueueAsync(new EmptyJobArgs(payment.TenantId)); - _unitOfWorkManager.Current.OnCompleted(async () => { await _backgroundJobManager.EnqueueAsync(args); }); + _unitOfWorkManager.Current.OnCompleted(async () => + { + using var scope = _serviceScopeFactory.CreateScope(); + var backgroundJobManager = scope.ServiceProvider.GetRequiredService(); + await backgroundJobManager.EnqueueAsync(args); + }); } else { _unitOfWorkManager.Current.OnCompleted(async () => { - var job = _serviceProvider.GetRequiredService(); - + using var scope = _serviceScopeFactory.CreateScope(); + var job = scope.ServiceProvider.GetRequiredService(); await job.ExecuteAsync(args); }); } @@ -199,14 +204,19 @@ public override async Task OnRefundStartedAsync(Payment payment, Refund refund) // Enqueue an empty job to ensure the background job worker is alive. await _backgroundJobManager.EnqueueAsync(new EmptyJobArgs(payment.TenantId)); - _unitOfWorkManager.Current.OnCompleted(async () => { await _backgroundJobManager.EnqueueAsync(args); }); + _unitOfWorkManager.Current.OnCompleted(async () => + { + using var scope = _serviceScopeFactory.CreateScope(); + var backgroundJobManager = scope.ServiceProvider.GetRequiredService(); + await backgroundJobManager.EnqueueAsync(args); + }); } else { _unitOfWorkManager.Current.OnCompleted(async () => { - var job = _serviceProvider.GetRequiredService(); - + using var scope = _serviceScopeFactory.CreateScope(); + var job = scope.ServiceProvider.GetRequiredService(); await job.ExecuteAsync(args); }); }