Galosys.Foundation.HttpClient 26.9.23.1

dotnet add package Galosys.Foundation.HttpClient --version 26.9.23.1
                    
NuGet\Install-Package Galosys.Foundation.HttpClient -Version 26.9.23.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Galosys.Foundation.HttpClient" Version="26.9.23.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Galosys.Foundation.HttpClient" Version="26.9.23.1" />
                    
Directory.Packages.props
<PackageReference Include="Galosys.Foundation.HttpClient" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Galosys.Foundation.HttpClient --version 26.9.23.1
                    
#r "nuget: Galosys.Foundation.HttpClient, 26.9.23.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Galosys.Foundation.HttpClient@26.9.23.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Galosys.Foundation.HttpClient&version=26.9.23.1
                    
Install as a Cake Addin
#tool nuget:?package=Galosys.Foundation.HttpClient&version=26.9.23.1
                    
Install as a Cake Tool

Galosys.Foundation.HttpClient

成熟度: 🟢 稳定 — 生产可用,测试充分,活跃维护

简介

Galosys.Foundation.HttpClient 是基于 Microsoft.Extensions.Http 和 Polly 的 HTTP 客户端模块,提供声明式 REST 客户端、请求头传播、服务发现集成、委托处理器等能力,简化微服务间的 HTTP 调用。

功能特性

  • 声明式 REST 客户端 - 通过接口定义 HTTP 调用
  • 请求头传播 - 自动传递 Authorization、租户等请求头
  • 服务发现集成 - 支持服务名称解析
  • 委托处理器 - 可扩展的请求/响应管道
  • 作用域感知 - 支持 DI 作用域的 HttpClient 工厂

安装

<PackageReference Include="Galosys.Foundation.HttpClient" Version="x.x.x" />

使用

注册服务

// Program.cs 或 Module 中
services.AddRestClient();

定义 REST 客户端接口

using System.Net.Http;
using System.Threading.Tasks;

[RestClient("https://api.example.com")]
public interface IUserService
{
    [GetMapping("/api/users/{id}")]
    Task<UserDto> GetUserAsync(int id);

    [PostMapping("/api/users")]
    Task CreateUserAsync(CreateUserRequest request);

    [GetMapping("/api/users")]
    Task<List<UserDto>> ListUsersAsync(int page = 1, int size = 10);
}

注入使用

public class UserController : ControllerBase
{
    private readonly IUserService _userService;

    public UserController(IUserService userService)
    {
        _userService = userService;
    }

    [HttpGet("{id}")]
    public async Task<UserDto> GetUser(int id)
    {
        return await _userService.GetUserAsync(id);
    }
}

自定义委托处理器

通过 AddHttpClient 注册自定义 DelegatingHandler:

services.AddHttpClient("MyClient")
    .AddHttpMessageHandler<AuthTokenHandler>();

public class AuthTokenHandler : DelegatingHandler
{
    private readonly ITokenProvider _tokenProvider;

    public AuthTokenHandler(ITokenProvider tokenProvider)
    {
        _tokenProvider = tokenProvider;
    }

    protected override async Task<HttpResponseMessage> SendAsync(
        HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var token = await _tokenProvider.GetTokenAsync();
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
        return await base.SendAsync(request, cancellationToken);
    }
}

作用域感知 HttpClient

services.AddHttpClient("MyClient")
    .AddScopeAwareHttpHandler<ScopedDelegatingHandler>();

出向 HTTP 诊断

AddScopeAwareHttpHandler 自动注册 Microsoft.Extensions.Http.Diagnostics 的可观测性管道:

  • AddRedaction()(先于 LatencyTelemetry 注册,幂等)
  • AddHttpClientLatencyTelemetry() — 产出分阶段延迟分解(DNS/连接/TLS/首字节/GC)
  • AddExtendedHttpClientLogging() — 记录方法、URL、状态码、耗时
services.AddHttpClient("MyClient")
    .AddScopeAwareHttpHandler<ScopedDelegatingHandler>();

路由参数默认 Strict 脱敏(/api/users/{userId} 的序列值被掩码)。该默认已固化在 AddScopeAwareHttpHandler 内部链中;如自建客户端链,可通过 options.RequestPathParameterRedactionMode 覆盖:

services.AddHttpClient("MyClient")
    .AddExtendedHttpClientLogging(o =>
    {
        o.LogBody = false;
        o.RequestPathParameterRedactionMode = HttpRouteParameterRedactionMode.None; // 调试用
    });

注意:直接使用原生 AddHttpClient(...)(不经 AddScopeAwareHttpHandler)时,需自行 services.AddRedaction(); + services.AddHttpClientLatencyTelemetry();,且 AddRedaction() 必须先调用。

核心类

类 说明
RestClientAttribute 标记接口为 REST 客户端,指定服务地址
GetMappingAttribute 标记方法为 GET 请求,指定路径模板
PostMappingAttribute 标记方法为 POST 请求,指定路径模板
RestClientDispatchProxy 动态代理实现,处理接口方法调用
ScopeAwareHttpClientFactory 支持 DI 作用域的 HttpClient 工厂

配置说明

请求头传播

Web 应用自动启用请求头传播(通过 AddHeaderPropagation)。

服务发现

通过 AddServiceDiscovery() 集成服务发现,支持:

  • 配置中心服务注册
  • Consul 服务发现
  • Nacos 服务发现

依赖包

  • Microsoft.AspNetCore.HeaderPropagation
  • Microsoft.Extensions.Http.Polly
  • Microsoft.Extensions.Http.Diagnostics
  • Microsoft.Extensions.Compliance.Redaction

依赖项目

  • Galosys.Foundation.Core

目录结构

Galosys.Foundation.HttpClient/
├── Galosys/Foundation/HttpClient/
│   └── RestClientDispatchProxy.cs      # 动态代理实现
├── Microsoft/Extensions/DependencyInjection/
│   ├── HttpClientServiceCollectionExntensions.cs  # 服务注册扩展
│   └── ScopeAwareHttpClientFactory.cs  # 作用域感知工厂
├── RestClientModule.cs                 # 模块注册
└── README.md

注意事项

  • 接口方法需使用 [GetMapping]、[PostMapping] 特性标记(来自 System.Net.Http 命名空间)
  • 简单类型参数自动拼接到 URL 查询字符串
  • 复杂类型参数自动序列化为 JSON(POST 请求体)
  • 方法缺少 [GetMapping]/[PostMapping] 特性时会抛出 InvalidOperationException
  • 默认超时时间为 10 秒
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Galosys.Foundation.HttpClient:

Package Downloads
Galosys.Foundation.NacosNaming

Galosys.Foundation快速开发库

Galosys.Foundation.Consul

Galosys.Foundation快速开发库

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
26.9.23.1 75 9/23/2026
26.9.16.1 112 9/16/2026
26.9.15.1 108 9/15/2026
26.9.14.1 111 9/14/2026
26.9.10.1 113 9/10/2026
26.9.3.1 100 9/3/2026
26.8.29.1 128 8/31/2026
26.8.26.1 129 8/26/2026
26.8.23.1 139 8/23/2026
26.8.21.1 150 8/21/2026
26.8.20.1 136 8/20/2026
26.8.18.1 134 8/18/2026
26.8.17.1 131 8/17/2026
26.8.13.2 137 8/13/2026
26.8.13.1 134 8/13/2026
26.8.12.2 129 8/12/2026
26.8.12.1 131 8/12/2026
26.8.10.1 131 8/10/2026
26.8.5.1 142 8/5/2026
26.8.4.1 147 8/4/2026
Loading failed