public static IServiceCollection ResolveDependencies(this IServiceCollection services, IConfiguration configuration) { services.AddSingleton <IValidationAttributeAdapterProvider, CpfValidationAttributeAdapterProvider>(); services.AddTransient <HttpClientAuthorizationDelegatingHandler>(); services.AddHttpClient <IWebAppAuthenticationService, WebAppAuthenticationService>(); services.AddHttpClient <IProductsService, ProductsService>() .AddHttpMessageHandler <HttpClientAuthorizationDelegatingHandler>() //.AddTransientHttpErrorPolicy(p => p.WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(1000))); .AddPolicyHandler(PollyPolicies.GetAsyncRetryPolicy()) .AddTransientHttpErrorPolicy(PollyPolicies.GetCircuitBreakerAsync()); #region Refit //services.AddHttpClient("Refit", options => { options.BaseAddress = new Uri(configuration.GetSection("UrlProducts").Value); }) // .AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>() // .AddTypedClient(RestService.For<IProductsServiceRefit>); #endregion services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddScoped <IUser, AspNetUser>(); return(services); }
private Task <HttpResponseMessage> GetWithPolly(string requestUri) { IAsyncPolicy <HttpResponseMessage> policy = null; if (_dictionary.ContainsKey(requestUri)) { policy = _dictionary[requestUri]; } else { policy = PollyPolicies.GetHttpPolicy(new TimeSpan(0, 1, 0)); _dictionary.TryAdd(requestUri, policy); } return(policy.ExecuteAsync(async() => await this.Client.GetAsync(requestUri))); }
/// <summary> /// 基于 Polly 发起 POST 请求 /// </summary> /// <param name="client"></param> /// <param name="requestUri"></param> /// <param name="body"></param> /// <param name="timeout">超时时长,分钟</param> /// <returns></returns> public static Task <HttpResponseMessage> PostWithPolly(this HttpClient client, string requestUri, object body, int timeout = 1) { IAsyncPolicy <HttpResponseMessage> policy = null; if (_dictionary.ContainsKey(requestUri)) { policy = _dictionary[requestUri]; } else { policy = PollyPolicies.GetHttpPolicy(new TimeSpan(0, timeout, 0)); _dictionary.TryAdd(requestUri, policy); } return(policy.ExecuteAsync(async() => await client.PostAsJsonAsync(requestUri, body))); }
public static async Task <RedisValue> StringGetWithPolly(this IDatabase db, RedisKey redisKey, CommandFlags flags = CommandFlags.None) { var key = $"{db.Database}StringGet.RedisValue"; IAsyncPolicy <RedisValue> policy = null; if (_dictionary.ContainsKey(key)) { policy = _dictionary[key] as IAsyncPolicy <RedisValue>; } else { policy = PollyPolicies.GetRedisCommandPolicy <RedisValue>(); _dictionary.TryAdd(key, policy); } return(await policy.ExecuteAsync(async() => { return await db.StringGetAsync(redisKey, flags); })); }
public static async Task <bool> StringSetWithPolly(this IDatabase db, RedisKey redisKey, RedisValue value, TimeSpan?expiry = null, When when = When.Always, CommandFlags flags = CommandFlags.None) { var key = $"{db.Database}StringSet"; IAsyncPolicy <bool> policy = null; if (_dictionary.ContainsKey(key)) { policy = _dictionary[key] as IAsyncPolicy <bool>; } else { policy = PollyPolicies.GetRedisCommandPolicy <bool>(); _dictionary.TryAdd(key, policy); } return(await policy.ExecuteAsync(() => { return Task.FromResult(db.StringSet(redisKey, value, expiry, when, flags)); })); }
public static async Task <bool> ExecuteWithPolly(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int?commandTimeout = null, CommandType?commandType = null, [CallerMemberName] string callerMemberName = "") { var key = cnn.ConnectionString + "ExecuteWithPolly" + callerMemberName; IAsyncPolicy <bool> policy = null; if (_dictionary.ContainsKey(key)) { policy = _dictionary[key] as IAsyncPolicy <bool>; } else { policy = PollyPolicies.GetDbCommandPolicy <bool>(); _dictionary.TryAdd(key, policy); } return(await policy.ExecuteAsync(async() => { return (await cnn.ExecuteAsync(sql, param, transaction, commandTimeout, commandType)) > 0; })); }
public async Task Get() { var academicYear = "1920"; var currentMajor = 1; var currentMinor = 2; var cancellationToken = CancellationToken.None; var version = new Version(currentMajor, currentMinor); var providerMock = new Mock <IApplicationVersionsProvider>(); providerMock .Setup(pm => pm.IsLatestVersion(academicYear, version, cancellationToken)) .ReturnsAsync(false); var policies = new PollyPolicies(Mock.Of <ILogger>()); var controller = NewController(providerMock.Object, policies); var result = await controller.Get(academicYear, currentMajor, currentMinor, cancellationToken); result.Should().BeFalse(); }