Skip to content

Commit

Permalink
Merge pull request #67 from EasyAbp/on-completed
Browse files Browse the repository at this point in the history
Use `IServiceScopeFactory` on UOW completed
  • Loading branch information
gdlcf88 authored Aug 3, 2023
2 parents dfb356c + 9321c44 commit 029f68f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
1 change: 1 addition & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
branches:
- master
- main
- release
jobs:
publish:
runs-on: ubuntu-20.04
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,15 +29,15 @@ public class PaidWeChatPayEventHandler : IWeChatPayEventHandler, ITransientDepen

public PaidWeChatPayEventHandler(
IDataFilter dataFilter,
IServiceProvider serviceProvider,
IServiceScopeFactory serviceScopeFactory,
IUnitOfWorkManager unitOfWorkManager,
IBackgroundJobManager backgroundJobManager,
IPaymentManager paymentManager,
IPaymentRecordRepository paymentRecordRepository,
IPaymentRepository paymentRepository)
{
_dataFilter = dataFilter;
_serviceProvider = serviceProvider;
_serviceScopeFactory = serviceScopeFactory;
_unitOfWorkManager = unitOfWorkManager;
_backgroundJobManager = backgroundJobManager;
_paymentManager = paymentManager;
Expand Down Expand Up @@ -104,15 +104,17 @@ public virtual async Task<WeChatRequestHandlingResult> HandleAsync(WeChatPayEven

_unitOfWorkManager.Current.OnCompleted(async () =>
{
await _backgroundJobManager.EnqueueAsync(args);
using var scope = _serviceScopeFactory.CreateScope();
var backgroundJobManager = scope.ServiceProvider.GetRequiredService<IBackgroundJobManager>();
await backgroundJobManager.EnqueueAsync(args);
});
}
else
{
_unitOfWorkManager.Current.OnCompleted(async () =>
{
var job = _serviceProvider.GetRequiredService<WeChatPayRefundJob>();

using var scope = _serviceScopeFactory.CreateScope();
var job = scope.ServiceProvider.GetRequiredService<WeChatPayRefundJob>();
await job.ExecuteAsync(args);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,14 +32,14 @@ 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;

public const string PaymentMethod = "WeChatPay";

public WeChatPayPaymentServiceProvider(
ISettingProvider settingProvider,
IServiceProvider serviceProvider,
IGuidGenerator guidGenerator,
ICurrentTenant currentTenant,
IUnitOfWorkManager unitOfWorkManager,
Expand All @@ -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;
Expand All @@ -64,6 +63,7 @@ public WeChatPayPaymentServiceProvider(
_paymentRecordRepository = paymentRecordRepository;
_paymentOpenIdProvider = paymentOpenIdProvider;
_paymentRepository = paymentRepository;
_serviceScopeFactory = serviceScopeFactory;
_abpWeChatPayOptionsProvider = abpWeChatPayOptionsProvider;
_abpWeChatPayServiceFactory = abpWeChatPayServiceFactory;
}
Expand Down Expand Up @@ -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<IBackgroundJobManager>();
await backgroundJobManager.EnqueueAsync(args);
});
}
else
{
_unitOfWorkManager.Current.OnCompleted(async () =>
{
var job = _serviceProvider.GetRequiredService<CloseWeChatPayOrderJob>();

using var scope = _serviceScopeFactory.CreateScope();
var job = scope.ServiceProvider.GetRequiredService<CloseWeChatPayOrderJob>();
await job.ExecuteAsync(args);
});
}
Expand All @@ -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<IBackgroundJobManager>();
await backgroundJobManager.EnqueueAsync(args);
});
}
else
{
_unitOfWorkManager.Current.OnCompleted(async () =>
{
var job = _serviceProvider.GetRequiredService<WeChatPayRefundJob>();

using var scope = _serviceScopeFactory.CreateScope();
var job = scope.ServiceProvider.GetRequiredService<WeChatPayRefundJob>();
await job.ExecuteAsync(args);
});
}
Expand Down

0 comments on commit 029f68f

Please sign in to comment.