DotNetCore.SKIT.FlurlHttpCl.../samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6/Services/BackgroundServices/TenpayCertificateRefreshingBackgroundService.cs

75 lines
3.1 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Settings;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net5.Services.BackgroundServices
{
2021-12-04 16:11:49 +08:00
class TenpayCertificateRefreshingBackgroundService : BackgroundService
{
private readonly ILogger _logger;
2021-12-04 16:11:49 +08:00
private readonly Options.TenpayOptions _tenpayOptions;
private readonly HttpClients.IWechatTenpayHttpClientFactory _tenpayHttpClientFactory;
2021-12-04 16:11:49 +08:00
public TenpayCertificateRefreshingBackgroundService(
ILoggerFactory loggerFactory,
2021-12-04 16:11:49 +08:00
IOptions<Options.TenpayOptions> tenpayOptions,
HttpClients.IWechatTenpayHttpClientFactory tenpayHttpClientFactory)
{
_logger = loggerFactory.CreateLogger(GetType());
2021-12-04 16:11:49 +08:00
_tenpayOptions = tenpayOptions.Value;
_tenpayHttpClientFactory = tenpayHttpClientFactory;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
2021-12-04 16:11:49 +08:00
var wxpayMerchant = _tenpayOptions.Merchants.FirstOrDefault();
if (wxpayMerchant == null)
{
_logger.LogWarning("未找到微信商户配置项。");
break;
}
try
{
2021-12-04 16:11:49 +08:00
var client = _tenpayHttpClientFactory.Create(wxpayMerchant.MerchantId);
var request = new QueryCertificatesRequest();
var response = await client.ExecuteQueryCertificatesAsync(request, cancellationToken: stoppingToken);
if (response.IsSuccessful())
{
2021-11-26 02:59:02 +08:00
// 注意:如果启用了 AutoDecryptResponseSensitiveProperty则无需再手动执行下面被注释的解密方法
// response = client.DecryptResponseSensitiveProperty(response);
foreach (var certificateModel in response.CertificateList)
{
client.CertificateManager.AddEntry(new CertificateEntry(certificateModel));
}
_logger.LogInformation("刷新微信商户平台证书成功。");
}
else
{
_logger.LogWarning(
"刷新微信商户平台证书失败(状态码:{0},错误代码:{1},错误描述:{2})。",
response.RawStatus, response.ErrorCode, response.ErrorMessage
);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "刷新微信商户平台证书遇到异常。");
}
await Task.Delay(TimeSpan.FromDays(1)); // 每隔 1 天轮询刷新
}
}
}
}