NetPro.Proxy
6.0.3-beta.9
This is a prerelease version of NetPro.Proxy.
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.3-beta.9
NuGet\Install-Package NetPro.Proxy -Version 6.0.3-beta.9
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.3-beta.9" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add NetPro.Proxy --version 6.0.3-beta.9
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: NetPro.Proxy, 6.0.3-beta.9"
#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.3-beta.9&prerelease // Install NetPro.Proxy as a Cake Tool #tool nuget:?package=NetPro.Proxy&version=6.0.3-beta.9&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
NetPro.Proxy使用
远程调用组件
使用
- 如果已添加环境变量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 | 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- NetPro.Core (>= 6.0.3-beta.9)
- NetPro.TypeFinder (>= 6.0.3-beta.9)
- 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 | 191 | 7/24/2023 |
6.0.15 | 434 | 7/19/2022 |
6.0.14 | 438 | 7/10/2022 |
6.0.13 | 437 | 6/15/2022 |
6.0.12 | 428 | 6/15/2022 |
6.0.11 | 409 | 6/15/2022 |
6.0.10 | 429 | 6/11/2022 |
6.0.9 | 434 | 6/8/2022 |
6.0.8 | 439 | 5/26/2022 |
6.0.8-beta.3 | 125 | 5/24/2022 |
6.0.8-beta.2 | 118 | 5/24/2022 |
6.0.7 | 451 | 5/18/2022 |
6.0.6 | 455 | 4/28/2022 |
6.0.5 | 432 | 3/30/2022 |
6.0.5-beta.20 | 117 | 4/27/2022 |
6.0.5-beta.19 | 124 | 4/25/2022 |
6.0.5-beta.18 | 125 | 4/22/2022 |
6.0.5-beta.17 | 122 | 4/16/2022 |
6.0.5-beta.16 | 126 | 4/8/2022 |
6.0.5-beta.15 | 124 | 4/8/2022 |
6.0.5-beta.14 | 136 | 4/7/2022 |
6.0.5-beta.13 | 131 | 4/7/2022 |
6.0.5-beta.12 | 127 | 4/6/2022 |
6.0.5-beta.11 | 123 | 4/6/2022 |
6.0.5-beta.10 | 126 | 3/31/2022 |
6.0.5-beta.9 | 137 | 3/26/2022 |
6.0.5-beta.8 | 128 | 3/22/2022 |
6.0.5-beta.7 | 126 | 3/21/2022 |
6.0.5-beta.6 | 127 | 3/14/2022 |
6.0.5-beta.5 | 126 | 3/2/2022 |
6.0.5-beta.4 | 137 | 2/22/2022 |
6.0.5-beta.3 | 125 | 2/18/2022 |
6.0.5-beta.2 | 127 | 2/18/2022 |
6.0.5-beta.1 | 130 | 2/16/2022 |
6.0.4 | 444 | 2/10/2022 |
6.0.3 | 420 | 2/9/2022 |
6.0.3-beta.9 | 130 | 2/10/2022 |
6.0.3-beta.7 | 153 | 1/27/2022 |
6.0.3-beta.6 | 142 | 1/19/2022 |
6.0.3-beta.5 | 152 | 1/17/2022 |
6.0.3-beta.4 | 150 | 1/16/2022 |
6.0.3-beta.3 | 155 | 1/13/2022 |
6.0.3-beta.2 | 176 | 1/11/2022 |
6.0.3-beta.1 | 171 | 1/11/2022 |
6.0.2 | 329 | 1/6/2022 |
6.0.1 | 980 | 12/3/2021 |
3.1.11 | 376 | 11/19/2021 |
3.1.10 | 452 | 7/29/2021 |
1.0.0 | 2,999 | 7/1/2021 |