From 375ff1a72952a7dc3fad884a1835e472b5f794cc Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Wed, 12 May 2021 18:20:52 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E5=AE=8C=E5=96=84=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Advanced_IHttpClientFactory.md | 62 ++++++++++++------- docs/WechatTenpayV3/README.md | 2 + 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/docs/WechatTenpayV3/Advanced_IHttpClientFactory.md b/docs/WechatTenpayV3/Advanced_IHttpClientFactory.md index 1eaba8de..2c331a56 100644 --- a/docs/WechatTenpayV3/Advanced_IHttpClientFactory.md +++ b/docs/WechatTenpayV3/Advanced_IHttpClientFactory.md @@ -7,44 +7,62 @@ > [《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) -你可以在构造得到 `WechatTenpayClient` 对象后: +当你的项目是运行在 ASP.NET Core 2.1 或更高版本的平台时,CLR 已经提供了全新的底层套接字实现,无需你手动干预 `HttpClient` 的生命周期。 + +如果你想手动管理 `HttpClient`,那么可以参考下面基于 DI/IoC 的代码实现: ```csharp -client.Configure(settings => -{ - settings.HttpClientFactory = HttpClientFactory; // 赋值为依赖注入的 `IHttpClientFactory` 对象 -}); -``` - -下面给出一个使用了依赖注入的完整例子: - -```csharp -using System.Net.Http; -using Microsoft.Extensions.Http; +using Flurl.Http; +using Flurl.Http.Configuration; using Microsoft.Extensions.Options; using SKIT.FlurlHttpClient.Wechat.TenpayV3; using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models; -public class MyWechatClientFactory +public class WechatTenpayClientFactory { - private readonly IHttpClientFactory _httpClientFactory; + 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)); + } + + public void Dispose() + { + // Do Nothing + } + } + + private readonly System.Net.Http.IHttpClientFactory _httpClientFactory; private readonly IOptions _wechatTenpayClientOptions; - public MyWechatClient( - IHttpClientFactory httpClientFactory, + public WechatTenpayClientFactory( + System.Net.Http.IHttpClientFactory httpClientFactory, IOptions wechatTenpayClientOptions) { - _httpClientFactory = httpClientFactory; - _wechatTenpayClientOptions = wechatTenpayClientOptions; + _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory)); + _wechatTenpayClientOptions = wechatTenpayClientOptions ?? throw new ArgumentNullException(nameof(wechatTenpayClientOptions)); + + FlurlHttp.Configure(settings => settings.FlurlClientFactory = new DelegatingFlurlClientFactory(_httpClientFactory)); } public WechatTenpayClient CreateClient() { - var client = new WechatTenpayClient(_wechatTenpayClientOptions.Value); - client.Configure(settings => settings.HttpClientFactory = _httpClientFactory); - - return client; + 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)。 diff --git a/docs/WechatTenpayV3/README.md b/docs/WechatTenpayV3/README.md index 5ed8f7c1..2ebea993 100644 --- a/docs/WechatTenpayV3/README.md +++ b/docs/WechatTenpayV3/README.md @@ -61,6 +61,8 @@ ### 安装: +提示:如果你使用 Visual Studio NuGet 管理器图形化界面,请在搜索结果中勾选“**包括预发行版**”。 + ```shell # 通过 NuGet 安装 > Install-Package SKIT.FlurlHttpClient.Wechat.TenpayV3