Beispiel #1
0
        private void ConfigurePolly(IServiceCollection service, IHttpRequestRetryHandler retryHandler = null)
        {
            if (_configuration.Polly == null)
            {
                return;
            }


            AsyncRetryPolicy <HttpResponseMessage> policy;



            if (retryHandler == null)
            {
                policy = Policy
                         .Handle <Exception>()
                         .OrResult <HttpResponseMessage>(response => !response.IsSuccessStatusCode)
                         .WaitAndRetryAsync(
                    retryCount: _configuration.Polly.Retries,
                    sleepDurationProvider: retryAttempt =>
                    TimeSpan.FromSeconds(_configuration.Polly.IntervalInSeconds));
            }
            else
            {
                policy = Policy
                         .Handle <Exception>()
                         .OrResult <HttpResponseMessage>(response => !response.IsSuccessStatusCode)
                         .WaitAndRetryAsync(
                    retryCount: _configuration.Polly.Retries,
                    sleepDurationProvider: retryAttempt =>
                    TimeSpan.FromSeconds(_configuration.Polly.IntervalInSeconds),
                    onRetry: retryHandler.OnRetry);
            }

            service.AddPolicyRegistry().Add(_configuration.Polly.Name, policy);
        }
Beispiel #2
0
        private IServiceCollection ConfigureServices(Action <HttpClient> httpClient = null, IHttpRequestRetryHandler retryHandler = null)
        {
            var services = new ServiceCollection();
            Action <HttpClient> httpClientAction;

            if (httpClient == null)
            {
                httpClientAction = c =>
                {
                    c.BaseAddress = new Uri(_configuration.BaseAddress);
                    foreach (var(key, value) in _configuration.RequestHeaders)
                    {
                        c.DefaultRequestHeaders.Add(key, value);
                    }
                }
            }
            ;
            else
            {
                httpClientAction = httpClient;
            }
            ConfigurePolly(services, retryHandler);

            services.AddHttpClient(GetRequestName(), httpClientAction)
            .AddPolicyHandlerFromRegistry(_configuration.Polly.Name);

            return(services);
        }
Beispiel #3
0
 /// <summary>
 /// Creates a request based on request configuration class. Where parameters will be passed as string.<para />
 /// Its also possible to configure httpclient as you wish and pass.<para />
 /// Futhermore you can create your on implementation for retries.  <para />
 /// </summary>
 /// <param name="configuration"> Configuration of request</param>
 /// <param name="httpClientConfigurations">Pass your own httpclient configuration</param>
 /// <param name="retryHandler">Case you want to create a custom retry implementation</param>
 /// <returns>IHttpMethod</returns>
 public static IHttpMethod <string> Build(RequestConfiguration configuration,
                                          Action <HttpClient> httpClientConfigurations = null, IHttpRequestRetryHandler retryHandler = null)
 {
     return(Build <string>(configuration, httpClientConfigurations, retryHandler));
 }
Beispiel #4
0
        /// <summary>
        /// Creates a request based on request configuration class.
        /// Its also possible to configure httpclient as you wish and pass.
        /// Futhermore you can create your on implementation for retries.
        /// </summary>
        /// <typeparam name="TEntity">Parameters type</typeparam>
        /// <param name="configuration"> Configuration of request</param>
        /// <param name="httpClientConfigurations">Pass your own httpclient configuration</param>
        /// <param name="retryHandler">Case you want to create a custom retry implementation</param>
        /// <returns></returns>
        public static IHttpMethod <TEntity> Build <TEntity>(RequestConfiguration configuration, Action <HttpClient> httpClientConfigurations = null, IHttpRequestRetryHandler retryHandler = null, ILogger logger = null)
        {
            var requestBuilder    = new RequestBuilder(configuration);
            var services          = requestBuilder.ConfigureServices(httpClientConfigurations, retryHandler);
            var serviceProvider   = services.BuildServiceProvider();
            var httpClientFactory = requestBuilder.GetService <IHttpClientFactory>(serviceProvider);

            return(requestBuilder.Build <TEntity>(httpClientFactory, logger));
        }