背景
.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