示例#1
0
        private static IAsyncPolicy <HttpResponseMessage> GetRetryPolicy(HttpRequestMessage request, HttpClientSettings settings)
        {
            var retryPolicy = HttpPolicyExtensions.HandleTransientHttpError().RetryAsync(settings.MaxRetryCount);
            var noOp        = Policy.NoOpAsync().AsAsyncPolicy <HttpResponseMessage>();

            return(request.Method == HttpMethod.Get ? retryPolicy : noOp);
        }
示例#2
0
 private static IAsyncPolicy <HttpResponseMessage> GetTimeoutPolicy(HttpClientSettings settings)
 {
     return(Policy.TimeoutAsync <HttpResponseMessage>(TimeSpan.FromSeconds(settings.TimeoutInSeconds)));
 }
示例#3
0
 private static IAsyncPolicy <HttpResponseMessage> GetCircuitBreakerPolicy(HttpClientSettings settings)
 {
     return(HttpPolicyExtensions
            .HandleTransientHttpError()
            .CircuitBreakerAsync(settings.CircuitBreakMaxRetries, TimeSpan.FromSeconds(settings.CircuitBreakBackoffInSeconds)));
 }
示例#4
0
        public static IHttpClientBuilder AddClient <TInterface, TImplementation>(this IServiceCollection services, HttpClientSettings settings)
            where TImplementation : HttpClientBase, TInterface
            where TInterface : class
        {
            Action <IServiceProvider, HttpClient> configureClient = (serviceProvider, client) => {
                client.BaseAddress = new Uri(settings.EndpointUrl);
                var defaultRequetHeaders = client.DefaultRequestHeaders;
                if (defaultRequetHeaders.Accept.All(m => m.MediaType != "application/json"))
                {
                    defaultRequetHeaders.Accept?.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                }

                if (settings.AuthSettings.TokenType != AuthTokenType.None)
                {
                    var accessToken = GetBearerToken(settings.AuthSettings).GetAwaiter().GetResult();
                    defaultRequetHeaders.Add("Authorization", accessToken);
                }
                defaultRequetHeaders.Add("request-id", Activity.Current.Id);
                Console.WriteLine($"Operation sent to request header: {Activity.Current.Id}");
            };

            return(services.AddHttpClient <TInterface, TImplementation>(configureClient)
                   .AddPolicyHandler(GetTimeoutPolicy(settings))
                   .AddPolicyHandler(request => GetRetryPolicy(request, settings))
                   .AddPolicyHandler(GetCircuitBreakerPolicy(settings)));
        }