NetPro.Proxy
6.0.15
.NET 6.0
This package targets .NET 6.0. The package is compatible with this framework or higher.
.NET Core 3.1
This package targets .NET Core 3.1. The package is compatible with this framework or higher.
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package NetPro.Proxy --version 6.0.15
NuGet\Install-Package NetPro.Proxy -Version 6.0.15
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="NetPro.Proxy" Version="6.0.15" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NetPro.Proxy --version 6.0.15
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: NetPro.Proxy, 6.0.15"
#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.
// Install NetPro.Proxy as a Cake Addin #addin nuget:?package=NetPro.Proxy&version=6.0.15 // Install NetPro.Proxy as a Cake Tool #tool nuget:?package=NetPro.Proxy&version=6.0.15
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
NetPro.Proxy远程调用
此库已归档,推荐直接使用原生组件,说明请查阅 WebApiClientCore使用说明
归档原因: 原生组件使用已足够方便,没有再次封装意义。
最佳使用建议
WebApiClientCore 组件已屏蔽了过多细节,使用已足够便捷,推荐按作者说明使用。
接口配置建议以配置文件方式
配置建议按以下标准
"Remoting": {
"IUserApi": {//接口定义,与代码interface一致
"HttpHost": "http://www.user.com/",
"UseParameterPropertyValidate": false,
"UseReturnValuePropertyValidate": false,
"JsonSerializeOptions": {
"IgnoreNullValues": true,
"WriteIndented": false
}
},
"IAdminApi": {//接口定义,与代码interface一致
"HttpHost": "http://www.admin.com/",
"UseParameterPropertyValidate": false,
"UseReturnValuePropertyValidate": false,
"JsonSerializeOptions": {
"IgnoreNullValues": true,
"WriteIndented": false
}
}
}
定义远程接口
/// <summary>
/// 记得要实现IHttpApi
/// </summary>
public interface IUserApi : IHttpApi
{
[HttpGet("api/users/{id}")]
Task<User> GetAsync(string id);
...
}
public interface IAdminApi : IHttpApi
{
[HttpGet("api/users/{id}")]
Task<User> GetAsync(string id);
...
}
注册
var sectionUser = configuration.GetSection($"Remoting:{nameof(ITaosProxy)}");
services.AddHttpApi<ITaosProxy>().ConfigureHttpApi(section).ConfigureHttpApi(o =>
{
// 符合国情的不标准时间格式,有些接口就是这么要求必须不标准
o.JsonSerializeOptions.Converters.Add(new JsonDateTimeConverter("yyyy-MM-dd HH:mm:ss"));
});
var sectionAdmin = configuration.GetSection($"Remoting:{nameof(IAdminApi)}");
services.AddHttpApi<IAdminApi>().ConfigureHttpApi(section).ConfigureHttpApi(o =>
{
// 符合国情的不标准时间格式,有些接口就是这么要求必须不标准
o.JsonSerializeOptions.Converters.Add(new JsonDateTimeConverter("yyyy-MM-dd HH:mm:ss"));
});
使用
public class MyService
{
private readonly IUserApi userApi;
public MyService(IUserApi userApi)
{
this.userApi = userApi;
}
}
以下为过时的文档
使用
- 如果已添加环境变量ASPNETCORE_HOSTINGSTARTUPASSEMBLIES=NetPro.Satrtup 启用自动初始化,添加appsetting.json 配置即可
appsetting.json
- 增加以下配置节点
"NetProProxyOption": {
"AssemblyPattern": "^XXX.*.Proxy$",//批量注入程序集的正则,此处表示将XXX开头,Proxy结尾的程序集中使用了NetProProxy功能的接口批量注入
"InterfacePattern": "^I.*.Proxy$", //I开头,Proxy结尾的接口
"IExampleProxy": "http://localhost:5000",//名称要与具体定义的接口名称一致,例如此项对应的接口定义为 public interface IExampleProxy{}
"IBaiduProxy": "http://baidu.com"
}
启用服务
如果没添加ASPNETCORE_HOSTINGSTARTUPASSEMBLIES=NetPro.Satrtup环境变量,按以下方式注入服务,并添加上一条appsetting.json 节点配置即可
public void ConfigureServices(IServiceCollection services)
{
services.AddFileProcessService();
var typeFinder = services.BuildServiceProvider().GetRequiredService<ITypeFinder>();
services.AddHttpProxy(configuration, typeFinder, configuration.GetValue<string>("MicroServicesEndpoint:Assembly", string.Empty));
}
使用
定义服务
public interface IExampleProxy //命名对应appsetting.json 中的Example节点
{
[HttpGet("")]//HttpGet服务
[WebApiClientFilter]//服务过滤器
ITask<dynamic> GetAsync([Parameter(Kind.Query)]string account);
[HttpPost("api/v1/NetProgoods/list")]
[Timeout(10 * 1000)] // 10s超时
[WebApiClientFilter]
ITask<dynamic> GetGoodsList(int appid, string appVersion);
// POST api/user
[HttpPost("api/user")]
[WebApiClientFilter]
ITask<dynamic> AddAsync([FormContent] dynamic user);
/// <summary>
/// 登录
/// </summary>
/// <param name="username"></param>
/// <param name="password"></param>
/// <param name="Captcha"></param>
/// <returns></returns>
[HttpPost("/api/ldap")]
[Timeout(10 * 1000)] // 10s超时
[JsonReturn(Enable = false)]
[Cache(60 * 1000)]//接口缓存
[WebApiClientFilter]
ITask<dynamic> LoginByPwd([Uri] string url, [Parameter(Kind.Query)] string username, string password, string Captcha);
}
服务注入
public class HttpProxyController : ControllerBase
{
private readonly ILogger _logger;
private readonly IExampleProxy _exampleProxy;
//构造函数注入
public HttpProxyController(
ILogger<DatabaseCurdController> logger
IExampleProxy exampleProxy)
{
_logger = logger;
_exampleProxy = exampleProxy;
}
[HttpGet("getorcreate")]
[PostResponseCache(Duration = 2)]
[ProducesResponseType(200, Type = typeof(ResponseResult))]
public async Task<IActionResult> GetOrCreateAsync(uint id)
{
await _exampleProxy.AddAsync("");//直接使用定义的接口
return Ok();
}
}
使用过滤
复制以下代码放在请求方法顶部以特性方式使用,可实现方法的请求与响应的拦截处理,如需个性化处理,以此作为模板稍作改动即可
/// <summary>
/// 过滤器
/// </summary>
public class WebApiClientFilter : ApiFilterAttribute
{
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override Task OnRequestAsync(ApiRequestContext context)
{
//请求开始前做的拦截
var uri= context.HttpContext.RequestMessage.RequestUri;
Console.WriteLine($"request uri is:{uri}");
return Task.CompletedTask;
}
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override Task OnResponseAsync(ApiResponseContext context)
{
//对于响应做的拦截
Console.WriteLine($"HasResult:{context.ResultStatus}");
Console.WriteLine($"context.Result:{context.Result}");
var resultString = context.HttpContext.ResponseMessage.Content.ReadAsStringAsync().Result;
Console.WriteLine($"ReadAsStringAsync(): {resultString}");
Console.WriteLine($"StatusCode: {context.HttpContext.ResponseMessage.StatusCode}");
return Task.CompletedTask;
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
.NET Core | netcoreapp3.1 is compatible. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETCoreApp 3.1
- NetPro.Startup (>= 6.0.15)
- NetPro.TypeFinder (>= 6.0.15)
- WebApiClientCore (>= 2.0.2)
-
net6.0
- NetPro.Startup (>= 6.0.15)
- NetPro.TypeFinder (>= 6.0.15)
- WebApiClientCore (>= 2.0.2)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on NetPro.Proxy:
Package | Downloads |
---|---|
NetPro.Web.Core
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
6.0.16 | 190 | 7/24/2023 |
6.0.15 | 433 | 7/19/2022 |
6.0.14 | 437 | 7/10/2022 |
6.0.13 | 436 | 6/15/2022 |
6.0.12 | 427 | 6/15/2022 |
6.0.11 | 408 | 6/15/2022 |
6.0.10 | 428 | 6/11/2022 |
6.0.9 | 433 | 6/8/2022 |
6.0.8 | 438 | 5/26/2022 |
6.0.8-beta.3 | 124 | 5/24/2022 |
6.0.8-beta.2 | 117 | 5/24/2022 |
6.0.7 | 450 | 5/18/2022 |
6.0.6 | 454 | 4/28/2022 |
6.0.5 | 430 | 3/30/2022 |
6.0.5-beta.20 | 116 | 4/27/2022 |
6.0.5-beta.19 | 123 | 4/25/2022 |
6.0.5-beta.18 | 124 | 4/22/2022 |
6.0.5-beta.17 | 121 | 4/16/2022 |
6.0.5-beta.16 | 125 | 4/8/2022 |
6.0.5-beta.15 | 123 | 4/8/2022 |
6.0.5-beta.14 | 135 | 4/7/2022 |
6.0.5-beta.13 | 128 | 4/7/2022 |
6.0.5-beta.12 | 126 | 4/6/2022 |
6.0.5-beta.11 | 122 | 4/6/2022 |
6.0.5-beta.10 | 125 | 3/31/2022 |
6.0.5-beta.9 | 136 | 3/26/2022 |
6.0.5-beta.8 | 127 | 3/22/2022 |
6.0.5-beta.7 | 125 | 3/21/2022 |
6.0.5-beta.6 | 126 | 3/14/2022 |
6.0.5-beta.5 | 125 | 3/2/2022 |
6.0.5-beta.4 | 134 | 2/22/2022 |
6.0.5-beta.3 | 124 | 2/18/2022 |
6.0.5-beta.2 | 124 | 2/18/2022 |
6.0.5-beta.1 | 128 | 2/16/2022 |
6.0.4 | 443 | 2/10/2022 |
6.0.3 | 419 | 2/9/2022 |
6.0.3-beta.9 | 129 | 2/10/2022 |
6.0.3-beta.7 | 152 | 1/27/2022 |
6.0.3-beta.6 | 141 | 1/19/2022 |
6.0.3-beta.5 | 149 | 1/17/2022 |
6.0.3-beta.4 | 149 | 1/16/2022 |
6.0.3-beta.3 | 152 | 1/13/2022 |
6.0.3-beta.2 | 175 | 1/11/2022 |
6.0.3-beta.1 | 170 | 1/11/2022 |
6.0.2 | 328 | 1/6/2022 |
6.0.1 | 979 | 12/3/2021 |
3.1.11 | 375 | 11/19/2021 |
3.1.10 | 449 | 7/29/2021 |
1.0.0 | 2,998 | 7/1/2021 |