public async Task HttpConnectionChangesAfterConfigChange_UseCollectionChangeToken() { // serviceCollection.AddSingleton<HttpJsonPlaceholderService, HttpJsonPlaceholderService>(); var config = new HttpClientCollectionOptions() { Defaults = new HttpClientOptions() { Connection = new HttpConnectionOptions() { Server = "defaults-before", Schema = "https", Port = 443, TimeoutMS = 50 } }, Clients = { ["service"] = new HttpClientOptions() { Connection = new HttpConnectionOptions() { Server = "service-before", Schema = "https", Port = 443, TimeoutMS = 50 } }, ["service2"] = new HttpClientOptions() { Connection = new HttpConnectionOptions() { Server = "service2-before", Schema = "http", Port = 443, TimeoutMS = 50 } } } }; Subject <object> changeToken = new Subject <object>(); var factory = HttpOptionsBuilder.Configure(builder => { builder.Configure(options => { Configure(options.Defaults, config.Defaults); foreach (var kvp in config.Clients) { options.AddClient(kvp.Key, o => Configure(o, kvp.Value)); } builder.UseChangeToken(source => changeToken.Subscribe((_) => source.InvokeChange())); void Configure(HttpClientOptions options, HttpClientOptions source) { options.Connection.Server = source.Connection.Server; options.Connection.Schema = source.Connection.Schema; options.Connection.Port = source.Connection.Port; options.Connection.Timeout = source.Connection.Timeout; } } ); }).Build(); var client = factory.CreateClient("service"); var client2 = factory.CreateClient("service2"); var defaults = factory.CreateClient("service3"); Assert.AreEqual(new Uri("https://service-before:443").ToString(), client.BaseAddress.ToString()); Assert.AreEqual(new Uri("http://service2-before:443").ToString(), client2.BaseAddress.ToString()); Assert.AreEqual(new Uri("https://defaults-before:443").ToString(), defaults.BaseAddress.ToString()); Assert.AreEqual(50, client.Timeout.TotalMilliseconds); config.Clients.Values.Concat(config.Defaults).ForEach(o => { o.Connection.Server = o.Connection.Server.Replace("before", "after"); o.Connection.Schema = o.Connection.Schema; o.Connection.Port = 7878; o.Connection.TimeoutMS = 500; ; }); changeToken.OnNext(Unit.Default); await Policy.HandleResult <string>(e => e.EndsWith("before")).WaitAndRetryAsync(new[] { TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(20), }).ExecuteAsync(() => Task.FromResult(factory.CreateClient("service").BaseAddress.Host)); Assert.AreEqual(new Uri("https://service-after:7878").ToString(), factory.CreateClient("service").BaseAddress.ToString()); Assert.AreEqual(new Uri("http://service2-after:7878").ToString(), factory.CreateClient("service2").BaseAddress.ToString()); Assert.AreEqual(new Uri("https://defaults-after:7878").ToString(), factory.CreateClient("service3").BaseAddress.ToString()); Assert.AreEqual(500, factory.CreateClient("service").Timeout.TotalMilliseconds); }
public async Task HttpConnectionChangesAfterConfigChange_UseFuncSource() { // serviceCollection.AddSingleton<HttpJsonPlaceholderService, HttpJsonPlaceholderService>(); var config = new HttpClientCollectionOptions() { Defaults = new HttpClientOptions() { Connection = new HttpConnectionOptions() { Server = "defaults-before", Schema = "https", Port = 443, TimeoutMS = 50 } }, Clients = { ["service"] = new HttpClientOptions() { Connection = new HttpConnectionOptions() { Server = "service-before", Schema = "https", Port = 443, TimeoutMS = 50 } }, ["service2"] = new HttpClientOptions() { Connection = new HttpConnectionOptions() { Server = "service2-before", Schema = "http", Port = 443, TimeoutMS = 50 } } } }; var httpClientCollection = HttpOptionsBuilder.Configure(builder => { builder.ConfigureOptionsBuilder(options => { builder.Services.AddHttpClientOptions((name, clientOptions) => { configure(clientOptions, config.Defaults); if (config.Clients.TryGetValue(name, out var clientConfig)) { configure(clientOptions, clientConfig); } }); void configure(HttpClientOptions options, HttpClientOptions source) { options.Connection.Server = source.Connection.Server; options.Connection.Schema = source.Connection.Schema; options.Connection.Port = source.Connection.Port; options.Connection.Timeout = source.Connection.Timeout; } } ); }).Build(); var client = httpClientCollection.CreateClient("service"); var client2 = httpClientCollection.CreateClient("service2"); var defaults = httpClientCollection.CreateClient("service3"); Assert.AreEqual(new Uri("https://service-before:443").ToString(), client.BaseAddress.ToString()); Assert.AreEqual(new Uri("http://service2-before:443").ToString(), client2.BaseAddress.ToString()); Assert.AreEqual(new Uri("https://defaults-before:443").ToString(), defaults.BaseAddress.ToString()); Assert.AreEqual(50, client.Timeout.TotalMilliseconds); config.Clients.Values.Concat(config.Defaults).ForEach(o => { o.Connection.Server = o.Connection.Server.Replace("before", "after"); o.Connection.Schema = o.Connection.Schema; o.Connection.Port = 7878; o.Connection.TimeoutMS = 500; ; }); httpClientCollection.InvokeChange(); await Policy.HandleResult <string>(e => e.EndsWith("before")).WaitAndRetryAsync(new[] { TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(20), TimeSpan.FromMilliseconds(20), }).ExecuteAsync(() => Task.FromResult(httpClientCollection.CreateClient("service").BaseAddress.Host)); Assert.AreEqual(new Uri("https://service-after:7878").ToString(), httpClientCollection.CreateClient("service").BaseAddress.ToString()); Assert.AreEqual(new Uri("http://service2-after:7878").ToString(), httpClientCollection.CreateClient("service2").BaseAddress.ToString()); Assert.AreEqual(new Uri("https://defaults-after:7878").ToString(), httpClientCollection.CreateClient("service3").BaseAddress.ToString()); Assert.AreEqual(500, httpClientCollection.CreateClient("service").Timeout.TotalMilliseconds); }