NetPro.Proxy
6.0.16
dotnet add package NetPro.Proxy --version 6.0.16
NuGet\Install-Package NetPro.Proxy -Version 6.0.16
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.16" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NetPro.Proxy --version 6.0.16
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: NetPro.Proxy, 6.0.16"
#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.16
// Install NetPro.Proxy as a Cake Tool
#tool nuget:?package=NetPro.Proxy&version=6.0.16
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.16)
- NetPro.TypeFinder (>= 6.0.16)
- WebApiClientCore (>= 2.0.2)
-
net6.0
- NetPro.Startup (>= 6.0.16)
- NetPro.TypeFinder (>= 6.0.16)
- 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 | 173 | 7/24/2023 |
6.0.15 | 409 | 7/19/2022 |
6.0.14 | 426 | 7/10/2022 |
6.0.13 | 409 | 6/15/2022 |
6.0.12 | 416 | 6/15/2022 |
6.0.11 | 384 | 6/15/2022 |
6.0.10 | 404 | 6/11/2022 |
6.0.9 | 396 | 6/8/2022 |
6.0.8 | 414 | 5/26/2022 |
6.0.8-beta.3 | 113 | 5/24/2022 |
6.0.8-beta.2 | 106 | 5/24/2022 |
6.0.7 | 421 | 5/18/2022 |
6.0.6 | 427 | 4/28/2022 |
6.0.5 | 404 | 3/30/2022 |
6.0.5-beta.20 | 105 | 4/27/2022 |
6.0.5-beta.19 | 114 | 4/25/2022 |
6.0.5-beta.18 | 113 | 4/22/2022 |
6.0.5-beta.17 | 110 | 4/16/2022 |
6.0.5-beta.16 | 114 | 4/8/2022 |
6.0.5-beta.15 | 113 | 4/8/2022 |
6.0.5-beta.14 | 124 | 4/7/2022 |
6.0.5-beta.13 | 119 | 4/7/2022 |
6.0.5-beta.12 | 116 | 4/6/2022 |
6.0.5-beta.11 | 113 | 4/6/2022 |
6.0.5-beta.10 | 114 | 3/31/2022 |
6.0.5-beta.9 | 125 | 3/26/2022 |
6.0.5-beta.8 | 115 | 3/22/2022 |
6.0.5-beta.7 | 114 | 3/21/2022 |
6.0.5-beta.6 | 113 | 3/14/2022 |
6.0.5-beta.5 | 113 | 3/2/2022 |
6.0.5-beta.4 | 126 | 2/22/2022 |
6.0.5-beta.3 | 114 | 2/18/2022 |
6.0.5-beta.2 | 116 | 2/18/2022 |
6.0.5-beta.1 | 120 | 2/16/2022 |
6.0.4 | 420 | 2/10/2022 |
6.0.3 | 410 | 2/9/2022 |
6.0.3-beta.9 | 119 | 2/10/2022 |
6.0.3-beta.7 | 140 | 1/27/2022 |
6.0.3-beta.6 | 134 | 1/19/2022 |
6.0.3-beta.5 | 142 | 1/17/2022 |
6.0.3-beta.4 | 140 | 1/16/2022 |
6.0.3-beta.3 | 145 | 1/13/2022 |
6.0.3-beta.2 | 168 | 1/11/2022 |
6.0.3-beta.1 | 162 | 1/11/2022 |
6.0.2 | 304 | 1/6/2022 |
6.0.1 | 968 | 12/3/2021 |
3.1.11 | 352 | 11/19/2021 |
3.1.10 | 420 | 7/29/2021 |
1.0.0 | 2,919 | 7/1/2021 |