OpenRobot.Framework.WebApiProxy
1.0.0
dotnet add package OpenRobot.Framework.WebApiProxy --version 1.0.0
NuGet\Install-Package OpenRobot.Framework.WebApiProxy -Version 1.0.0
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="OpenRobot.Framework.WebApiProxy" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpenRobot.Framework.WebApiProxy" Version="1.0.0" />
<PackageReference Include="OpenRobot.Framework.WebApiProxy" />
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 OpenRobot.Framework.WebApiProxy --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: OpenRobot.Framework.WebApiProxy, 1.0.0"
#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 OpenRobot.Framework.WebApiProxy@1.0.0
#: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=OpenRobot.Framework.WebApiProxy&version=1.0.0
#tool nuget:?package=OpenRobot.Framework.WebApiProxy&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
OpenRobot.Framework.WebApiProxy
一个强大的 .NET 8.0 HTTP 客户端库,专为与基于 ABP 框架的 Web API 进行交互而设计。它为 HttpClient 提供了类型安全的包装器,内置支持多租户、JWT 授权、文件上传下载以及 SignalR 实时通信。
主要特性
- 类型安全的 API 调用 - 强类型响应支持,减少运行时错误
- ABP 框架集成 - 原生支持 ABP 的多租户、授权和响应格式
- 文件操作 - 内置文件上传下载,支持进度跟踪
- SignalR 实时通信 - 内置自动重连的 SignalR 客户端
- UI 回调支持 - 可集成加载指示器和错误提示
- System.Text.Json - 使用高性能的 .NET 内置 JSON 序列化
- 简洁的 API - 链式配置,易于使用
安装
dotnet add package OpenRobot.Framework.WebApiProxy
或在 Visual Studio 的 NuGet 包管理器控制台中运行:
Install-Package OpenRobot.Framework.WebApiProxy
快速开始
1. 配置 API 连接
using OpenRobot.Framework.OpenApiProxy;
// 设置 API 主机地址
OpenApiFactory.SetBaseApiHost("https://api.example.com");
// 设置 JWT Bearer 令牌
OpenApiFactory.SetAuthorization("your-jwt-token");
// 设置租户 ID(多租户应用)
OpenApiFactory.SetTenantId("tenant-id-here");
2. 发起 HTTP 请求
using OpenRobot.Framework;
// GET 请求
var users = await OpenApiProxyFactory.GetRequest<List<UserDto>>("api/users");
// 带查询参数的 GET 请求
var result = await OpenApiProxyFactory.GetRequest("api/users", AutoLoading: true,
new RequestParm("skipCount", 0),
new RequestParm("maxResultCount", 10));
// POST 请求
var newUser = await OpenApiProxyFactory.PostRequest<UserDto>("api/users", new {
Name = "张三",
Email = "zhangsan@example.com"
});
// PUT 请求
await OpenApiProxyFactory.PutRequest("api/users/1", updateUserDto);
// DELETE 请求
await OpenApiProxyFactory.DeleteRequest("api/users/1");
使用示例
文件上传
await OpenApiProxyFactory.PostFileUploadRequest(
"api/upload",
@"C:\Documents\file.pdf",
"file", // 表单字段名
AutoLoading: true,
new RequestParm("category", "documents")
);
文件下载(带进度)
await OpenApiProxyFactory.GetFileDownloadRequest(
"api/download/1",
@"C:\Downloads\file.pdf",
progress => Console.WriteLine($"下载进度: {progress}%")
);
SignalR 实时通信
using OpenRobot.Framework.WebApiProxy.Realtime;
// 定义事件处理器
public class ChatEventHandler : ISignalrOnEvent
{
public string OnMethodName => "ReceiveMessage";
public Type[] MethodTypes => new[] { typeof(string), typeof(string) };
public Task Dowork(object?[] data)
{
var user = data[0]?.ToString();
var message = data[1]?.ToString();
Console.WriteLine($"{user}: {message}");
return Task.CompletedTask;
}
}
// 创建并启动连接
var proxy = new ClientSignalrProxy("https://api.example.com", "chathub", new ChatEventHandler());
proxy.ClientLogEvent += (level, message) => Console.WriteLine($"[{level}] {message}");
await proxy.Start(); // 内置自动重连
// 调用服务器方法
await proxy.InvokeProxy("SendMessage", new object[] { "你好", "世界" });
await proxy.Stop();
UI 加载状态集成
public class UILoadingHandler : IOpenApiCustuomRequest
{
public void StartRequest(string requestInfo)
{
// 显示加载指示器
Application.Current.MainWindow.IsEnabled = false;
}
public void EndRequest()
{
// 隐藏加载指示器
Application.Current.MainWindow.IsEnabled = true;
}
public void FailRequest(string error)
{
// 显示错误对话框
MessageBox.Show($"请求失败: {error}");
}
}
// 注册 UI 回调
OpenApiFactory.SetCustuomRequest(new UILoadingHandler());
配置选项
| 方法 | 说明 | 示例 |
|---|---|---|
SetBaseApiHost() |
设置 API 主机地址 | OpenApiFactory.SetBaseApiHost("https://api.example.com") |
SetAuthorization() |
设置 JWT Bearer 令牌 | OpenApiFactory.SetAuthorization("your-token") |
SetTenantId() |
设置 ABP 租户 ID | OpenApiFactory.SetTenantId("tenant-1") |
SetHttpVersion() |
设置 HTTP 版本 | OpenApiFactory.SetHttpVersion(2, 0) |
SetCustuomRequest() |
设置请求回调处理器 | OpenApiFactory.SetCustuomRequest(new MyHandler()) |
ABP 框架集成
本库自动处理 ABP 框架的特定请求头和响应格式:
自动添加的请求头
Abp.TenantId: 多租户标识符Authorization: Bearer 令牌.AspNetCore.Culture: 文化设置(默认为 "zh-Hans")Cache-Control: 设置为 "no-cache"
支持的 ABP 响应格式
{
"success": true,
"result": { ... },
"error": {
"code": 0,
"message": "错误信息",
"details": "详细信息",
"validationErrors": "..."
},
"targetUrl": "",
"unAuthorizedRequest": false
}
依赖项
| 包名 | 版本 | 用途 |
|---|---|---|
| Microsoft.AspNetCore.SignalR.Client | 10.0.3 | SignalR 客户端 |
| Microsoft.AspNetCore.SignalR.Protocols.MessagePack | 10.0.3 | MessagePack 二进制协议 |
| System.Text.Json | 内置 | JSON 序列化(驼峰命名) |
系统要求
- .NET 8.0 或更高版本
API 参考
主要类
| 类名 | 命名空间 | 说明 |
|---|---|---|
OpenApiFactory |
OpenRobot.Framework.OpenApiProxy |
配置类 |
OpenApiProxyFactory |
OpenRobot.Framework |
HTTP 请求工厂 |
ClientSignalrProxy |
OpenRobot.Framework.WebApiProxy.Realtime |
SignalR 客户端 |
RequestResult<T> |
OpenRobot.Framework.OpenApiProxy |
响应结果 |
IOpenApiCustuomRequest |
OpenRobot.Framework.OpenApiProxy |
请求回调接口 |
数据模型
// 请求参数
public class RequestParm
{
public string Name { get; set; }
public object Value { get; set; }
}
// 错误信息
public class RequestError
{
public int Code { get; set; }
public string Message { get; set; }
public string Details { get; set; }
public string ValidationErrors { get; set; }
}
// 响应结果
public class RequestResult<T>
{
public bool Success { get; set; }
public T Result { get; set; }
public RequestError Error { get; set; }
public string TargetUrl { get; set; }
public bool UnAuthorizedRequest { get; set; }
}
许可证
本项目采用 MIT 许可证。
作者
OpenRobot
反馈与贡献
欢迎通过以下方式提供反馈:
- 提交 Issue
- 发起 Pull Request
- 联系维护者
注意: 本库专为与 ABP 框架后端配合使用而设计。如需与其他 API 交互,可能需要额外的适配工作。
| Product | Versions 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.
-
net8.0
- Microsoft.AspNetCore.SignalR.Client (>= 10.0.3)
- Microsoft.AspNetCore.SignalR.Protocols.MessagePack (>= 10.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 96 | 4/16/2026 |