背景
.NET 中 有沒有類似 Java 中 Feign 這樣的框架?經過查詢和實驗,發現 在 .NET 平臺上,雖然沒有直接的 Feign 框架的埠,但是有一些類似的框架和庫,它們提供了類似的功能和設計理念。下面是一些在 .NET 中用於宣告式 HTTP 客戶端的框架和庫:
- Refit:
Refit 是一個用於構建宣告式、型別安全的 HTTP 客戶端的庫。它允許您透過定義介面來描述 HTTP API,並生成客戶端程式碼。Refit 使用屬性路由的方式定義 API 呼叫,類似於 Feign。它支援各種 HTTP 方法,如 GET、POST、PUT、DELETE 等,並支援非同步操作。
https://github.com/reactiveui/refit - RestEase:
RestEase 也是一個用於建立型別安全的 HTTP 客戶端的庫。它提供了類似於 Refit 的宣告式 API 定義方式,允許您透過編寫介面來描述 HTTP API。RestEase 支援各種 HTTP 方法,並提供了簡單易用的 fluent API。
https://github.com/canton7/RestEase - Feign.net
feign.net 是一個基於 .NET Standard 2.0 的庫,它實現了與 Feign 類似的介面定義和呼叫方式。feign.net 支援非同步操作,並提供了與 Refit 和 RestEase 類似的特性。
https://github.com/daixinkai/feign.net
整合 Refit
要在 ASP.NET Core 中整合 Refit,首先需要安裝 Refit 包。可以透過 NuGet 包管理器或者 .NET CLI 來完成:
dotnet add package Refit
接下來,您可以建立一個介面,用於定義對遠端 API 的呼叫。例如:
using Microsoft.AspNetCore.Mvc;
using Refit;
using RefitDemo.Models;
namespace RefitDemo.WebApi
{
public interface IWeatherForecastApi
{
[Get("/WeatherForecast/Get")]
Task<string> GetWeatherForecast(string id);
[Post("/WeatherForecast/Post")]
Task<WeatherForecast> PostWeatherForecast(WeatherForecast weatherForecast);
}
}
然後,您可以在 ASP.NET Core 應用程式中使用 Refit 客戶端。一種常見的方法是將其注入到服務中,以便在需要時進行使用。例如,在 Startup.cs 中配置:
builder.Services.AddRefitClient<IWeatherForecastApi>(new RefitSettings
{
ContentSerializer = new NewtonsoftJsonContentSerializer(
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
}
)
}).ConfigureHttpClient(c => c.BaseAddress = new Uri("http://localhost:5237"));
//封裝
builder.Services.AddRefitClients("RefitDemo.WebApi", "http://localhost:5237");
public static class RefitExtensions
{
public static void AddRefitClients(this IServiceCollection services, string targetNamespace, string baseAddress, RefitSettings? refitSettings = null)
{
// 獲取指定名稱空間中的所有型別
var types = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.Namespace == targetNamespace && t.IsInterface).ToList();
foreach (var type in types)
{
services.AddRefitClient(type, refitSettings).ConfigureHttpClient(c => c.BaseAddress = new Uri(baseAddress));
}
}
}
最後,您可以在需要使用 API 客戶端的地方注入 IWeatherForecastApi 介面,並使用它來呼叫遠端 API:
using Microsoft.AspNetCore.Mvc;
using RefitDemo.WebApi;
namespace RefitDemo.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly ILogger<WeatherForecastController> _logger;
private readonly IWeatherForecastApi _weatherForecastApi;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IWeatherForecastApi weatherForecastApi)
{
_logger = logger;
_weatherForecastApi = weatherForecastApi;
}
[HttpGet("GetWeatherForecast")]
public async Task<string> GetWeatherForecast()
{
return await _weatherForecastApi.GetWeatherForecast("1111");
}
[HttpGet("PostWeatherForecast")]
public async Task<WeatherForecast> PostWeatherForecast()
{
return await _weatherForecastApi.PostWeatherForecast(new WeatherForecast { Date = DateOnly.MaxValue,Summary = "1111" });
}
[HttpGet("Get")]
public string Get(string id)
{
return id;
}
[HttpPost("Post")]
public WeatherForecast Post(WeatherForecast weatherForecast)
{
return weatherForecast;
}
}
}
其它功能:https://github.com/reactiveui/refit?tab=readme-ov-file#table-of-contents
整合 RestEase
要在 ASP.NET Core 中整合 RestEase,首先需要安裝 RestEase 包。可以透過 NuGet 包管理器或者 .NET CLI 來完成:
dotnet add package RestEase
接下來,您可以建立一個介面,用於定義對遠端 API 的呼叫。例如:
using Microsoft.AspNetCore.Mvc;
using RestEase;
using RestEaseDemo.Models;
namespace RestEaseDemo.WebApi
{
public interface IWeatherForecastApi
{
[Get("/WeatherForecast/Get")]
Task<string> GetWeatherForecast(string id);
[Post("/WeatherForecast/Post")]
Task<WeatherForecast> PostWeatherForecast(WeatherForecast weatherForecast);
}
}
然後,您可以在 ASP.NET Core 應用程式中使用 RestEase 客戶端。一種常見的方法是將其注入到服務中,以便在需要時進行使用。例如,在 Startup.cs 中配置:
builder.Services.AddRestEaseClient<IWeatherForecastApi>("http://localhost:5252");
然後,您可以在 ASP.NET Core 應用程式中使用 RestEase 客戶端。與 Refit 不同的是,RestEase 不需要額外的配置,您只需要直接使用介面即可。在需要使用 API 客戶端的地方注入 IMyApi 介面,並使用它來呼叫遠端 API:
using Microsoft.AspNetCore.Mvc;
using RestEaseDemo.WebApi;
namespace RestEaseDemo.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly ILogger<WeatherForecastController> _logger;
private readonly IWeatherForecastApi _weatherForecastApi;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IWeatherForecastApi weatherForecastApi)
{
_logger = logger;
_weatherForecastApi = weatherForecastApi;
}
[HttpGet("GetWeatherForecast")]
public async Task<string> GetWeatherForecast()
{
return await _weatherForecastApi.GetWeatherForecast("1111");
}
[HttpGet("PostWeatherForecast")]
public async Task<WeatherForecast> PostWeatherForecast()
{
return await _weatherForecastApi.PostWeatherForecast(new WeatherForecast { Date = DateOnly.MaxValue,Summary = "1111" });
}
[HttpGet("Get")]
public string Get(string id)
{
return id;
}
[HttpPost("Post")]
public WeatherForecast Post(WeatherForecast weatherForecast)
{
return weatherForecast;
}
}
}
其它功能:https://github.com/canton7/RestEase?tab=readme-ov-file#table-of-contents