Galosys.Foundation.HttpClient 26.5.12.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package Galosys.Foundation.HttpClient --version 26.5.12.2
                    
NuGet\Install-Package Galosys.Foundation.HttpClient -Version 26.5.12.2
                    
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.5.12.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Galosys.Foundation.HttpClient" Version="26.5.12.2" />
                    
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.5.12.2
                    
#r "nuget: Galosys.Foundation.HttpClient, 26.5.12.2"
                    
#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.5.12.2
                    
#: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.5.12.2
                    
Install as a Cake Addin
#tool nuget:?package=Galosys.Foundation.HttpClient&version=26.5.12.2
                    
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>();

核心类

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

配置说明

请求头传播

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

服务发现

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

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

依赖包

  • Microsoft.AspNetCore.HeaderPropagation
  • Microsoft.Extensions.Http.Polly

依赖项目

  • 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 net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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.5.15.1 38 5/15/2026
26.5.12.3 134 5/12/2026
26.5.12.2 113 5/12/2026
26.4.27.1-rc1 138 4/26/2026
26.4.25.1-rc1 138 4/25/2026
26.4.22.2-rc7 134 4/22/2026
26.4.22.2-rc6 131 4/22/2026
26.4.22.2-rc4 135 4/22/2026
26.4.22.2-rc3 118 4/22/2026
26.4.19.1-rc1 125 4/19/2026
26.4.12.8-rc1 123 4/12/2026
26.4.12.7-rc1 127 4/12/2026
26.4.12.5-rc1 102 4/12/2026
26.1.30.1-rc1 196 1/30/2026
26.1.29.1 198 1/29/2026
26.1.28.5 199 1/28/2026
26.1.28.4 203 1/28/2026
26.1.28.2 205 1/28/2026
26.1.23.6 213 1/23/2026
26.1.21.1 209 1/21/2026
Loading failed