public static IHttpClientBuilder UseHttpClientFactory(this ICoreBuilder clientBuilder) { CustomHttpClientFactoryBuilder builder = new CustomHttpClientFactoryBuilder(clientBuilder.Services, "SimpleS3"); //Contrary to the naming, this does not add a HttpClient to the services. It is the factories etc. necessary for HttpClientFactory to work. //It also bind the HttpClient to HttpClientFactoryNetworkDriver instead of adding it directly to the service collection. //Note that it adds INetworkDriver as a transient service in order to change out the HttpClient. builder.Services.AddHttpClient <INetworkDriver, HttpClientFactoryNetworkDriver>(builder.Name); builder.Services.Configure <HttpClientFactoryOptions>(builder.Name, (options, x) => { IOptions <HttpClientFactoryConfig> factoryConfig = x.GetService <IOptions <HttpClientFactoryConfig> >(); options.HandlerLifetime = factoryConfig.Value.HandlerLifetime; options.HttpClientActions.Add(client => { client.DefaultRequestHeaders.UserAgent.TryParseAdd(Constants.DefaultUserAgent); client.DefaultRequestHeaders.TransferEncodingChunked = false; }); options.HttpMessageHandlerBuilderActions.Add(b => { HttpClientHandler handler = new HttpClientHandler(); handler.UseCookies = false; handler.MaxAutomaticRedirections = 3; handler.SslProtocols = SslProtocols.None; //Let the OS handle the protocol to use handler.UseProxy = factoryConfig.Value.UseProxy; handler.Proxy = factoryConfig.Value.Proxy; b.PrimaryHandler = handler; }); }); return(builder); }
public static IHttpClientBuilder UseHttpClientFactory(this ICoreBuilder clientBuilder) { CustomHttpClientFactoryBuilder builder = new CustomHttpClientFactoryBuilder(clientBuilder.Services); //Contrary to the naming, this does not add a HttpClient to the services. It is the factories etc. necessary for HttpClientFactory to work. builder.Services.AddHttpClient(); builder.Services.AddSingleton <INetworkDriver, HttpClientFactoryNetworkDriver>(); builder.Services.Configure <HttpClientFactoryOptions>((options, services) => { IOptions <HttpClientFactoryConfig> factoryConfig = services.GetRequiredService <IOptions <HttpClientFactoryConfig> >(); options.HandlerLifetime = factoryConfig.Value.HandlerLifetime; options.HttpClientActions.Add(client => { client.DefaultRequestHeaders.UserAgent.TryParseAdd(Constants.DefaultUserAgent); client.DefaultRequestHeaders.TransferEncodingChunked = false; }); options.HttpMessageHandlerBuilderActions.Add(b => { HttpClientHandler handler = new HttpClientHandler(); handler.UseCookies = false; handler.MaxAutomaticRedirections = 3; handler.SslProtocols = SslProtocols.None; //Let the OS handle the protocol to use handler.UseProxy = factoryConfig.Value.UseProxy; handler.Proxy = factoryConfig.Value.Proxy; b.PrimaryHandler = handler; }); }); return(builder); }
public static IHttpClientBuilder UseHttpClientFactory(this ICoreBuilder clientBuilder, Action <HttpClientFactoryConfig> config = null) { CustomHttpClientFactoryBuilder builder = new CustomHttpClientFactoryBuilder(clientBuilder.Services); builder.Services.AddHttpClient(); //Contrary to the naming, this does not add a HttpClient to the services. It is the factories etc. necessary for HttpClientFactory to work. builder.Services.AddSingleton <IConfigureOptions <HttpClientFactoryOptions> >(x => { return(new ConfigureNamedOptions <HttpClientFactoryOptions>(builder.Name, options => { IOptions <HttpClientFactoryConfig> o = x.GetService <IOptions <HttpClientFactoryConfig> >(); options.HandlerLifetime = o.Value.HandlerLifetime; options.HttpClientActions.Add(client => { client.DefaultRequestHeaders.UserAgent.TryParseAdd(Constants.DefaultUserAgent); client.DefaultRequestHeaders.TransferEncodingChunked = false; }); })); }); if (config != null) { builder.Services.Configure(config); } builder.Services.AddSingleton(x => { IHttpClientFactory httpClientFactory = x.GetRequiredService <IHttpClientFactory>(); return(httpClientFactory.CreateClient("SimpleS3")); }); builder.Services.AddSingleton <INetworkDriver, HttpClientFactoryNetworkDriver>(); builder.Services.AddSingleton(x => { IOptions <HttpClientFactoryConfig> options = x.GetService <IOptions <HttpClientFactoryConfig> >(); HttpClientHandler handler = new HttpClientHandler(); handler.UseCookies = false; handler.MaxAutomaticRedirections = 3; handler.SslProtocols = SslProtocols.None; if (options != null) { handler.UseProxy = options.Value.UseProxy; handler.Proxy = options.Value.Proxy; } return(handler); }); return(builder); }