6.15HttpRemoteBuilder 构建器

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

HttpRemoteBuilder 是一个构建器,用于配置和构建 IHttpRemoteService 服务所需的所有设置。在应用启动时,通常通过调用 services.AddHttpRemote 方法来指定这些配置。

以下展示了 HttpRemoteBuilder 提供的所有配置功能:

cs
services.AddHttpRemote(builder =>{    // 添加自定义内容处理器    builder.AddHttpContentProcessors(() => [ new CustomStringContentProcessor() ]);    // 添加自定义内容转换器    builder.AddHttpContentConverters(() => [ new SpanCharContentConverter() ]);    // 添加自定义泛型内容转换器    builder.AddGenericHttpContentConverters(() => [ new(typeof(IAsyncEnumerable<>), typeArgs => (IHttpContentConverter)Activator.CreateInstance(typeof(AsyncEnumerableContentConverter<>).MakeGenericType(typeArgs[0]))!) ]);    // 设置自定义对象内容转换器工厂    builder.UseObjectContentConverterFactory<CustomObjectContentConverterFactory>();    builder.UseObjectContentConverterFactory(typeof(CustomObjectContentConverterFactory));    // 添加 HTTP 声明式服务    builder.AddHttpDeclarative<IHttpService>();    builder.AddHttpDeclarative(typeof(IHttpService));   // 通过 requireIHttpDeclarative 参数,支持注册无需实现 IHttpDeclarative 接口的声明式代理    // 批量添加 HTTP 声明式服务    builder.AddHttpDeclaratives([typeof(IHttpService), typeof(IHttpService2), ...]);    // 扫描程序集批量添加 HTTP 声明式服务    builder.AddHttpDeclarativesFromAssemblies([ assembly1, assembly2, ... ]);  // 若使用 Furion 框架可直接设置 App.Assemblies    // 添加自定义 HTTP 声明式提取器    builder.AddHttpDeclarativeExtractors(() => [ new AcceptDeclarativeExtractor() ]);    // 扫描程序集批量添加 HTTP 声明式提取器    builder.AddHttpDeclarativeExtractorsFromAssemblies([ assembly1, assembly2, ... ]); // 若使用 Furion 框架可直接设置 App.Assemblies    // 添加 HTTP 请求管道处理器服务    builder.AddPipelineHandler<CustomHttpRequestPipelineHandler>();    builder.AddPipelineHandler(typeof(CustomHttpRequestPipelineHandler));    // 设置自定义日志服务,可通过继承 HttpRemoteLoggerBase 实现    builder.UseLogger<CustomHttpRemoteLogger>();    builder.UseLogger(typeof(CustomHttpRemoteLogger));});