最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c# 如何更簡單的使用Polly

 更新時(shí)間:2021年03月23日 10:56:03   作者:victor.x.qu  
這篇文章主要介紹了c# 如何更簡單的使用Polly,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下

Polly是一個(gè)C#實(shí)現(xiàn)的彈性瞬時(shí)錯(cuò)誤處理庫
它可以幫助我們做一些容錯(cuò)模式處理,比如:

  • 超時(shí)與重試(Timeout and Retry)
  • 熔斷器(Circuit Breaker)
  • 艙壁隔離(Bulkhead Isolation)
  • 回退(Fallback)

使用也是非常簡單的,比如:

// Retry multiple times, calling an action on each retry 
// with the current exception and retry count
Policy
 .Handle<SomeExceptionType>()
 .Retry(3, onRetry: (exception, retryCount) =>
 {
 // Add logic to be executed before each retry, such as logging
 });

但是每個(gè)地方我們都得這樣寫,個(gè)人還是不喜,
那么怎么簡化呢?
當(dāng)然是使用 Norns.Urd 這些AOP框架封裝我們常用的東西做成 Attribute 啦

如何實(shí)現(xiàn)簡化呢?

我們來嘗試將 Retry功能 做成 RetryAttribute吧

1.安裝 AOP 框架

自己寫多累呀,用現(xiàn)成的多好呀

dotnet add package Norns.Urd

2.編寫 Retry InterceptorAttribute

 public class RetryAttribute : AbstractInterceptorAttribute
 {
 private readonly int retryCount;

 public RetryAttribute(int retryCount)
 {
  this.retryCount = retryCount;
 }

 public override async Task InvokeAsync(AspectContext context, AsyncAspectDelegate next)
 {
  await Policy.Handle<Exception>()
  .RetryAsync(retryCount)
  .ExecuteAsync(() => next(context));
 }
 }

3.考慮到 async 和 sync 在Polly 有差異,那么我們兼容一下吧

 public class RetryAttribute : AbstractInterceptorAttribute
 {
 private readonly int retryCount;

 public RetryAttribute(int retryCount)
 {
  this.retryCount = retryCount;
 }

 public override void Invoke(AspectContext context, AspectDelegate next)
 {
  Policy.Handle<Exception>()
  .Retry(retryCount)
  .Execute(() => next(context));
 }

 public override async Task InvokeAsync(AspectContext context, AsyncAspectDelegate next)
 {
  await Policy.Handle<Exception>()
  .RetryAsync(retryCount)
  .ExecuteAsync(() => next(context));
 }
 }

4.我們來做個(gè)測試吧

 public class RetryTest
 {
 public class DoRetryTest
 {
  public int Count { get; set; }

  [Retry(2)] // 使用 Retry
  public virtual void Do()
  {
  if (Count < 50)
  {
   Count++; // 每調(diào)用一次就加1
   throw new FieldAccessException();
  }
  }
 }

 public DoRetryTest Mock()
 {
  return new ServiceCollection()
  .AddTransient<DoRetryTest>()
  .ConfigureAop()
  .BuildServiceProvider()
  .GetRequiredService<DoRetryTest>();
 }

 [Fact]
 public void RetryWhenSync()
 {
  var sut = Mock();
  Assert.Throws<FieldAccessException>(() => sut.Do());
  Assert.Equal(3, sut.Count); //我們期望調(diào)用總共 3 次
 }
 }

是的,就是這樣,我們可以在任何地方使用 RetryAttribute

當(dāng)然,一些常見的方法已經(jīng)封裝在了 Norns.Urd.Extensions.Polly

這里通過Norns.Urd將Polly的各種功能集成為更加方便使用的功能

如何啟用 Norns.Urd + Polly, 只需使用EnablePolly()

如:

new ServiceCollection()
 .AddTransient<DoTimeoutTest>()
 .ConfigureAop(i => i.EnablePolly())

TimeoutAttribute

[Timeout(seconds: 1)] // timeout 1 seconds, when timeout will throw TimeoutRejectedException
double Wait(double seconds);

[Timeout(timeSpan: "00:00:00.100")] // timeout 100 milliseconds, only work on async method when no CancellationToken
async Task<double> WaitAsync(double seconds, CancellationToken cancellationToken = default);

[Timeout(timeSpan: "00:00:01")] // timeout 1 seconds, but no work on async method when no CancellationToken
async Task<double> NoCancellationTokenWaitAsync(double seconds);

RetryAttribute

[Retry(retryCount: 2, ExceptionType = typeof(AccessViolationException))] // retry 2 times when if throw Exception
void Do()

CircuitBreakerAttribute

[CircuitBreaker(exceptionsAllowedBeforeBreaking: 3, durationOfBreak: "00:00:01")] 
//or
[AdvancedCircuitBreaker(failureThreshold: 0.1, samplingDuration: "00:00:01", minimumThroughput: 3, durationOfBreak: "00:00:01")]
void Do()

BulkheadAttribute

[Bulkhead(maxParallelization: 5, maxQueuingActions: 10)]
void Do()

有關(guān) Norns.Urd, 大家可以查看 https://fs7744.github.io/Norns.Urd/zh-cn/index.html

以上就是c# 如何更簡單的使用Polly的詳細(xì)內(nèi)容,更多關(guān)于c# 使用Polly的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 基于動(dòng)態(tài)修改App.Config與web.Config的使用詳解

    基于動(dòng)態(tài)修改App.Config與web.Config的使用詳解

    本篇文章是對(duì)動(dòng)態(tài)修改App.Config與web.Config的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C#使用private font改變PDF文件的字體詳解

    C#使用private font改變PDF文件的字體詳解

    這篇文章主要給大家介紹了關(guān)于C#使用private font改變PDF文件的字體的相關(guān)資料,文中通過示例代碼以及圖片介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • asp.net(c#)編程實(shí)現(xiàn)將彩色圖片變灰階圖片的方法示例

    asp.net(c#)編程實(shí)現(xiàn)將彩色圖片變灰階圖片的方法示例

    這篇文章主要介紹了asp.net(c#)編程實(shí)現(xiàn)將彩色圖片變灰階圖片的方法,結(jié)合實(shí)例形式分析了C#圖片讀取及屬性操作相關(guān)技巧,需要的朋友可以參考下
    2017-07-07
  • C#實(shí)現(xiàn)Zip壓縮目錄中所有文件的方法

    C#實(shí)現(xiàn)Zip壓縮目錄中所有文件的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)Zip壓縮目錄中所有文件的方法,涉及C#針對(duì)文件的讀寫與zip壓縮相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • WPF基礎(chǔ)教程之元素綁定詳解

    WPF基礎(chǔ)教程之元素綁定詳解

    這篇文章主要給大家介紹了關(guān)于WPF基礎(chǔ)教程之元素綁定的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • C#使用DllImport調(diào)用非托管的代碼的方法

    C#使用DllImport調(diào)用非托管的代碼的方法

    C#調(diào)用非托管代碼的方式主要有Com調(diào)用、DllImport方式調(diào)用、加載非托管動(dòng)態(tài)鏈接庫、直接執(zhí)行機(jī)器碼等方式?,F(xiàn)在介紹一下我自己常用的DllImport方式調(diào)用MSDN中提到的GetShortPathName方法;
    2013-03-03
  • C#淺拷貝和深拷貝實(shí)例解析

    C#淺拷貝和深拷貝實(shí)例解析

    這篇文章主要介紹了C#淺拷貝和深拷貝,是比較重要的概念,需要的朋友可以參考下
    2014-08-08
  • C#結(jié)束進(jìn)程及子進(jìn)程

    C#結(jié)束進(jìn)程及子進(jìn)程

    這篇文章介紹了C#操作結(jié)束進(jìn)程及子進(jìn)程的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • c# linq的差集,并集,交集,去重代碼(分享)

    c# linq的差集,并集,交集,去重代碼(分享)

    下面小編就為大家分享一篇c# linq的差集,并集,交集,去重代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • c#自定義泛型類的實(shí)現(xiàn)

    c#自定義泛型類的實(shí)現(xiàn)

    本篇文章是對(duì)c#中自定義泛型類的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05

最新評(píng)論

井冈山市| 隆林| 巢湖市| 四平市| 竹溪县| 基隆市| 绩溪县| 阳西县| 荆门市| 高邑县| 库尔勒市| 军事| 岳阳县| 英山县| 永年县| 长葛市| 岗巴县| 灵寿县| 方山县| 临桂县| 崇信县| 绥中县| 曲水县| 宣化县| 仁化县| 兰州市| 大英县| 庆安县| 福州市| 吴桥县| 城口县| 长顺县| 乐都县| 行唐县| 日喀则市| 房产| 伊宁县| 宜章县| 寿阳县| 同心县| 日照市|