簡介
Polly是一種.NET彈性和瞬態故障處理庫,允許我們以非常順暢和線程安全的方式來執諸如行重試,斷路,超時,故障恢復等策略。 Polly針對對.NET 4.0,.NET 4.5和.NET Standard 1.1以及.NET Core實現
可以實現熔斷與降級機制
主要參考以下兩個鏈接
參考鏈接
https://www.cnblogs.com/edisonchou/p/9159644.html
https://www.cnblogs.com/CreateMyself/p/7589397.html
例子
private async Task<T> getData<T>(string url, Jwt jwt, string xApiKey)
{
var headers = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>( "authorizationtoken", "bearer " + jwt.access_token ),
new KeyValuePair<string, string>( "x-api-key", xApiKey ),
};
try
{
var policy = Policy<T>.Handle<Exception>().RetryAsync<T>(10);
return await policy.ExecuteAsync(async () =>
{
return await _httpService.RequestObjAsync<T>(url, "GET", headers);
});
}
catch (Exception ex)
{
Console.WriteLine("ERROR!!!!:" + ex.Message);
throw ex;
}
}