private static IHttpClientBuilder SetupClient(this Container services, IServiceCollection collection, string clientName, IClientConfiguration clientConfiguration, TokenFlow tokenFlow) { var httpClientBuilder = collection.AddHttpClient(clientName) .ConfigureHttpClient(client => client.BaseAddress = new Uri(clientConfiguration.ApiBaseAddress + clientConfiguration.ProjectKey + "/")) .AddHttpMessageHandler(c => { var providers = services.GetAllInstances <ITokenProvider>(); var provider = providers.FirstOrDefault(tokenProvider => tokenProvider.TokenFlow == tokenFlow); provider.ClientConfiguration = clientConfiguration; return(new AuthorizationHandler(provider)); }) .AddHttpMessageHandler(c => new CorrelationIdHandler(new DefaultCorrelationIdProvider(clientConfiguration))) .AddHttpMessageHandler(c => new ErrorHandler(new ApiExceptionFactory(clientConfiguration, services.GetService <ISerializerService>()))) .AddHttpMessageHandler(c => new LoggerHandler(services.GetService <ILoggerFactory>())); return(httpClientBuilder); }
private static void UseTokenProviders(this Container services, IHttpClientFactory factory) { var providers = new List <Registration> { Lifestyle.Transient.CreateRegistration( () => new AnonymousSessionTokenProvider(factory, services.GetService <IAnonymousCredentialsStoreManager>(), services.GetService <ITokenSerializerService>()), services), Lifestyle.Transient.CreateRegistration( () => new ClientCredentialsTokenProvider(factory, services.GetService <ITokenStoreManager>(), services.GetService <ITokenSerializerService>()), services), Lifestyle.Transient.CreateRegistration( () => new PasswordTokenProvider(factory, services.GetService <IUserCredentialsStoreManager>(), services.GetService <ITokenSerializerService>()), services) }; services.RegisterCollection <ITokenProvider>(providers); }
private static IDictionary <string, IHttpClientBuilder> UseSingleClient(this Container services, IConfiguration configuration, string clientName, TokenFlow tokenFlow) { var collection = new ServiceCollection(); services.UseHttpApiDefaults(collection); services.Register <IClient>(() => new CtpClient(services.GetService <IHttpClientFactory>(), services.GetService <IHttpApiCommandFactory>(), services.GetService <ISerializerService>(), services.GetService <IUserAgentProvider>()) { Name = clientName }); var configurationSection = configuration.GetSection(clientName); IClientConfiguration clientConfiguration = configurationSection.Get <ClientConfiguration>(); Validator.ValidateObject(clientConfiguration, new ValidationContext(clientConfiguration), true); var builders = new Dictionary <string, IHttpClientBuilder> { { clientName, services.SetupClient(collection, clientName, clientConfiguration, tokenFlow) } }; services.Register <IHttpClientFactory>(() => collection.BuildServiceProvider().GetService <IHttpClientFactory>(), Lifestyle.Singleton); return(builders); }
private static IDictionary <string, IHttpClientBuilder> UseMultipleClients(this Container services, IConfiguration configuration, IDictionary <string, TokenFlow> clients) { var collection = new ServiceCollection(); services.UseHttpApiDefaults(collection); var builders = new Dictionary <string, IHttpClientBuilder>(); var clientBuilders = new List <Registration>(); foreach (KeyValuePair <string, TokenFlow> client in clients) { string clientName = client.Key; TokenFlow tokenFlow = client.Value; IClientConfiguration clientConfiguration = configuration.GetSection(clientName).Get <ClientConfiguration>(); Validator.ValidateObject(clientConfiguration, new ValidationContext(clientConfiguration), true); builders.Add(clientName, services.SetupClient(collection, clientName, clientConfiguration, tokenFlow)); clientBuilders.Add(Lifestyle.Singleton.CreateRegistration(() => new CtpClient(services.GetService <IHttpClientFactory>(), services.GetService <IHttpApiCommandFactory>(), services.GetService <ISerializerService>(), services.GetService <IUserAgentProvider>()) { Name = clientName }, services)); } services.RegisterCollection <IClient>(clientBuilders); services.Register <IHttpClientFactory>(() => collection.BuildServiceProvider().GetService <IHttpClientFactory>(), Lifestyle.Singleton); return(builders); }
public static IServiceProvider UseSimpleInjector(this IServiceCollection services) { var container = new Container(); var scopeFactory = new SimpleInjectorServiceScopeFactory(container); container.Options.AllowOverridingRegistrations = true; container.Options.ResolveUnregisteredCollections = true; container.Options.SuppressLifestyleMismatchVerification = true; container.Options.DefaultScopedLifestyle = scopeFactory; foreach (var descriptor in services) { //if (descriptor.ServiceType.FullName.IndexOf("IConfigureOptions") >= 0) //{ // System.Diagnostics.Debugger.Break(); //} var lifetime = ConvertLifetimeToSimpleInjectorLifetime(descriptor.Lifetime); if (descriptor.ImplementationType != null) { var serviceTypeInfo = descriptor.ServiceType.GetTypeInfo(); if (serviceTypeInfo.IsGenericTypeDefinition) { if (descriptor.ImplementationType.GetTypeInfo().IsGenericTypeDefinition) { // How do you pass the lifetime here? container.Register(descriptor.ServiceType, descriptor.ImplementationType, lifetime); } else { container.Register(descriptor.ServiceType, new[] { descriptor.ImplementationType }, lifetime); } } else { container.Register(descriptor.ServiceType, descriptor.ImplementationType, lifetime); // for collection based container.Register(descriptor.ImplementationType, descriptor.ImplementationType, lifetime); } } else if (descriptor.ImplementationFactory != null) { container.Register( descriptor.ServiceType, () => descriptor.ImplementationFactory(container.GetService<IServiceProvider>()), lifetime); } else { container.Register( descriptor.ServiceType, () => descriptor.ImplementationInstance, lifetime); } } var groupedTypes = services .GroupBy(s => s.ServiceType); foreach (var groupedType in groupedTypes) { var serviceType = groupedType.Key; var type = typeof(IEnumerable<>).MakeGenericType(serviceType); if (serviceType.GetTypeInfo().IsGenericTypeDefinition) { container.RegisterCollection(serviceType, groupedType.Select(t => t.ImplementationType)); } else { // TODO: Get longest lifestyle? var lifetime = ConvertLifetimeToSimpleInjectorLifetime(groupedType.First().Lifetime); container.Register( type, () => { var collectionServices = groupedType .Where(c => c.ImplementationType != null) .Select(c => container.GetRequiredService(c.ImplementationType)); return typeof(System.Linq.Enumerable) .GetMethod("Cast", new[] { typeof(System.Collections.IEnumerable) }) .MakeGenericMethod(groupedType.Key) .Invoke(null, new object[] { collectionServices }); }, lifetime); } } container.Register<IServiceProvider>(() => container, Lifestyle.Singleton); container.Register<IServiceScopeFactory>(() => scopeFactory, Lifestyle.Singleton); // container.Verify(); return container; }