| | 1 | | using Application.Abstractions; |
| | 2 | | using Domain.Currencies; |
| | 3 | | using Infrastructure.Caching; |
| | 4 | | using Infrastructure.ExchangeProviders; |
| | 5 | | using Infrastructure.ExchangeProviders.Frankfurter; |
| | 6 | | using Infrastructure.Extensions; |
| | 7 | | using Microsoft.Extensions.Configuration; |
| | 8 | | using Microsoft.Extensions.DependencyInjection; |
| | 9 | | using Microsoft.Extensions.Http.Resilience; |
| | 10 | | using Polly; |
| | 11 | | using TimeProvider = Infrastructure.Utilities.TimeProvider; |
| | 12 | |
|
| | 13 | |
|
| | 14 | | namespace Infrastructure |
| | 15 | | { |
| | 16 | | public static class DependencyInjection |
| | 17 | | { |
| | 18 | | public static IServiceCollection InjectInfrastructure( |
| | 19 | | this IServiceCollection services, |
| | 20 | | IConfiguration configuration) |
| 4 | 21 | | { |
| 4 | 22 | | services.AddCaching(configuration); |
| 4 | 23 | | services.AddCurrencyProviders(configuration); |
| 4 | 24 | | services.AddUtilities(); |
| 4 | 25 | | services.AddAuthentication(configuration); |
| 4 | 26 | | return services; |
| 4 | 27 | | } |
| | 28 | |
|
| | 29 | | private static void AddCaching(this IServiceCollection services, IConfiguration configuration) |
| 4 | 30 | | { |
| 5 | 31 | | services.AddStackExchangeRedisCache(options => options.Configuration = configuration.GetConnectionString("Ca |
| 4 | 32 | | services.AddSingleton<ICacheService, CacheService>(); |
| 4 | 33 | | } |
| | 34 | |
|
| | 35 | | public static IServiceCollection AddCurrencyProviders(this IServiceCollection services, IConfiguration configura |
| 4 | 36 | | { |
| 4 | 37 | | var baseUrl = configuration["ProviderUrls:FrankfurterBaseUrl"]; |
| 4 | 38 | | services.AddHttpClient<IExchangeProvider, FrankfurterExchangeProvider>(client => |
| 1 | 39 | | { |
| 1 | 40 | | client.BaseAddress = new Uri(baseUrl); |
| 1 | 41 | | }) |
| 4 | 42 | | .AddStandardResilienceHandler().Configure(x => |
| 1 | 43 | | { |
| 1 | 44 | | x.Retry.MaxRetryAttempts = 3; |
| 1 | 45 | | x.Retry.Delay = TimeSpan.FromSeconds(1); |
| 1 | 46 | | x.Retry.UseJitter = true; |
| 1 | 47 | | x.Retry.BackoffType = DelayBackoffType.Exponential; |
| 1 | 48 | | x.CircuitBreaker.MinimumThroughput = 10; |
| 1 | 49 | | x.TotalRequestTimeout.Timeout = TimeSpan.FromSeconds(10); |
| 5 | 50 | | }); |
| 4 | 51 | | services.AddTransient<IExchangeProviderFactory, ExchangeProviderFactory>(); |
| 4 | 52 | | return services; |
| 4 | 53 | | } |
| | 54 | | public static IServiceCollection AddUtilities(this IServiceCollection services) |
| 4 | 55 | | { |
| 4 | 56 | | services.AddSingleton<ITimeProvider, TimeProvider>(); |
| 4 | 57 | | return services; |
| 4 | 58 | | } |
| | 59 | | } |
| | 60 | | } |