docs: 改进与 IHttpClientFactory 集成的示例代码

This commit is contained in:
Fu Diwei 2022-01-18 23:15:37 +08:00
parent 28ec4faba5
commit 47575857b6
3 changed files with 39 additions and 58 deletions

View File

@ -15,15 +15,13 @@
如果你想手动管理 `HttpClient`,那么可以参考下面基于 DI/IoC 的代码实现:
```csharp
using Flurl.Http;
using Flurl.Http.Configuration;
using Microsoft.Extensions.Options;
using SKIT.FlurlHttpClient.Wechat.Api;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
public class WechatApiClientFactory
{
internal class DelegatingFlurlClientFactory : IFlurlClientFactory
internal class DelegatingFlurlClientFactory : Flurl.Http.Configuration.DefaultHttpClientFactory
{
private readonly System.Net.Http.IHttpClientFactory _httpClientFactory;
@ -32,14 +30,9 @@ public class WechatApiClientFactory
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
}
public IFlurlClient Get(Flurl.Url url)
public override System.Net.Http.HttpClient CreateHttpClient(System.Net.Http.HttpMessageHandler handler)
{
return new FlurlClient(_httpClientFactory.CreateClient(url.ToUri().Host));
}
public void Dispose()
{
// Do Nothing
return _httpClientFactory.CreateClient();
}
}
@ -52,13 +45,13 @@ public class WechatApiClientFactory
{
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
_wechatApiClientOptions = wechatApiClientOptions ?? throw new ArgumentNullException(nameof(wechatApiClientOptions));
Flurl.Http.FlurlHttp.GlobalSettings.FlurlClientFactory = new DelegatingFlurlClientFactory(_httpClientFactory);
}
public WechatApiClient CreateClient()
{
return new WechatApiClient(_wechatApiClientOptions.Value);
WechatApiClient client = new WechatApiClient(_wechatApiClientOptions.Value);
client.Configure((settings) => settings.FlurlHttpClientFactory = new DelegatingFlurlClientFactory(_httpClientFactory));
return client;
}
}
```

View File

@ -1,25 +1,21 @@
using System;
using System.Linq;
using Flurl;
using Flurl.Http;
using Flurl.Http.Configuration;
using System.Net.Http;
using Microsoft.Extensions.Options;
namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.HttpClients.Implements
{
partial class WechatApiHttpClientFactory : IWechatApiHttpClientFactory
{
private readonly System.Net.Http.IHttpClientFactory _httpClientFactory;
private readonly IHttpClientFactory _httpClientFactory;
private readonly Options.WechatOptions _wechatOptions;
public WechatApiHttpClientFactory(
System.Net.Http.IHttpClientFactory httpClientFactory,
IHttpClientFactory httpClientFactory,
IOptions<Options.WechatOptions> wechatOptions)
{
_httpClientFactory = httpClientFactory;
_wechatOptions = wechatOptions.Value;
FlurlHttp.GlobalSettings.FlurlClientFactory = new DelegatingFlurlClientFactory(_httpClientFactory);
}
public WechatApiClient Create(string appId)
@ -32,34 +28,32 @@ namespace SKIT.FlurlHttpClient.Wechat.Api.Sample.Services.HttpClients.Implements
if (wechatAccountOptions == null)
throw new Exception("未在配置项中找到该 AppId 对应的微信账号。");
return new WechatApiClient(new WechatApiClientOptions()
var wechatApiClientOptions = new WechatApiClientOptions()
{
AppId = wechatAccountOptions.AppId,
AppSecret = wechatAccountOptions.AppSecret,
PushToken = _wechatOptions.CallbackToken
});
};
var wechatApiClient = new WechatApiClient(wechatApiClientOptions);
wechatApiClient.Configure((settings) => settings.FlurlHttpClientFactory = new DelegatingFlurlClientFactory(_httpClientFactory));
return wechatApiClient;
}
}
partial class WechatApiHttpClientFactory
{
internal class DelegatingFlurlClientFactory : IFlurlClientFactory
internal class DelegatingFlurlClientFactory : Flurl.Http.Configuration.DefaultHttpClientFactory
{
private readonly System.Net.Http.IHttpClientFactory _httpClientFactory;
private readonly IHttpClientFactory _httpClientFactory;
public DelegatingFlurlClientFactory(System.Net.Http.IHttpClientFactory httpClientFactory)
public DelegatingFlurlClientFactory(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
}
public IFlurlClient Get(Url url)
public override HttpClient CreateHttpClient(HttpMessageHandler handler)
{
return new FlurlClient(_httpClientFactory.CreateClient(url.ToUri().Host));
}
public void Dispose()
{
// Do Nothing
return _httpClientFactory.CreateClient();
}
}
}

View File

@ -1,28 +1,24 @@
using System;
using System.Linq;
using Flurl;
using Flurl.Http;
using Flurl.Http.Configuration;
using System.Net.Http;
using Microsoft.Extensions.Options;
namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Services.HttpClients.Implements
{
partial class WechatTenpayHttpClientFactory : IWechatTenpayHttpClientFactory
{
private readonly System.Net.Http.IHttpClientFactory _httpClientFactory;
private readonly IHttpClientFactory _httpClientFactory;
private readonly Options.TenpayOptions _tenpayOptions;
private readonly IWechatTenpayCertificateManagerFactory _tenpayCertificateManagerFactory;
public WechatTenpayHttpClientFactory(
System.Net.Http.IHttpClientFactory httpClientFactory,
IHttpClientFactory httpClientFactory,
IOptions<Options.TenpayOptions> tenpayOptions,
IWechatTenpayCertificateManagerFactory tenpayCertificateManagerFactory)
{
_httpClientFactory = httpClientFactory;
_tenpayOptions = tenpayOptions.Value;
_tenpayCertificateManagerFactory = tenpayCertificateManagerFactory;
FlurlHttp.GlobalSettings.FlurlClientFactory = new DelegatingFlurlClientFactory(_httpClientFactory);
}
public WechatTenpayClient Create(string merchantId)
@ -31,42 +27,40 @@ namespace SKIT.FlurlHttpClient.Wechat.TenpayV3.Sample.Services.HttpClients.Imple
// 这里的工厂方法是为了演示多租户而存在的,可根据商户号生成不同的 API 客户端。
// 如果你的项目只存在唯一一个租户,那么直接注入 `WechatTenpayClient` 即可。
var tenpayMerchantOptions = _tenpayOptions.Merchants?.FirstOrDefault(e => string.Equals(merchantId, e.MerchantId));
if (tenpayMerchantOptions == null)
var tenpayMerchantConfig = _tenpayOptions.Merchants?.FirstOrDefault(e => string.Equals(merchantId, e.MerchantId));
if (tenpayMerchantConfig == null)
throw new Exception("未在配置项中找到该 MerchantId 对应的微信商户号。");
return new WechatTenpayClient(new WechatTenpayClientOptions()
var wechatTenpayClientOptions = new WechatTenpayClientOptions()
{
MerchantId = tenpayMerchantOptions.MerchantId,
MerchantV3Secret = tenpayMerchantOptions.SecretV3,
MerchantCertSerialNumber = tenpayMerchantOptions.CertSerialNumber,
MerchantCertPrivateKey = tenpayMerchantOptions.CertPrivateKey,
CertificateManager = _tenpayCertificateManagerFactory.Create(tenpayMerchantOptions.MerchantId),
MerchantId = tenpayMerchantConfig.MerchantId,
MerchantV3Secret = tenpayMerchantConfig.SecretV3,
MerchantCertSerialNumber = tenpayMerchantConfig.CertSerialNumber,
MerchantCertPrivateKey = tenpayMerchantConfig.CertPrivateKey,
CertificateManager = _tenpayCertificateManagerFactory.Create(tenpayMerchantConfig.MerchantId),
AutoEncryptRequestSensitiveProperty = true,
AutoDecryptResponseSensitiveProperty = true
});
};
var wechatTenpayClient = new WechatTenpayClient(wechatTenpayClientOptions);
wechatTenpayClient.Configure((settings) => settings.FlurlHttpClientFactory = new DelegatingFlurlClientFactory(_httpClientFactory));
return wechatTenpayClient;
}
}
partial class WechatTenpayHttpClientFactory
{
internal class DelegatingFlurlClientFactory : IFlurlClientFactory
internal class DelegatingFlurlClientFactory : Flurl.Http.Configuration.DefaultHttpClientFactory
{
private readonly System.Net.Http.IHttpClientFactory _httpClientFactory;
private readonly IHttpClientFactory _httpClientFactory;
public DelegatingFlurlClientFactory(System.Net.Http.IHttpClientFactory httpClientFactory)
public DelegatingFlurlClientFactory(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
}
public IFlurlClient Get(Url url)
public override HttpClient CreateHttpClient(HttpMessageHandler handler)
{
return new FlurlClient(_httpClientFactory.CreateClient(url.ToUri().Host));
}
public void Dispose()
{
// Do Nothing
return _httpClientFactory.CreateClient();
}
}
}