mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-09-19 18:22:24 +08:00
docs: 完善文档
This commit is contained in:
@@ -2,67 +2,4 @@
|
||||
|
||||
---
|
||||
|
||||
> 请先自行阅读:
|
||||
>
|
||||
> [《Microsoft Docs - 使用 IHttpClientFactory 实现复原 HTTP 请求》](https://docs.microsoft.com/zh-cn/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests)
|
||||
>
|
||||
> [《Microsoft Docs - 在 ASP.NET Core 中使用 IHttpClientFactory 发出 HTTP 请求》](https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/http-requests#httpclient-and-lifetime-management)
|
||||
>
|
||||
> [《Microsoft Docs - .NET Core 2.1 的新增功能:套接字改进》](https://docs.microsoft.com/zh-CN/dotnet/core/whats-new/dotnet-core-2-1#sockets-improvements)
|
||||
|
||||
当你的项目是运行在 ASP.NET Core 2.1 或更高版本的平台时,CLR 已经提供了全新的底层套接字实现,无需你手动干预 `HttpClient` 的生命周期。
|
||||
|
||||
如果你想手动管理 `HttpClient`,那么可以参考下面基于 DI/IoC 的代码实现:
|
||||
|
||||
```csharp
|
||||
using Flurl.Http;
|
||||
using Flurl.Http.Configuration;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3;
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
|
||||
|
||||
public class WechatTenpayClientFactory
|
||||
{
|
||||
internal class DelegatingFlurlClientFactory : IFlurlClientFactory
|
||||
{
|
||||
private readonly System.Net.Http.IHttpClientFactory _httpClientFactory;
|
||||
|
||||
public DelegatingFlurlClientFactory(System.Net.Http.IHttpClientFactory httpClientFactory)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
|
||||
}
|
||||
|
||||
public Flurl.Http.IFlurlClient Get(Flurl.Url url)
|
||||
{
|
||||
return new FlurlClient(_httpClientFactory.CreateClient(url.ToUri()));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// Do Nothing
|
||||
}
|
||||
}
|
||||
|
||||
private readonly System.Net.Http.IHttpClientFactory _httpClientFactory;
|
||||
private readonly IOptions<WechatTenpayClientOptions> _wechatTenpayClientOptions;
|
||||
|
||||
public WechatTenpayClientFactory(
|
||||
System.Net.Http.IHttpClientFactory httpClientFactory,
|
||||
IOptions<WechatTenpayClientOptions> wechatTenpayClientOptions)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
|
||||
_wechatTenpayClientOptions = wechatTenpayClientOptions ?? throw new ArgumentNullException(nameof(wechatTenpayClientOptions));
|
||||
|
||||
Flurl.Http.FlurlHttp.GlobalSettings.FlurlClientFactory = new DelegatingFlurlClientFactory(_httpClientFactory);
|
||||
}
|
||||
|
||||
public WechatTenpayClient CreateClient()
|
||||
{
|
||||
return new WechatTenpayClient(_wechatTenpayClientOptions.Value);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
需要强调的是,虽然 `WechatTenpayClient` 实现了 `System.IDisposable` 接口,但你不应该在 DI/IoC 中手动释放它,而是应该交给 IoC 容器自动管理它。
|
||||
|
||||
此外你应注意,`System.Net.Http.IHttpClientFactory` 与 `Flurl.Http.Configuration.IHttpClientFactory` 是两个不同的类型,[使用时请加以区分](https://flurl.dev/docs/configuration/#httpclientfactory)。
|
||||
`IHttpClientFactory` 在本库下的用法与在 [SKIT.FlurlHttpClient.Wechat.Api](../WechatApi/README.md) 模块下的用法类似,请参阅[相关文档](../WechatApi/Advanced_IHttpClientFactory.md)。
|
||||
|
7
docs/WechatTenpayV3/Advanced_Interceptor.md
Normal file
7
docs/WechatTenpayV3/Advanced_Interceptor.md
Normal file
@@ -0,0 +1,7 @@
|
||||
### 如何使用拦截器?
|
||||
|
||||
---
|
||||
|
||||
拦截器在本库下的用法与在 [SKIT.FlurlHttpClient.Wechat.Api](../WechatApi/README.md) 模块下的用法类似,请参阅[相关文档](../WechatApi/Advanced_Interceptor.md)。
|
||||
|
||||
本库内置了一个用于请求时自动生成签名的拦截器。
|
@@ -2,45 +2,4 @@
|
||||
|
||||
---
|
||||
|
||||
> 请先自行阅读:
|
||||
>
|
||||
> [《Microsoft Docs - .NET 中的 JSON 序列化和反序列化(封送和拆收)》](https://docs.microsoft.com/zh-cn/dotnet/standard/serialization/system-text-json-overview)
|
||||
|
||||
默认情况下,本库使用 `System.Text.Json` 作为 JSON 序列化器。
|
||||
|
||||
如果你更习惯于 `Newtonsoft.Json`,那么你可以在构造得到 `WechatTenpayClient` 对象后:
|
||||
|
||||
```csharp
|
||||
client.Configure(settings =>
|
||||
{
|
||||
settings.JsonSerializer = new FlurlNewtonsoftJsonSerializer();
|
||||
});
|
||||
```
|
||||
|
||||
此外,如果你希望调整一些序列化器的配置项,那么可以:
|
||||
|
||||
```csharp
|
||||
using System.Text.Json;
|
||||
using SKIT.FlurlHttpClient.Wechat;
|
||||
|
||||
client.Configure(settings =>
|
||||
{
|
||||
var jsonOptions = FlurlSystemTextJsonSerializer.GetDefaultSerializerOptions();
|
||||
jsonOptions.WriteIndented = true;
|
||||
settings.JsonSerializer = new FlurlSystemTextJsonSerializer(jsonOptions);
|
||||
});
|
||||
```
|
||||
|
||||
使用 `Newtonsoft.Json` 时也是同样的:
|
||||
|
||||
```csharp
|
||||
using Newtonsoft.Json;
|
||||
using SKIT.FlurlHttpClient.Wechat;
|
||||
|
||||
client.Configure(settings =>
|
||||
{
|
||||
var jsonSettings = FlurlNewtonsoftJsonSerializer.GetDefaultSerializerSettings();
|
||||
jsonSettings.Formatting = Formatting.Indented;
|
||||
settings.JsonSerializer = new FlurlNewtonsoftJsonSerializer(jsonSettings);
|
||||
});
|
||||
```
|
||||
JSON 序列化器在本库下的用法与在 [SKIT.FlurlHttpClient.Wechat.Api](../WechatApi/README.md) 模块下的用法类似,请参阅[相关文档](../WechatApi/Advanced_JsonSerializer.md)。
|
@@ -127,6 +127,8 @@ var response = await client.ExecuteCreatePayTransactionJsapiAsync(request);
|
||||
|
||||
- [如何指定 JSON 序列化器?](./Advanced_JsonSerializer.md)
|
||||
|
||||
- [如何使用拦截器?](./Advanced_Interceptor.md)
|
||||
|
||||
- [如何解密响应中的敏感数据?](./Advanced_ResponseDataDecryption.md)
|
||||
|
||||
- [如何解密回调通知事件中的敏感数据?](./Advanced_EventDataDecryption.md)
|
||||
|
Reference in New Issue
Block a user