From ed7a888078b3d91d4f96483e02ae40f3bcc8ee9c Mon Sep 17 00:00:00 2001
From: wanggaofeng <15601716045@163.com>
Date: Tue, 12 Mar 2024 18:50:53 +0800
Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=EF=BC=9A=E6=9B=B4=E6=96=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../HttpClientStudy.Core.csproj | 4 --
.../HttpClients/PipelineHttpClient.cs | 38 -------------------
.../CustomHeadersHandler.cs | 25 ++++++++++++
.../HttpDelegatingHandlers/HandlerB.cs | 24 ------------
.../{HandlerA.cs => LoggingHandler.cs} | 12 ++++--
HttpClientStudy.Core/UsePolly/UsePolly.cs | 19 ++++++++++
.../HttpClientStudy.UnitTest.csproj | 3 --
.../HttpClients/HttpClientQuestTest.cs | 19 ----------
.../HttpClients/PipelineClientTest.cs | 7 +++-
.../HttpRequests/HttpRequestMethodsTest.cs | 22 +++++++++++
.../HttpResponses/HttpResponseContentTest.cs | 24 ++++++++++++
HttpClientStudy.UnitTest/StartupTest.cs | 6 ++-
.../UseJson/UseJsonInHttpTest.cs | 23 +++++++++++
.../UsePolly/UsePollyTest.cs | 25 ++++++++++++
HttpClientStudy.WebApp/Program.cs | 2 +-
15 files changed, 157 insertions(+), 96 deletions(-)
create mode 100644 HttpClientStudy.Core/HttpDelegatingHandlers/CustomHeadersHandler.cs
delete mode 100644 HttpClientStudy.Core/HttpDelegatingHandlers/HandlerB.cs
rename HttpClientStudy.Core/HttpDelegatingHandlers/{HandlerA.cs => LoggingHandler.cs} (50%)
create mode 100644 HttpClientStudy.Core/UsePolly/UsePolly.cs
create mode 100644 HttpClientStudy.UnitTest/HttpRequests/HttpRequestMethodsTest.cs
create mode 100644 HttpClientStudy.UnitTest/HttpResponses/HttpResponseContentTest.cs
create mode 100644 HttpClientStudy.UnitTest/UseJson/UseJsonInHttpTest.cs
create mode 100644 HttpClientStudy.UnitTest/UsePolly/UsePollyTest.cs
diff --git a/HttpClientStudy.Core/HttpClientStudy.Core.csproj b/HttpClientStudy.Core/HttpClientStudy.Core.csproj
index e0f30b1..fe133c2 100644
--- a/HttpClientStudy.Core/HttpClientStudy.Core.csproj
+++ b/HttpClientStudy.Core/HttpClientStudy.Core.csproj
@@ -19,8 +19,4 @@
-
-
-
-
diff --git a/HttpClientStudy.Core/HttpClients/PipelineHttpClient.cs b/HttpClientStudy.Core/HttpClients/PipelineHttpClient.cs
index 85104cd..b034979 100644
--- a/HttpClientStudy.Core/HttpClients/PipelineHttpClient.cs
+++ b/HttpClientStudy.Core/HttpClients/PipelineHttpClient.cs
@@ -65,42 +65,4 @@ namespace HttpClientStudy.Core
return httpClient;
}
}
-
- ///
- /// 默认处理器1 - 添加自定义请求头
- ///
- public class CustomHeadersHandler : DelegatingHandler
- {
- protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
- {
- // 在请求中添加自定义头部
- request.Headers.Add("X-Custom-Header", "CustomValue");
-
- // 调用管道中的下一个处理器
- return await base.SendAsync(request, cancellationToken);
-
- //响应信息(可不处理)
- }
- }
-
- ///
- /// 默认处理器1 - 简单的日志记录
- ///
- public class LoggingHandler : DelegatingHandler
- {
- protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
- {
- // 记录请求信息
- Console.WriteLine($"Request: {request.Method} {request.RequestUri}");
-
- // 调用管道中的下一个处理器,并获取响应
- var response = await base.SendAsync(request, cancellationToken);
-
- // 记录响应信息
- Console.WriteLine($"Response: {response.StatusCode}");
-
- return response;
- }
- }
-
}
diff --git a/HttpClientStudy.Core/HttpDelegatingHandlers/CustomHeadersHandler.cs b/HttpClientStudy.Core/HttpDelegatingHandlers/CustomHeadersHandler.cs
new file mode 100644
index 0000000..1ce5d5a
--- /dev/null
+++ b/HttpClientStudy.Core/HttpDelegatingHandlers/CustomHeadersHandler.cs
@@ -0,0 +1,25 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace HttpClientStudy.Core
+{
+ ///
+ /// 默认处理器1 - 添加自定义请求头
+ ///
+ public class CustomHeadersHandler : DelegatingHandler
+ {
+ protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
+ {
+ // 在请求中添加自定义头部
+ request.Headers.Add("X-Custom-Header", "CustomValue");
+
+ // 调用管道中的下一个处理器
+ return await base.SendAsync(request, cancellationToken);
+
+ //响应信息(可不处理)
+ }
+ }
+}
diff --git a/HttpClientStudy.Core/HttpDelegatingHandlers/HandlerB.cs b/HttpClientStudy.Core/HttpDelegatingHandlers/HandlerB.cs
deleted file mode 100644
index b0a1137..0000000
--- a/HttpClientStudy.Core/HttpDelegatingHandlers/HandlerB.cs
+++ /dev/null
@@ -1,24 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace HttpClientStudy.Core
-{
- public class HandlerB : DelegatingHandler
- {
- protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
- {
- InnerHandler = new SocketsHttpHandler() { };
-
- //请求前业务
-
- var response = await base.SendAsync(request, cancellationToken);
-
- //响应后业务
-
- return response;
- }
- }
-}
diff --git a/HttpClientStudy.Core/HttpDelegatingHandlers/HandlerA.cs b/HttpClientStudy.Core/HttpDelegatingHandlers/LoggingHandler.cs
similarity index 50%
rename from HttpClientStudy.Core/HttpDelegatingHandlers/HandlerA.cs
rename to HttpClientStudy.Core/HttpDelegatingHandlers/LoggingHandler.cs
index b2e2cb1..421d9ec 100644
--- a/HttpClientStudy.Core/HttpDelegatingHandlers/HandlerA.cs
+++ b/HttpClientStudy.Core/HttpDelegatingHandlers/LoggingHandler.cs
@@ -6,15 +6,21 @@ using System.Threading.Tasks;
namespace HttpClientStudy.Core
{
- public class HandlerA : DelegatingHandler
+ ///
+ /// 默认处理器1 - 简单的日志记录
+ ///
+ public class LoggingHandler : DelegatingHandler
{
protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
- //请求前业务
+ // 记录请求信息
+ Console.WriteLine($"Request: {request.Method} {request.RequestUri}");
+ // 调用管道中的下一个处理器,并获取响应
var response = await base.SendAsync(request, cancellationToken);
- //响应后业务
+ // 记录响应信息
+ Console.WriteLine($"Response: {response.StatusCode}");
return response;
}
diff --git a/HttpClientStudy.Core/UsePolly/UsePolly.cs b/HttpClientStudy.Core/UsePolly/UsePolly.cs
new file mode 100644
index 0000000..d9a3065
--- /dev/null
+++ b/HttpClientStudy.Core/UsePolly/UsePolly.cs
@@ -0,0 +1,19 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace HttpClientStudy.Core.UsePolly
+{
+ ///
+ /// 使用Polly类库,实现重试、熔断、限流等功能
+ ///
+ public class UsePolly
+ {
+ public void UsePollyDemo()
+ {
+ // TODO: 实现Polly的重试、熔断、限流功能
+ }
+ }
+}
diff --git a/HttpClientStudy.UnitTest/HttpClientStudy.UnitTest.csproj b/HttpClientStudy.UnitTest/HttpClientStudy.UnitTest.csproj
index af00e93..5c28ef6 100644
--- a/HttpClientStudy.UnitTest/HttpClientStudy.UnitTest.csproj
+++ b/HttpClientStudy.UnitTest/HttpClientStudy.UnitTest.csproj
@@ -45,9 +45,6 @@
-
-
-
diff --git a/HttpClientStudy.UnitTest/HttpClients/HttpClientQuestTest.cs b/HttpClientStudy.UnitTest/HttpClients/HttpClientQuestTest.cs
index ff31285..f7bbe06 100644
--- a/HttpClientStudy.UnitTest/HttpClients/HttpClientQuestTest.cs
+++ b/HttpClientStudy.UnitTest/HttpClients/HttpClientQuestTest.cs
@@ -27,25 +27,6 @@ namespace HttpClientStudy.UnitTest.HttpClients
_logger = outputHelper;
}
- ///
- /// 多HttpClient实例问题
- /// 1、HttpClient虽然继承了 IDisposable 接口,但它是可以被共享的,且线程安全。Using使用并不能立即释放
- /// 2、网络特性:虽然HttpClient客户端断开,但Web服务并不知道,连接依然存在,状态为 “ TIME_WAIT”(继续等待看是否还有延迟的包会传输过来。)
- /// Windows默认为 240s
- /// 引发常见问题:
- /// 1、HttpClient 多实例并存,有很大资源消耗;
- /// 2、在高并发的情况下,连接来不及释放, socket 被耗尽: 程序异常
- /// 3、连带着会出现“各种套接字问题”
- ///
- /// 查看问题:
- /// 程序并发或多次请求后,通过 netstate 查看下 TCP 连接情况:有很多 TIME_WAIT 状态连接;很可能程序异常;
- ///
- /// 解决:
- /// 1、复用HttpClient
- /// 2、HttpClientFactory
- /// 3、解决DNS问题可以给HttpClient传参:SocketsHttpHandler
- ///
- ///
[Fact]
public async Task Multi_ClientObject_Test()
{
diff --git a/HttpClientStudy.UnitTest/HttpClients/PipelineClientTest.cs b/HttpClientStudy.UnitTest/HttpClients/PipelineClientTest.cs
index 79e6ee3..ef40fc3 100644
--- a/HttpClientStudy.UnitTest/HttpClients/PipelineClientTest.cs
+++ b/HttpClientStudy.UnitTest/HttpClients/PipelineClientTest.cs
@@ -22,10 +22,13 @@ namespace HttpClientStudy.UnitTest.HttpClients
public async Task Delegating_Test()
{
//构建管道
- var handler = new HandlerA()
+ var handler = new CustomHeadersHandler()
{
//相当于下一个中间件(管道)
- InnerHandler = new HandlerB()
+ InnerHandler = new SocketsHttpHandler()
+ {
+ AllowAutoRedirect = true
+ }
};
//构造中传入管道对象
diff --git a/HttpClientStudy.UnitTest/HttpRequests/HttpRequestMethodsTest.cs b/HttpClientStudy.UnitTest/HttpRequests/HttpRequestMethodsTest.cs
new file mode 100644
index 0000000..4a9ccf1
--- /dev/null
+++ b/HttpClientStudy.UnitTest/HttpRequests/HttpRequestMethodsTest.cs
@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace HttpClientStudy.UnitTest.HttpRequests
+{
+ public class HttpRequestMethodsTest
+ {
+ public HttpRequestMethodsTest()
+ {
+
+ }
+
+ [Fact]
+ public void Test()
+ {
+
+ }
+ }
+}
diff --git a/HttpClientStudy.UnitTest/HttpResponses/HttpResponseContentTest.cs b/HttpClientStudy.UnitTest/HttpResponses/HttpResponseContentTest.cs
new file mode 100644
index 0000000..d43283d
--- /dev/null
+++ b/HttpClientStudy.UnitTest/HttpResponses/HttpResponseContentTest.cs
@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace HttpClientStudy.UnitTest.HttpResponses
+{
+ public class HttpResponseContentTest
+ {
+ private readonly ITestOutputHelper _output;
+
+ public HttpResponseContentTest(ITestOutputHelper testOutput)
+ {
+ _output = testOutput;
+ }
+
+ [Fact]
+ public void Test()
+ {
+
+ }
+ }
+}
diff --git a/HttpClientStudy.UnitTest/StartupTest.cs b/HttpClientStudy.UnitTest/StartupTest.cs
index cec67ca..d384a6f 100644
--- a/HttpClientStudy.UnitTest/StartupTest.cs
+++ b/HttpClientStudy.UnitTest/StartupTest.cs
@@ -13,8 +13,10 @@ namespace HttpClientStudy.UnitTest
[Fact]
public void StartWebApiProject()
{
- var startup = new Startup();
- startup.StartWebApiProject();
+ string projectAndMutexName = WebApiConfigManager.GetWebApiConfigOption().CurrentValue.WebAppMutexName;
+ var mutex = new Mutex(true, projectAndMutexName, out bool createdResult);
+
+ Assert.False(createdResult);
}
}
}
diff --git a/HttpClientStudy.UnitTest/UseJson/UseJsonInHttpTest.cs b/HttpClientStudy.UnitTest/UseJson/UseJsonInHttpTest.cs
new file mode 100644
index 0000000..6c7e6c1
--- /dev/null
+++ b/HttpClientStudy.UnitTest/UseJson/UseJsonInHttpTest.cs
@@ -0,0 +1,23 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace HttpClientStudy.UnitTest.UseJson
+{
+ public class UseJsonInHttpTest
+ {
+ private readonly ITestOutputHelper _output;
+ public UseJsonInHttpTest(ITestOutputHelper testOutput)
+ {
+ _output = testOutput;
+ }
+
+ [Fact]
+ public void Test()
+ {
+
+ }
+ }
+}
diff --git a/HttpClientStudy.UnitTest/UsePolly/UsePollyTest.cs b/HttpClientStudy.UnitTest/UsePolly/UsePollyTest.cs
new file mode 100644
index 0000000..4464663
--- /dev/null
+++ b/HttpClientStudy.UnitTest/UsePolly/UsePollyTest.cs
@@ -0,0 +1,25 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+using Xunit;
+
+namespace HttpClientStudy.UnitTest.UsePolly
+{
+ public class UsePollyTest
+ {
+ private readonly ITestOutputHelper _output;
+ public UsePollyTest(ITestOutputHelper testOutput)
+ {
+ _output = testOutput;
+ }
+
+ [Fact]
+ public void Test()
+ {
+
+ }
+ }
+}
diff --git a/HttpClientStudy.WebApp/Program.cs b/HttpClientStudy.WebApp/Program.cs
index b1ee07d..a473e15 100644
--- a/HttpClientStudy.WebApp/Program.cs
+++ b/HttpClientStudy.WebApp/Program.cs
@@ -130,7 +130,7 @@ namespace HttpClientStudy.WebApp
//֤
builder.Services //֤ܹ
- .AddAuthentication(authOption =>
+ .AddAuthentication(authOption =>
{
authOption.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
authOption.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;