コード例 #1
0
        public static void UseWebClients(this IServiceCollection services, IConfiguration configuration)
        {
            var exchangeRateOptions = new ExchangeRateApiOptions();

            configuration.GetSection(nameof(ExchangeRateApiOptions)).Bind(exchangeRateOptions, c => c.BindNonPublicProperties = true);
            services.AddSingleton(exchangeRateOptions);

            services.AddHttpClient <IExchangeRateApiClient, ExchangeRateApiClient>(c =>
            {
                c.BaseAddress = new Uri(exchangeRateOptions.BaseAddress);
                c.DefaultRequestHeaders.Add("Accept", "application/json");
            })
            .AddPolicyHandler(GetRetryPolicy(exchangeRateOptions.RetryCount, exchangeRateOptions.SleepDurationInSeconds))
            .AddPolicyHandler(GetCircuitBreakerPolicy(exchangeRateOptions.HandledEventsAllowedBeforeBreaking, exchangeRateOptions.DurationOfBreakInSeconds));

            var currconvOptions = new CurrconvApiOptions();

            configuration.GetSection(nameof(CurrconvApiOptions)).Bind(currconvOptions, c => c.BindNonPublicProperties = true);
            services.AddSingleton(currconvOptions);

            services.AddHttpClient <ICurrconvApiClient, CurrconvApiClient>(c =>
            {
                c.BaseAddress = new Uri(currconvOptions.BaseAddress);
                c.DefaultRequestHeaders.Add("Accept", "application/json");
            })
            .AddPolicyHandler(GetRetryPolicy(currconvOptions.RetryCount, currconvOptions.SleepDurationInSeconds))
            .AddPolicyHandler(GetCircuitBreakerPolicy(currconvOptions.HandledEventsAllowedBeforeBreaking, currconvOptions.DurationOfBreakInSeconds));

            services.AddHealthChecks()
            .AddCheck <CurrconvApiHealthCheck>(nameof(CurrconvApiHealthCheck), Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus.Unhealthy);
        }
コード例 #2
0
 public CurrconvApiClient(HttpClient httpClient, CurrconvApiOptions options)
 {
     _httpClient = httpClient;
     _options    = options;
 }