5.2接口定义与使用

创建于 2026 年 8 月 17 日约 3 分钟读完

在利用 HTTP 声明式请求之前,您需要定义一个接口,并确保它实现 IHttpDeclarative 接口:

cs
public interface IHttpService : IHttpDeclarative{}

随后,在 Startup.csProgram.cs 文件中,配置并注册 HttpRemote 服务,以启用 HTTP 声明式请求功能:

cs
services.AddHttpRemote(builder =>{    // 使用泛型方式注册 IHttpService 声明式接口    builder.AddHttpDeclarative<IHttpService>();    // 或者使用类型方式    // builder.AddHttpDeclarative(typeof(IHttpService));    // 若需注册多个接口,可使用以下方法(非示例中的数组语法)    // builder.AddHttpDeclaratives(new[] { typeof(IHttpService), typeof(IHttpService) });    // 推荐:从程序集中扫描并批量注册    // builder.AddHttpDeclarativesFromAssemblies([Assembly.GetEntryAssembly()]);    // 若使用 Furion 框架,可直接传入 App.Assemblies    // builder.AddHttpDeclarativesFromAssemblies(App.Assemblies);});

在服务中使用 IHttpService 声明式请求时,可通过构造函数注入:

cs
public class YourService{    private readonly IHttpService _httpService;    public YourService(IHttpService httpService)    {        _httpService = httpService;    }}

若您使用的是 .NET 8 及以上版本时,可利用主构造函数注入进一步简化代码:

cs
public class YourService(IHttpService httpService){    // 使用 httpService 变量}

某些场景下,您也可以仅在特定方法中注入,通过在参数前添加 [FromServices] 特性实现:

cs
public class YourService{    public Task<string> GetResource([FromServices] IHttpService httpService)    {        // 您的业务逻辑    }}

此外,如果您希望动态解析声明式服务,可以先注入 IHttpRemoteService,再调用其 For<T>() 方法获取实例:

cs
public class YourService(IHttpRemoteService httpRemoteService){    public async Task InvokeAsync()    {        var httpService = httpRemoteService.For<IHttpService>();    }}

开放泛型接口

HTTP 声明式接口同样支持开放泛型定义,例如:

cs
public interface IHttpService<T> : IHttpDeclarative{}

需要注意的是,使用程序集扫描方式(如 builder.AddHttpDeclarativesFromAssemblies(assemblies))时会默认跳过开放泛型接口,因为它要求提供运行时的具体类型(即封闭泛型类型)。此时,应显式注册封闭泛型版本:

cs
services.AddHttpRemote(builder =>{    // 注册封闭泛型类型,如 IHttpService<string>    builder.AddHttpDeclarative<IHttpService<string>>();    // 或者使用类型方式    // builder.AddHttpDeclarative(typeof(IHttpService<string>));});

在业务中使用时,可直接通过依赖注入获取指定封闭类型(如 IHttpService<string>),或调用 IHttpRemoteService.For<IHttpService<string>>() 动态解析服务实例。

无需实现 IHttpDeclarative 接口

某些情况下,您可能希望直接为普通接口生成声明式代理,而不强制要求该接口实现 IHttpDeclarative。例如,定义一个普通的 IMyApi 接口:

cs
public interface IMyApi{    [Get("https://api.furion.net/users/{id}")]    Task<User> GetUserAsync(int id);}

此时在注册时指定 requireIHttpDeclarative: false

cs
services.AddHttpRemote(builder =>{    builder.AddHttpDeclarative(typeof(IMyApi), requireIHttpDeclarative: false);});

注册后,该接口的使用方式与普通声明式接口完全一致,可通过构造函数注入、[FromServices] 特性注入或 IHttpRemoteService.For<T>() 动态解析。