/// <summary>ctor</summary> /// <param name="services">the services</param> /// <param name="scopeFactory">the scope factory</param> /// <param name="loggerFactory">the logger factory</param> /// <param name="configuration">the configuration</param> public HttpClientRecycler( IServiceProvider services, IServiceScopeFactory scopeFactory, ILoggerFactory loggerFactory, HttpClientConfiguration configuration ) { this._Services = services; this._ScopeFactory = scopeFactory; this._LoggerFactory = loggerFactory; this._Configuration = configuration; }
/// <summary> /// Add a configuration /// </summary> /// <param name="name">the name of the configuration</param> /// <param name="configure">action to configure</param> public void AddConfiguration(string name, Action <HttpClientConfiguration> configure) { var configuration = new HttpClientConfiguration() { Name = name }; if (configure != null) { configure(configuration); } this.Configurations.Add(configuration); }
/// <summary>Creates a HttpClient based on the configuration.</summary> /// <param name="configuration"></param> /// <returns>A HttpClient based on the configuration</returns> /// <remarks>The HttpClient will be created for each call of this - the underlying HttpClientHandler can be reused/shared.</remarks> public virtual HttpClient CreateHttpClient(HttpClientConfiguration configuration) { while (true) { if (this._Recyclers.TryGetValue(configuration, out var recycler)) { if (recycler.IsValid()) { return(recycler.CreateHttpClient()); } } { recycler = new HttpClientRecycler(this.Services, this.ScopeFactory, this.LoggerFactory, configuration); if (!this._Recyclers.TryAdd(configuration, recycler)) { continue; } else { return(recycler.CreateHttpClient()); } } } }
/// <inheritdoc/> public void SetConfiguration(HttpClientConfiguration configuration) { this.Configuration = configuration; }