diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6/Controllers/TenpayNotifyController.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6/Controllers/TenpayNotifyController.cs index b298bfa7..1c78c7fd 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6/Controllers/TenpayNotifyController.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6/Controllers/TenpayNotifyController.cs @@ -35,7 +35,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Controllers string content = await reader.ReadToEndAsync(); _logger.LogInformation("接收到微信支付推送的数据:{0}", content); - using var client = _wechatTenpayClientFactory.Create(merchantId); + var client = _wechatTenpayClientFactory.Create(merchantId); bool valid = client.VerifyEventSignature( webhookTimestamp: timestamp, webhookNonce: nonce, diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6/Controllers/TenpayOrderController.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6/Controllers/TenpayOrderController.cs index a4ae6459..df1f9cac 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6/Controllers/TenpayOrderController.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6/Controllers/TenpayOrderController.cs @@ -31,7 +31,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Controllers [Route("jsapi")] public async Task CreateOrderByJsapi([FromBody] Models.CreateOrderByJsapiRequest requestModel) { - using var client = _wechatTenpayClientFactory.Create(requestModel.MerchantId); + var client = _wechatTenpayClientFactory.Create(requestModel.MerchantId); var request = new CreatePayTransactionJsapiRequest() { OutTradeNumber = "SAMPLE_OTN_" + DateTimeOffset.Now.ToString("yyyyMMddHHmmssfff"), diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6/Controllers/TenpayRefundController.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6/Controllers/TenpayRefundController.cs index 8b2afd84..d8addae6 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6/Controllers/TenpayRefundController.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6/Controllers/TenpayRefundController.cs @@ -31,7 +31,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Controllers [Route("")] public async Task CreateRefund([FromBody] Models.CreateRefundRequest requestModel) { - using var client = _wechatTenpayClientFactory.Create(requestModel.MerchantId); + var client = _wechatTenpayClientFactory.Create(requestModel.MerchantId); var request = new CreateRefundDomesticRefundRequest() { TransactionId = requestModel.TransactionId, diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6/Services/HttpClients/Implements/WechatTenpayClientFactory.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6/Services/HttpClients/Implements/WechatTenpayClientFactory.cs index c76460dc..b87ea371 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6/Services/HttpClients/Implements/WechatTenpayClientFactory.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_Net6/Services/HttpClients/Implements/WechatTenpayClientFactory.cs @@ -1,18 +1,22 @@ using System; using System.Linq; +using System.Net.Http; using Microsoft.Extensions.Options; namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Services.HttpClients.Implements { internal partial class WechatTenpayClientFactory : IWechatTenpayClientFactory { + private readonly IHttpClientFactory _httpClientFactory; private readonly Options.TenpayOptions _tenpayOptions; private readonly IWechatTenpayCertificateManagerFactory _tenpayCertificateManagerFactory; public WechatTenpayClientFactory( + IHttpClientFactory httpClientFactory, IOptions tenpayOptions, IWechatTenpayCertificateManagerFactory tenpayCertificateManagerFactory) { + _httpClientFactory = httpClientFactory; _tenpayOptions = tenpayOptions.Value; _tenpayCertificateManagerFactory = tenpayCertificateManagerFactory; } @@ -37,7 +41,9 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Services.HttpClients.Imple AutoEncryptRequestSensitiveProperty = false, AutoDecryptResponseSensitiveProperty = false }; - var wechatTenpayClient = new WechatTenpayClient(wechatTenpayClientOptions); + var wechatTenpayClient = WechatTenpayClientBuilder.Create(wechatTenpayClientOptions) + .UseHttpClient(_httpClientFactory.CreateClient(), disposeClient: false) + .Build(); return wechatTenpayClient; } } diff --git a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_NetFramework47/Services/BackgroundJobs/TenpayCertificateRefreshingBackgroundJob.cs b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_NetFramework47/Services/BackgroundJobs/TenpayCertificateRefreshingBackgroundJob.cs index 0c3e8f5e..f405e408 100644 --- a/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_NetFramework47/Services/BackgroundJobs/TenpayCertificateRefreshingBackgroundJob.cs +++ b/samples/SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample_NetFramework47/Services/BackgroundJobs/TenpayCertificateRefreshingBackgroundJob.cs @@ -17,7 +17,7 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Services.BackgroundJobs try { const string ALGORITHM_TYPE = "RSA"; - var client = _wechatTenpayClientFactory.Create(tenpayMerchantOptions.MerchantId); + using var client = _wechatTenpayClientFactory.Create(tenpayMerchantOptions.MerchantId); var request = new QueryCertificatesRequest() { AlgorithmType = ALGORITHM_TYPE }; var response = await client.ExecuteQueryCertificatesAsync(request); if (response.IsSuccessful()) diff --git a/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/WechatTenpayClientBuilder.cs b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/WechatTenpayClientBuilder.cs new file mode 100644 index 00000000..395c54e1 --- /dev/null +++ b/src/SKIT.FlurlHttpClient.Wechat.TenpayV3/WechatTenpayClientBuilder.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; + +namespace SKIT.FlurlHttpClient.Wechat.TenpayV3 +{ + /// + /// 用于构造 实例的构造器。 + /// + public partial class WechatTenpayClientBuilder : ICommonClientBuilder + { + private readonly WechatTenpayClientOptions _options; + private readonly IList> _configures; + private readonly IList _interceptors; + private HttpClient? _httpClient; + private bool? _disposeClient; + + private WechatTenpayClientBuilder(WechatTenpayClientOptions options) + { + _options = options; + _configures = new List>(); + _interceptors = new List(); + } + + ICommonClientBuilder ICommonClientBuilder.ConfigureSettings(Action configure) + { + return ConfigureSettings(configure); + } + + ICommonClientBuilder ICommonClientBuilder.UseInterceptor(HttpInterceptor interceptor) + { + return UseInterceptor(interceptor); + } + + ICommonClientBuilder ICommonClientBuilder.UseHttpClient(HttpClient httpClient, bool disposeClient) + { + return UseHttpClient(httpClient, disposeClient); + } + + public WechatTenpayClientBuilder ConfigureSettings(Action configure) + { + if (configure is null) throw new ArgumentNullException(nameof(configure)); + + _configures.Add(configure); + return this; + } + + public WechatTenpayClientBuilder UseInterceptor(HttpInterceptor interceptor) + { + if (interceptor is null) throw new ArgumentNullException(nameof(interceptor)); + + _interceptors.Add(interceptor); + return this; + } + + public WechatTenpayClientBuilder UseHttpClient(HttpClient httpClient, bool disposeClient = true) + { + if (httpClient is null) throw new ArgumentNullException(nameof(httpClient)); + + _httpClient = httpClient; + _disposeClient = disposeClient; + return this; + } + + public WechatTenpayClient Build() + { + WechatTenpayClient client = _disposeClient.HasValue + ? new WechatTenpayClient(_options, _httpClient, _disposeClient.Value) + : new WechatTenpayClient(_options, _httpClient); + + foreach (Action configure in _configures) + { + client.Configure(configure); + } + + foreach (HttpInterceptor interceptor in _interceptors) + { + client.Interceptors.Add(interceptor); + } + + return client; + } + } + + partial class WechatTenpayClientBuilder + { + public static WechatTenpayClientBuilder Create(WechatTenpayClientOptions options) + { + if (options is null) throw new ArgumentNullException(nameof(options)); + + return new WechatTenpayClientBuilder(options); + } + } +}