private static void UseMultipleClients(this IServiceCollection services, IConfiguration configuration, IDictionary <string, TokenFlow> clients) { services.UseHttpApiDefaults(); TokenFlowMapper tokenFlowMapper = new TokenFlowMapper(); foreach (KeyValuePair <string, TokenFlow> client in clients) { string clientName = client.Key; TokenFlow tokenFlow = client.Value; var clientConfigurationToRemove = services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IClientConfiguration)); services.Remove(clientConfigurationToRemove); var tokenFlowRegisterToRemove = services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(ITokenFlowRegister)); services.Remove(tokenFlowRegisterToRemove); services.SetupClient(configuration, clientName, tokenFlow); services.AddClient(clientName, tokenFlowMapper); } services.AddSingleton <ITokenFlowMapper>(tokenFlowMapper); }
private static void AddClient(this IServiceCollection services, string clientName, TokenFlowMapper tokenFlowMapper) { ServiceProvider serviceProvider = services.BuildServiceProvider(); tokenFlowMapper.Clients.Add(clientName, serviceProvider.GetService <ITokenFlowRegister>()); IClient client = new Client(serviceProvider.GetService <IHttpClientFactory>(), serviceProvider.GetService <IHttpApiCommandFactory>(), serviceProvider.GetService <ISerializerService>()) { Name = clientName }; services.AddSingleton(client); }