ASP VNext 开源服务容错处理库Polly

背景

Polly是一个.NET弹性和瞬态故障处理库,允许开发人员以流畅和线程安全的方式表达诸如重试,断路器,超时,隔离头和回退之类的策略。

Polly面向.NET Standard 1.1(覆盖范围:.NET Core 1.0,Mono,Xamarin,UWP,WP8.1 +)和.NET Standard 2.0+(覆盖范围:.NET Core 2.0 + 、. NET Core 3.0和更高版本的Mono,Xamarin和UWP目标)。nuget软件包还包括.NET Framework 4.6.1和4.7.2的直接目标。

使用方式

通过NuGet安装

Install-Package Polly

代码实现

故障处理,响应策略

故障处理策略处理您通过策略执行的委托引发的特定异常或          返回的结果。

  步骤1:指定您希望策略处理的异常/错误

// Single exception type
Policy.Handle<HttpRequestException>()// Single exception type with condition
Policy.Handle<SqlException>(ex => ex.Number == 1205)// Multiple exception types
Policy.Handle<HttpRequestException>().Or<OperationCanceledException>()// Multiple exception types with condition
Policy.Handle<SqlException>(ex => ex.Number == 1205).Or<ArgumentException>(ex => ex.ParamName == "example")// Inner exceptions of ordinary exceptions or AggregateException, with or without conditions
// (HandleInner matches exceptions at both the top-level and inner exceptions)
Policy.HandleInner<HttpRequestException>().OrInner<OperationCanceledException>(ex => ex.CancellationToken != myToken)

步骤2:指定策略应如何处理这些故障

2.1、重试

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

2.2、永远重试(直到成功)

// Retry forever
Policy.Handle<SomeExceptionType>().RetryForever()// Retry forever, calling an action on each retry with the 
// current exception
Policy.Handle<SomeExceptionType>().RetryForever(onRetry: exception =>{// Add logic to be executed before each retry, such as logging       });// Retry forever, calling an action on each retry with the
// current exception and context provided to Execute()
Policy.Handle<SomeExceptionType>().RetryForever(onRetry: (exception, context) =>{// Add logic to be executed before each retry, such as logging       });

2.3、等待并重试

// Retry, waiting a specified duration between each retry. 
// (The wait is imposed on catching the failure, before making the next try.)
Policy.Handle<SomeExceptionType>().WaitAndRetry(new[]{TimeSpan.FromSeconds(1),TimeSpan.FromSeconds(2),TimeSpan.FromSeconds(3)});// Retry, waiting a specified duration between each retry, 
// calling an action on each retry with the current exception
// and duration
Policy.Handle<SomeExceptionType>().WaitAndRetry(new[]{TimeSpan.FromSeconds(1),TimeSpan.FromSeconds(2),TimeSpan.FromSeconds(3)}, (exception, timeSpan) => {// Add logic to be executed before each retry, such as logging    }); // Retry, waiting a specified duration between each retry, 
// calling an action on each retry with the current exception, 
// duration and context provided to Execute()
Policy.Handle<SomeExceptionType>().WaitAndRetry(new[]{TimeSpan.FromSeconds(1),TimeSpan.FromSeconds(2),TimeSpan.FromSeconds(3)}, (exception, timeSpan, context) => {// Add logic to be executed before each retry, such as logging    });// Retry, waiting a specified duration between each retry, 
// calling an action on each retry with the current exception, 
// duration, retry count, and context provided to Execute()
Policy.Handle<SomeExceptionType>().WaitAndRetry(new[]{TimeSpan.FromSeconds(1),TimeSpan.FromSeconds(2),TimeSpan.FromSeconds(3)}, (exception, timeSpan, retryCount, context) => {// Add logic to be executed before each retry, such as logging    });// Retry a specified number of times, using a function to 
// calculate the duration to wait between retries based on 
// the current retry attempt (allows for exponential backoff)
// In this case will wait for
//  2 ^ 1 = 2 seconds then
//  2 ^ 2 = 4 seconds then
//  2 ^ 3 = 8 seconds then
//  2 ^ 4 = 16 seconds then
//  2 ^ 5 = 32 seconds
Policy.Handle<SomeExceptionType>().WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)) );// Retry a specified number of times, using a function to 
// calculate the duration to wait between retries based on 
// the current retry attempt, calling an action on each retry 
// with the current exception, duration and context provided 
// to Execute()
Policy.Handle<SomeExceptionType>().WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (exception, timeSpan, context) => {// Add logic to be executed before each retry, such as logging});// Retry a specified number of times, using a function to 
// calculate the duration to wait between retries based on 
// the current retry attempt, calling an action on each retry 
// with the current exception, duration, retry count, and context 
// provided to Execute()
Policy.Handle<SomeExceptionType>().WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (exception, timeSpan, retryCount, context) => {// Add logic to be executed before each retry, such as logging});

开源地址

https://github.com/App-vNext/Polly

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/306266.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python中定制类_python定制类__str__(实例详解)

在接下来的文章中,让我们明白什么是python中的自定义类。学习什么是python的自定义类,python定制类可以扮演何种角色在python编程。当你看到像__xxx__ __slots__变量或函数名,你应该注意他们。这些在Python中有特殊用途。我们已经知道如何使用__slots__,我们也知道__len__()方法…

如何在 C# 中使用 Exceptionless

背景“Exceptionless”一词的定义是&#xff1a;无例外。Exceptionless可为您的JavaScript&#xff0c;Node&#xff0c;.NET Core&#xff0c;ASP.NET&#xff0c;Web API&#xff0c;WebForms&#xff0c;WPF&#xff0c;控制台和MVC应用程序提供实时错误报告。它将收集到的信…

xilinx7中管脚mrcc和srcc_Xilinx 7系列FPGA架构之SelectIO结构(一)

引言&#xff1a;从本文开始我们介绍Xilinx 7系列FPGA的SelectIO资源结构及使用&#xff0c;我们在进行FPGA外设硬件及软件设计时&#xff0c;如ADC、PHY、DDR3等&#xff0c;通常会涉及到该资源。本节我们介绍以下知识点&#xff1a;SelectIO资源概述及结构SelectIO管脚通用设…

IdentityServer4系列 | 混合模式

一、前言在上一篇关于授权码模式中&#xff0c; 已经介绍了关于授权码的基本内容&#xff0c;认识到这是一个拥有更为安全的机制,但这个仍然存在局限&#xff0c;虽然在文中我们说到通过后端的方式去获取token,这种由web服务器和授权服务器直接通信&#xff0c;不需要经过用户的…

3m格式的文件怎么转换成mp3_一招就能让PDF与其他格式文件相互转换,这样的大招你值得拥有...

大家都知道&#xff0c;现在不少的大佬比起用Word、Excel等等这些格式文件&#xff0c;它们更喜欢使用PDF文件。而我们不管是将Word、Excel等文件转换成PDF&#xff0c;还是将PDF转换成其他格式文件&#xff0c;都是一件麻烦事&#xff0c;更别说在手机上操作这一切了。别担心&…

如何使用 C# 中的 Tuple

开局一张图&#xff0c;首先声明的是 Tuple 不是什么新鲜概念&#xff0c;在编程语言 F#&#xff0c;Python 中早就有这个了&#xff0c;Tuple 是一种 有序的&#xff0c;有限不可变的&#xff0c;支持混杂类型的固定个数的 一种数据结构&#xff0c;有些朋友可能就想问了&…

中设置colorbar_【值得收藏】如何画出学术论文中需要的各种精美插图,看这一篇就够了!...

本文整理自知乎问答&#xff0c;仅用于学术分享&#xff0c;著作权归作者所有。如有侵权&#xff0c;请联系后台作删文处理。方法一作者&#xff5c;冯昱尧https://www.zhihu.com/question/21664179/answer/18928725强烈推荐 Python 的绘图模块 matplotlib: python plotting 。…

一文说通C#中的异步迭代器

今天来写写C#中的异步迭代器 - 机制、概念和一些好用的特性迭代器的概念迭代器的概念在C#中出现的比较早&#xff0c;很多人可能已经比较熟悉了。通常迭代器会用在一些特定的场景中。举个例子&#xff1a;有一个foreach循环&#xff1a;foreach (var item in Sources) {Console…

GraphQL:Descriptor Attributes

GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时。GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述&#xff0c;使得客户端能够准确地获得它需要的数据&#xff0c;而且没有任何冗余&#xff0c;也让 API 更容易地随着时间推移而演进&#xff0c…

idea怎么把代码放到git_在IDEA中如何初始化Git,把项目推送到Git上

在IDEA中如何初始化Git&#xff0c;把项目推送到Git上登录Gitee(码云)账号&#xff0c;新建仓库先按如下步骤简单新建一个仓库&#xff1a;创建成功后&#xff0c;会出现下图中所示的原始文件&#xff1a;在IDEA上的Terminal中进行操作注意&#xff1a;可能有些朋友刚打开Termi…

大会线上同步直播, 来不到现场也可以线上看直播,以及参会秘籍

紧急提醒&#xff1a;还有1天&#xff01;2020.NET开发者大会就要开幕啦&#xff01;你都做好参会准备没有&#xff1f;特殊时期&#xff0c;为方便小伙伴们顺利参会&#xff0c;小编特意整理了这篇大会参会攻略&#xff0c;大到各种日程安排、小到签到、出行、防疫等&#xff…

大曾幽默打油诗_这才是真正的幽默打油诗,逗人一笑,又引人深思!

阅读本文前&#xff0c;请您先点击上面的蓝色字体“点点星光”&#xff0c;再点击“关注”&#xff0c;这样您就可以继续免费收到文章了。每天都有分享。完全是免费订阅&#xff0c;请放心关注来源&#xff1a;诗词天地大曾 &#xff0c;曾初良&#xff0c;也乐斋主&#xff0c…

如何使用第三方日志记录提供程序替代.NET Core中的内置程序

背景.NET Core 支持适用于各种内置和第三方日志记录提供程序的日志记录 API。 先来看下如何将日志记录 API 与内置提供程序一起使用。调用 CreateDefaultBuilder&#xff0c;这将添加以下日志记录提供程序&#xff1a;控制台调试EventSourceEventLog&#xff1a;仅限 Windowspu…

mysql新加不了数据库_MySQL数据库之mysql增加新用户无法登陆解决方法

本文主要向大家介绍了MySQL数据库之mysql增加新用户无法登陆解决方法 &#xff0c;通过具体的内容向大家展现&#xff0c;希望对大家学习MySQL数据库有所帮助。今天安装openstack folsom版本&#xff0c;安装完mysql&#xff0c;为各个服务增加对应的数据库和用户后&#xff0c…

编程去除背景绿幕抠图,基于.NET+OpenCVSharp

摘要&#xff1a;本文介绍了一种使用OpenCVSharp对摄像头中的绿幕视频进行实时“抠人像、替换背景”的方式&#xff0c;对于项目中的算法进行了分析。本文中给出了简化OpenCVSharp中Mat、MatExpr等托管资源释放的方法。本文还介绍了“高效摄像头播放控件”以及和OpenCVSharp的性…

.NET 云原生架构师训练营(模块二 基础巩固 依赖注入)--学习笔记

2.2.1 核心模块--依赖注入什么是依赖注入.NET Core DI 生命周期服务设计服务范围检查ASP.NET Core 依赖注入&#xff1a;https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/dependency-injection?viewaspnetcore-5.0什么是依赖注入Dependency injection 依赖注入Inv…

vue ref 绑定的事件需要移除吗_Vue易遗忘的基础复习(二)

数据请求Vue-resource请求在Vue2.0之后已经被舍弃2. fetch请求因为传统 Ajax &#xff08;指 XMLHttpRequest&#xff09;存在一些令人头疼的问题&#xff1a;配置和调用方式非常混乱&#xff0c;而且基于事件的异步模型写起来也没有现代的 Promise&#xff0c;generator/yield…

如何在 ASP.NET Core 中使用 API 分析器

ASP.NET Core 2.2 引入了 API 分析器&#xff0c;它有利于提高 API 的文档化&#xff0c;API分析器 可以应用在任何带有 ApiController 特性的 Controller 上&#xff0c;本篇就和大家一起讨论下。安装 API 分析器 如果你使用的是 ASP.NET Core 2.2 的话&#xff0c;用 Visual …

.net mysql字符串截取_【MySQL】字符串截取之SUBSTRING_INDEX和【MySQL】字符串四则运算...

substring_index(str,delim,count)str:要处理的字符串delim:分隔符count:计数例子&#xff1a;strwww.google.com1.count是正数&#xff0c;那么就是从左往右数&#xff0c;第N个分隔符的左边的全部内容SELECT SUBSTRING_INDEX(www.google.com,.,1);结果是&#xff1a;wwwSELEC…

用C#+Selenium+ChromeDriver 爬取网页,完美模拟真实的用户浏览行为

背景Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中&#xff0c;就像真正的用户在操作一样。而对于爬虫来说&#xff0c;使用Selenium操控浏览器来爬取网上的数据那么肯定是爬虫中的杀手武器。这里&#xff0c;我将介绍selenium 谷歌浏览器的一般使…