public static IHttpClientBuilder AddHttpApi <THttpApi>(this IServiceCollection services, Action <HttpApiOptions <THttpApi>, IServiceProvider> configureOptions) where THttpApi : class, IHttpApi { services .AddOptions <HttpApiOptions <THttpApi> >() .Configure(configureOptions); return(services .AddHttpClient(typeof(THttpApi).FullName) .AddTypedClient((httpClient, serviceProvider) => { var httpApiConfig = new HttpApiConfig(httpClient) { ServiceProvider = serviceProvider }; var httpApiOptions = serviceProvider.GetRequiredService <IOptions <HttpApiOptions <THttpApi> > >().Value; MergeOptions(httpApiConfig, httpApiOptions); return HttpApi.Create <THttpApi>(httpApiConfig); }) .ConfigurePrimaryHttpMessageHandler(() => new DefaultHttpClientHandler())); }
/// <summary> /// 创建TInterface接口的代理实例 /// </summary> /// <param name="httpApiConfig">httpApi配置</param> /// <returns></returns> protected virtual HttpApi CreateHttpApi(HttpApiConfig httpApiConfig) { return(HttpApi.Create(this.InterfaceType, httpApiConfig)); }