![]() |
VOOZH | about |
dotnet add package NetPro.Proxy --version 6.0.16
NuGet\Install-Package NetPro.Proxy -Version 6.0.16
<PackageReference Include="NetPro.Proxy" Version="6.0.16" />
<PackageVersion Include="NetPro.Proxy" Version="6.0.16" />Directory.Packages.props
<PackageReference Include="NetPro.Proxy" />Project file
paket add NetPro.Proxy --version 6.0.16
#r "nuget: NetPro.Proxy, 6.0.16"
#:package NetPro.Proxy@6.0.16
#addin nuget:?package=NetPro.Proxy&version=6.0.16Install as a Cake Addin
#tool nuget:?package=NetPro.Proxy&version=6.0.16Install as a Cake Tool
此库已归档,推荐直接使用原生组件,说明请查阅 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;
}
}
"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 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp3.1 netcoreapp3.1 is compatible. |
Showing the top 1 NuGet packages that depend on NetPro.Proxy:
| Package | Downloads |
|---|---|
|
NetPro.Web.Core
Package Description |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 6.0.16 | 398 | 7/24/2023 |
| 6.0.15 | 609 | 7/19/2022 |
| 6.0.14 | 600 | 7/10/2022 |
| 6.0.13 | 609 | 6/15/2022 |
| 6.0.12 | 584 | 6/15/2022 |
| 6.0.11 | 566 | 6/15/2022 |
| 6.0.10 | 595 | 6/11/2022 |
| 6.0.9 | 620 | 6/8/2022 |
| 6.0.8 | 603 | 5/26/2022 |
| 6.0.8-beta.3 | 269 | 5/24/2022 |
| 6.0.8-beta.2 | 254 | 5/24/2022 |
| 6.0.7 | 617 | 5/18/2022 |
| 6.0.6 | 631 | 4/28/2022 |
| 6.0.5-beta.20 | 275 | 4/27/2022 |
| 6.0.5-beta.19 | 279 | 4/25/2022 |
| 6.0.5-beta.18 | 290 | 4/22/2022 |
| 6.0.5-beta.17 | 269 | 4/16/2022 |
| 6.0.5-beta.16 | 305 | 4/8/2022 |
| 6.0.5-beta.15 | 266 | 4/8/2022 |
| 6.0.5-beta.14 | 282 | 4/7/2022 |