您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)Polly如何在c#項(xiàng)目中使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
Polly是一個C#實(shí)現(xiàn)的彈性瞬時錯誤處理庫
它可以幫助我們做一些容錯模式處理,比如:
超時與重試(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 });
但是每個地方我們都得這樣寫,個人還是不喜,
那么怎么簡化呢?
當(dāng)然是使用 Norns.Urd 這些AOP框架封裝我們常用的東西做成 Attribute 啦
我們來嘗試將 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.我們來做個測試吧
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
這里通過Norns.Urd將Polly的各種功能集成為更加方便使用的功能
如:
new ServiceCollection() .AddTransient<DoTimeoutTest>() .ConfigureAop(i => i.EnablePolly())
[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);
[Retry(retryCount: 2, ExceptionType = typeof(AccessViolationException))] // retry 2 times when if throw Exception void Do()
[CircuitBreaker(exceptionsAllowedBeforeBreaking: 3, durationOfBreak: "00:00:01")] //or [AdvancedCircuitBreaker(failureThreshold: 0.1, samplingDuration: "00:00:01", minimumThroughput: 3, durationOfBreak: "00:00:01")] void Do()
[Bulkhead(maxParallelization: 5, maxQueuingActions: 10)] void Do()
看完上述內(nèi)容,你們對Polly如何在c#項(xiàng)目中使用有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。