EasyApiProxy.Core
4.5.0
dotnet add package EasyApiProxy.Core --version 4.5.0
NuGet\Install-Package EasyApiProxy.Core -Version 4.5.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="EasyApiProxy.Core" Version="4.5.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EasyApiProxy.Core" Version="4.5.0" />
<PackageReference Include="EasyApiProxy.Core" />
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 EasyApiProxy.Core --version 4.5.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EasyApiProxy.Core, 4.5.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 EasyApiProxy.Core@4.5.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=EasyApiProxy.Core&version=4.5.0
#tool nuget:?package=EasyApiProxy.Core&version=4.5.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
EasyApiProxy
- 簡單的 Web Api 代理類別產生
- 支援Hawk驗證 與 BearerToken驗證
- Nuget Package Install
- 提供 DefaultApi協定 實作支援 於套件 EasyApiProxy.WebApi
- 提供 用戶端 Hawk驗證 於套件 EasyApiProxy.HawkAuth
呼叫範例
// 建立API 代理物件 Factory 必須重複使用 因為其實做包含一個專用的 HttpClient
// HttpClient 微軟官方明確的說必須公用 否則回有tcp/ip資源不足的問題
var factory = new ApiProxyBuilder()
// 套用 DefaultApi 通訊協議
.UseDefaultApiProtocol("http://localhost:8081/api/Demo")
.Build<IDemoApi>();
// 建立代理物件
using (var proxy = factory.Create()) {
var api = proxy.Api;
// 呼叫Api
var ret1 = await api.Login(new Login { Account = "david", Password = "123" });
// 傳遞Bearer Token 後續呼叫會自動帶上
proxy.SetBearer(ret1.Token);
}
呼叫範例 整合 .net core
var services = new ServiceCollection();
services.AddApiProxy<IDemoApi>(
configApiAction: (sp, builder) =>
{
// 設定 ApiProxy 的預設協定
builder.UseDefaultApiProtocol("http://localhost:5249/api/Demo",
defaltTimeoutSeconds: 30);
// 設定 Basic 驗證
builder.UseBasicAuthorize(new BasicCredential
{
Account = "admin",
PassCode = "admin1234"
});
});
var provider = services.BuildServiceProvider();
using var scope = provider.CreateScope();
var proxy1 = scope.ServiceProvider.GetRequiredService<IApiProxy<IDemoApi>>();
var api1 = scope.ServiceProvider.GetRequiredService<IDemoApi>();
Api 介面定義
// 展示Api 定義
// 注意 DefaultApi協定 輸入參數只能0或1個。
public interface IDemoApi
{
Task<AccountInfo> Login(Login req);
Task Logout(TokenInfo req);
Task<string> GetEmail(TokenInfo req);
string GetServerInfo();
}
後台實作範例 Microsoft.AspNet.WebApi.Owin
- 參考套件 EasyApiProxy.WebApi 提供 DefaultApiResult 實作
/// <summary>
/// 範例API
/// </summary>
[DefaultApiResult] // 套用DefaultApi 通訊封裝
public partial class DemoApiController : ApiController, IDemoApi
{
[HttpPost]
public async Task<AccountInfo> Login(Login req)
{
await Task.Delay(1000);
if (req.Account == "david" && req.Password == "123")
{
return new AccountInfo { Account = req.Account, Token = "123456789", Expired = DateTime.Now.AddHours(1) };
}
throw new NotImplementedException();
}
[HttpPost]
public async Task Logout(TokenInfo req)
{
await Task.Delay(1000);
if (req.Token == "123456789")
return;
throw new ApplicationException("The Token Not exists");
}
[HttpPost]
public async Task<string> GetEmail(TokenInfo req)
{
await Task.Delay(1000);
if (req.Token == "123456789")
{
return "david@gmail.com";
}
throw new ApplicationException("The Token Not exists");
}
[HttpPost]
public string GetServerInfo()
{
return "Demo Server";
}
}
// Startup 啟動規劃WebApi
public class Startup
{
public void Configuration(IAppBuilder app) {
var apiConfig = new System.Web.Http.HttpConfiguration();
// 啟用WebApi
{
// use attibute routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// 必須套用 DefaultApi Json 設定
config.Formatters.JsonFormatter.SerializerSettings = DefaultApiExtension.DefaultJsonSerializerSettings;
//use webapi
app.UseWebApi(apiConfig);
}
}
}
用戶端啟用HAWK驗證
- 參考套件 EasyApiProxy.HawkAuth
// hawk 證書
var credential = new HawkNet.HawkCredential();
credential.Id = "API";
credential.Key = "XXXXXXXX";
credential.Algorithm = "sha256";
// proxy factory
var factory = new ApiProxyBuilder()
// 套用 DefaultApi 通訊協議
.UseDefaultApiProtocol("http://localhost:8081/api/Demo")
// 啟用Hawk驗證
.UseHawkAuthorize(credential)
.Build<IDemoApi>();
// 建立代理物件
var proxy = factory.Create();
var api = proxy.Api;
// 呼叫Api
var ret = await api.Login(new Login { Account = "david", Password = "123" });
後台API端啟用HAWK驗證 範例 Microsoft.AspNet.WebApi.Owin
- 引用套件 HawkNet.Owin
// Startup 啟動規劃WebApi 與 Hawk驗證
public class Startup
{
public void Configuration(IAppBuilder app) {
var apiConfig = new System.Web.Http.HttpConfiguration();
// 啟用Hawk驗證
{
var credential = new HawkCredential();
credential.Id = "API";
credential.Key = "XXXXXXXX";
credential.User = "API";
credential.Algorithm = "sha256";
app.UseHawkAuthentication(new HawkAuthenticationOptions
{
Credentials = (id) => Task.FromResult(credential),
IncludeServerAuthorization = false,
TimeskewInSeconds = 120
});
}
// 啟用WebApi
{
// use attibute routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// 必須套用 DefaultApi Json 設定
config.Formatters.JsonFormatter.SerializerSettings = DefaultApiExtension.DefaultJsonSerializerSettings;
//use webapi
app.UseWebApi(apiConfig);
}
}
}
/// <summary>
/// 範例API
/// </summary>
[DefaultApiResult] //套用DefaultApi 回傳格式
[Authorize(Users="API")] // 要求授權通過
public partial class DemoApiController : ApiController, IDemoApi {
...
}
用戶端啟用Basic驗證
- 內建支援
// basic 證書
var credential = new BasicCredential
{
Account = "API",
PassCode = "XXXXXXXX",
};
// proxy factory
var factory = new ApiProxyBuilder()
// 套用 DefaultApi 通訊協議
.UseDefaultApiProtocol("http://localhost:8081/api/Demo")
// 啟用Basic驗證
.UseBasicAuthorize(credential)
.Build<IDemoApi>();
// 建立代理物件
var proxy = factory.Create();
var api = proxy.Api;
// 呼叫Api
var ret = await api.Login(new Login { Account = "david", Password = "123" });
用戶端啟用驗證(直接指定Http Authroization Header)
- 內建支援
// proxy factory
var factory = new ApiProxyBuilder()
// 套用 DefaultApi 通訊協議
.UseDefaultApiProtocol("http://localhost:8081/api/Demo")
// 直接指定Http Authroization Header(Basic驗證)
.UseAuthorizationHeader("Basic YWRtaW46YWRtaW4xMjM0")
.Build<IDemoApi>();
後台API端啟用Basic驗證 範例 Microsoft.AspNet.WebApi.Owin
- 引用套件 Thinktecture.IdentityModel.Owin.BasicAuthentication
// Startup 啟動規劃WebApi 與 Hawk驗證
public class Startup
{
public void Configuration(IAppBuilder app) {
var apiConfig = new System.Web.Http.HttpConfiguration();
// 啟用Basic驗證
app.UseBasicAuthentication(new BasicAuthenticationOptions(
"DefaultApi",
async (user, secret) => await GetBasicUser(user, secret)));
// 啟用WebApi
{
// use attibute routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// 必須套用 DefaultApi Json 設定
config.Formatters.JsonFormatter.SerializerSettings = DefaultApiExtension.DefaultJsonSerializerSettings;
//use webapi
app.UseWebApi(apiConfig);
}
}
async Task<IEnumerable<Claim>> GetBasicUser(string account, string secret)
{
await Task.FromResult(0);
if (account == "API" && secret == "XXXXXXXX")
{
var c1 = new Claim(ClaimTypes.Name, account);
return new Claim[] { c1};
}
else
return null;
}
}
/// <summary>
/// 範例API
/// </summary>
[DefaultApiResult] //套用DefaultApi 回傳格式
[Authorize(Users="API")] // 要求授權通過
public partial class DemoApiController : ApiController, IDemoApi {
...
}
EasyInjector 整合
var injector = new EasyInjector();
// 建置整合到注入依賴
injector.AddApiProxy<IDemoApi>((sp, builder) =>
builder.UseDefaultApiProtocol("http://localhost:5249/api/Demo"));
using (var scope = injector.CreateScope()) {
// 取得Proxy
var proxy1 = scope.ServiceProvider.GetRequiredService<IApiProxy<IDemoApi>>();
// 取得Api = proxy1.Api
var api1 = scope.ServiceProvider.GetRequiredService<IDemoApi>();
}
服務遠端或本地 (依據選項)
var injector = new EasyInjector();
// 建置整合到注入依賴
injector.AddApiProxy<IDemoApi>((sp, builder) => {
var opt = sp.GetRequiredService<DemoOpt>();
// 選項沒有Url代表使用本地服務
if (opt.ApiUrl == null)
builder.UseLocalApi<IDemoApi>(sp => new DempApiLocal())
// 否則使用遠端服務
else
builder.UseDefaultApiProtocol(opt.ApiUrl);
});
| 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
- EasyInjector.DependencyInjection (>= 1.6.5)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on EasyApiProxy.Core:
| Package | Downloads |
|---|---|
|
EasyApiProxy.WebApi.Core
WebApi Server端 WebApi for DefaultApi Protocol (.net8) |
|
|
EasyApiProxy.HawkAuth.Core
WebApi 的快速代理類別 HAWK驗證(net8) |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 4.5.0 | 265 | 9/9/2025 |
| 4.4.0 | 234 | 8/28/2025 |
| 4.3.0 | 219 | 5/29/2025 |
| 4.2.1 | 219 | 5/19/2025 |
| 4.2.0 | 189 | 4/25/2025 |
| 4.1.0 | 203 | 2/7/2025 |
| 4.0.0 | 310 | 2/7/2025 |
| 3.3.1 | 181 | 1/16/2025 |
| 3.3.0 | 175 | 1/16/2025 |
| 3.2.6 | 158 | 1/15/2025 |
| 3.2.5 | 173 | 1/14/2025 |
| 3.2.4 | 177 | 1/14/2025 |
| 3.2.3 | 161 | 1/13/2025 |
| 3.2.2 | 157 | 1/13/2025 |
| 3.2.1 | 167 | 1/13/2025 |
| 3.2.0 | 171 | 1/10/2025 |
| 3.1.6 | 220 | 12/23/2024 |
| 3.1.5 | 152 | 12/17/2024 |
| 3.1.0 | 230 | 9/11/2024 |
| 3.0.0 | 216 | 8/27/2024 |
| 2.2.0 | 267 | 8/23/2024 |