NICWebApi.Extensions.RemoteRequest 1.0.41

dotnet add package NICWebApi.Extensions.RemoteRequest --version 1.0.41
                    
NuGet\Install-Package NICWebApi.Extensions.RemoteRequest -Version 1.0.41
                    
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="NICWebApi.Extensions.RemoteRequest" Version="1.0.41" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NICWebApi.Extensions.RemoteRequest" Version="1.0.41" />
                    
Directory.Packages.props
<PackageReference Include="NICWebApi.Extensions.RemoteRequest" />
                    
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 NICWebApi.Extensions.RemoteRequest --version 1.0.41
                    
#r "nuget: NICWebApi.Extensions.RemoteRequest, 1.0.41"
                    
#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 NICWebApi.Extensions.RemoteRequest@1.0.41
                    
#: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=NICWebApi.Extensions.RemoteRequest&version=1.0.41
                    
Install as a Cake Addin
#tool nuget:?package=NICWebApi.Extensions.RemoteRequest&version=1.0.41
                    
Install as a Cake Tool

NICWebApi.Extensions.RemoteRequest

NICWebApi.Extensions.RemoteRequest 是独立的轻量远程 HTTP 调用扩展库,基于 HttpClientFactory 提供统一请求模型、JSON 序列化、默认请求头、命名客户端、请求/响应拦截器、流式文件传输、上传下载进度和统一响应模型。

它不负责服务端 HTTP 上下文、动态 API、统一返回格式、JWT 认证、Swagger UI 或复杂服务治理;也不会内置某个公司的 token、签名或业务认证规则。字符串扩展方法只作为便捷语法,企业高频调用推荐使用 IRemoteRequestClient、命名客户端、轻量声明式代理或传入由 HttpClientFactory 创建的 HttpClient

快速开始

builder.Services.AddRemoteRequest();
using NICWebApi.Extensions.RemoteRequest.Abstractions;

public sealed class UserGateway
{
    private readonly IRemoteRequestClient _remote;

    public UserGateway(IRemoteRequestClient remote)
    {
        _remote = remote;
    }

    public Task<UserDto?> GetUserAsync(long id, CancellationToken cancellationToken)
    {
        return _remote.GetFromJsonAsync<UserDto>($"https://api.example.com/users/{id}", cancellationToken);
    }
}

需要统一基础地址、默认请求头或多个外部服务时,可使用命名客户端:

builder.Services.AddRemoteRequestClient("billing", options =>
{
    options.BaseAddress = "https://billing.example.com";
    options.DefaultHeaders["X-App"] = "Billing";
});

var client = remoteRequestClientFactory.CreateClient("billing");
var bill = await client.GetFromJsonAsync<BillDto>("/bills/1", cancellationToken);

统一签名、租户头、审计等横切逻辑可使用拦截器:

builder.Services.AddRemoteRequestInterceptor<TenantHeaderInterceptor>();

重试和断路器按客户端显式启用:

builder.Services.AddRemoteRequestClient("billing", options =>
{
    options.BaseAddress = "https://billing.example.com";
})
.AddRemoteRequestResilience();

轻量声明式代理:

builder.Services.AddRemoteRequestProxy<IPeopleRemoteApi>();

[RemoteClient("people")]
public interface IPeopleRemoteApi
{
    [RemoteGet("/users/{id}")]
    Task<UserDto?> GetUserAsync([RemoteRoute] long id, [RemoteQuery("expand")] string expand);
}

表单、上传和下载:

var token = await _remote.PostFormAsync<TokenResponse>("/oauth/token", new Dictionary<string, string>
{
    ["grant_type"] = "client_credentials",
    ["client_id"] = "nic"
});

var upload = await _remote.UploadFileAsync<UploadResponse>("/files", "D:\\files\\a.xlsx");
var bytes = await _remote.GetByteArrayAsync("/files/1", cancellationToken);
await _remote.DownloadFileAsync("/files/1", "D:\\files\\a.xlsx", cancellationToken);

上传和下载进度:

var progress = new Progress<RemoteRequestProgress>(value =>
{
    Console.WriteLine($"{value.BytesTransferred}/{value.TotalBytes}");
});

await _remote.UploadBytesAsync<UploadResponse>("/files", bytes, "a.xlsx", progress);
await _remote.DownloadFileAsync("/files/1", "D:\\files\\a.xlsx", progress, cancellationToken);

便捷字符串扩展:

using NICWebApi.Extensions.RemoteRequest.Extensions;

using var response = await "https://api.example.com/users/1".GetAsync();
var user = await "https://api.example.com/users/1".GetFromJsonAsync<UserDto>();
await "https://api.example.com/files".UploadFileAsync("D:\\files\\a.xlsx");
await "https://api.example.com/files".UploadBytesAsync(bytes, "a.xlsx");
await "https://api.example.com/files".UploadStreamAsync(stream, "a.xlsx");

复用已有 HttpClient

var user = await "/users/1".GetFromJsonAsync<UserDto>(httpClient, cancellationToken: cancellationToken);

更多内容:

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 is compatible.  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 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 (1)

Showing the top 1 NuGet packages that depend on NICWebApi.Extensions.RemoteRequest:

Package Downloads
NICWebApi.Extensions.AspNetCore

NICWebApi ASP.NET Core 一键集成扩展包,组合 DynamicAPI、RESTfulResult、JWT、Mapper、DependencyInjection、Http、RemoteRequest、Caching、Idempotency、Schedule、Messaging 和 OperationLogging 的默认注册。

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.41 144 7/7/2026