main
wanggaofeng 1 year ago
parent c4cd9f08d6
commit 822d4b262a

@ -0,0 +1,2 @@
HttpClientѧϰ
==============

@ -7,6 +7,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.1" />
<PackageReference Include="Polly" Version="8.2.1" />
<PackageReference Include="Polly.Caching.IDistributedCache" Version="1.0.4" />
<PackageReference Include="Polly.Extensions.Http" Version="3.0.0" />
<PackageReference Include="System.Net.Http.Json" Version="8.0.0" />
</ItemGroup>

@ -10,11 +10,11 @@ namespace HttpClientStudy.Core
/// <summary>
/// 管道方式 HttpClient客户端
/// </summary>
public class PipelineClient
public class PipelineHttpClient
{
public List<DelegatingHandler> HttpMessageHandlers { get; set; }
public PipelineClient()
public PipelineHttpClient()
{
HttpMessageHandlers = new List<DelegatingHandler>()
{
@ -24,7 +24,7 @@ namespace HttpClientStudy.Core
}
public PipelineClient(List<DelegatingHandler> httpMessageHandlers)
public PipelineHttpClient(List<DelegatingHandler> httpMessageHandlers)
{
HttpMessageHandlers = httpMessageHandlers;
if (httpMessageHandlers == null || httpMessageHandlers?.Count == 0)

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Polly;
using Polly.Utilities;
namespace HttpClientStudy.Core
{
/// <summary>
/// 使用Polly的 HttpClient
/// </summary>
public class PollyHttpClient
{
}
}

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HttpClientStudy.Core
{
/// <summary>
/// 使用工厂模式的客户端
/// 缺点使用IoC框架
/// </summary>
public class UsedFactoryHttpClient
{
}
}

@ -13,7 +13,7 @@ namespace HttpClientStudy.UnitTest
[Fact]
public async Task Test()
{
HttpClient client = new PipelineClient().CreateHttpClient();
HttpClient client = new PipelineHttpClient().CreateHttpClient();
var r = await client.GetAsync("https://www.baidu.com");
r.EnsureSuccessStatusCode();

@ -11,7 +11,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpClientStudy.WebApp", "H
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpClientStudy.UnitTest", "HttpClientStudy.UnitTest\HttpClientStudy.UnitTest.csproj", "{A64DAAE3-9FDA-4B37-AE87-FA0812DBF5A9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpClientStudy.Service", "HttpClientStudy.Service\HttpClientStudy.Service.csproj", "{5FB70481-4D39-4C90-9DAC-9453BBABD860}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpClientStudy.Service", "HttpClientStudy.Service\HttpClientStudy.Service.csproj", "{5FB70481-4D39-4C90-9DAC-9453BBABD860}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{56D9132E-6D9B-4D4B-B82C-D8F74AA4373A}"
ProjectSection(SolutionItems) = preProject
Docs\说明.md = Docs\说明.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

Loading…
Cancel
Save