c# - 执行 Polly 策略时如何包含 return 语句?
问题描述
下面是我在 C# Windows 应用程序中的代码,其中处理了与 Oracle、FTP 和空引用异常的连接:
public Result Execute()
{
Result result = null;
string errorMessage = string.Empty;
var retryTimes = 1000;
var retryableErrorCodes = new[] { "ORA-03113", "ORA-03114", "ORA-12543",
"ORA-12170", "ORA-12154", "ORA-12541", "ORA-12560", "ORA-03135",
"Connection request timed out" };
var retryExceptionError = new[] { "Object reference not set to an instance of an object" };
RetryPolicy retryPolicyFTP = Policy
.Handle<Xceed.Ftp.FtpInvalidStateException>().Or<Xceed.Ftp.FtpIOException>()
.WaitAndRetry(retryTimes, _ => TimeSpan.FromSeconds(1));
RetryPolicy retryPolicyOracle = Policy
.Handle<OracleException>(ex => retryableErrorCodes.Any(errorCode => ex.Message.ToString().Contains(errorCode)))
.WaitAndRetry(retryTimes, _ => TimeSpan.FromSeconds(1));
RetryPolicy retryException = Policy
.Handle<Exception>(ex => retryExceptionError.Any(errorCode => ex.Message.ToString().Contains(errorCode)))
.WaitAndRetry(retryTimes, _ => TimeSpan.FromSeconds(1));
Policy.Wrap(retryPolicyFTP, retryPolicyOracle, retryException).Execute(() =>
{
//few lines of C# Code like fetching details from Database
if(some condition)
{
//Some Operations
return new Result(ResultType.Failure, "This function has Failed");
}
if(some other condition)
{
//Some Operations
return new Result(ResultType.Success, "This function is Successful");
}
//Some more lines of C# Code
});
return Result.Successful;
}
使用这段代码,我不能return
在函数中间使用关键字,因为 Polly 框架不允许这样做。
您能否建议return
在函数中间处理关键字的更好方法是什么?
解决方案
在 Polly 中,您可以为方法和函数定义装饰器。
在方法的情况下,重试策略应定义如下:
RetryPolicy retryPolicyFTP = Policy
.Handle<Xceed.Ftp.FtpInvalidStateException>().Or<Xceed.Ftp.FtpIOException>()
.WaitAndRetry(retryTimes, _ => TimeSpan.FromSeconds(1));
如果是函数,重试策略应定义如下:
RetryPolicy<Result> retryPolicyFTP = Policy<Result>
.Handle<Xceed.Ftp.FtpInvalidStateException>().Or<Xceed.Ftp.FtpIOException>()
.WaitAndRetry(retryTimes, _ => TimeSpan.FromSeconds(1));
您也应该在这里发现<Result>
lhs 和 rhs 上的部分。
有了这些知识,您的方法可以这样重写:
public Result Execute()
{
Result result = null;
string errorMessage = string.Empty;
var retryTimes = 1000;
var retryableErrorCodes = new[] { "ORA-03113", "ORA-03114", "ORA-12543", "ORA-12170", "ORA-12154", "ORA-12541", "ORA-12560", "ORA-03135", "Connection request timed out" };
var retryExceptionError = new[] { "Object reference not set to an instance of an object" };
RetryPolicy<Result> retryPolicyFTP = Policy<Result>
.Handle<Xceed.Ftp.FtpInvalidStateException>().Or<Xceed.Ftp.FtpIOException>()
.WaitAndRetry(retryTimes, _ => TimeSpan.FromSeconds(1));
RetryPolicy<Result> retryPolicyOracle = Policy<Result>
.Handle<OracleException>(ex => retryableErrorCodes.Any(errorCode => ex.Message.ToString().Contains(errorCode)))
.WaitAndRetry(retryTimes, _ => TimeSpan.FromSeconds(1));
RetryPolicy<Result> retryException = Policy<Result>
.Handle<Exception>(ex => retryExceptionError.Any(errorCode => ex.Message.ToString().Contains(errorCode)))
.WaitAndRetry(retryTimes, _ => TimeSpan.FromSeconds(1));
Result res = Policy.Wrap(retryPolicyFTP, retryPolicyOracle, retryException).Execute(() =>
{
if (some condition)
{
return new Result(ResultType.Failure, "This function has Failed");
}
if (some other condition)
{
return new Result(ResultType.Success, "This function is Successful");
}
return Result.Successful;
});
return res;
}
因为你Execute
必须返回 aResult
这就是为什么Result.Successful
可以在Execute
块内移动。
我还建议像这样分开策略声明和执行:
public Result Execute()
{
...
var strategy = Policy.Wrap(retryPolicyFTP, retryPolicyOracle, retryException)
return strategy.Execute(() =>
{
if (some condition)
{
return new Result(ResultType.Failure, "This function has Failed");
}
if (some other condition)
{
return new Result(ResultType.Success, "This function is Successful");
}
return Result.Successful;
});
}
推荐阅读
- c++ - 运算符重载使用运算符 + 为类模板添加
- exception - 异常向量是中断向量的更好名称吗?(在 ARM 的上下文中)
- swift - SwiftUI NavigationLink - “懒惰”的目的地?或者如何不编码重复的“标签”?
- c - 我从一位同事那里看到了一些有趣的代码。我很困惑发生了什么。它是哪个标准?我没能在 GOOGLE 中找到答案
- c++ - “警告:数组索引表达式中的逗号”但逗号分隔函数参数
- python-3.x - Opencv findCountours 函数
- swift - 如何在 SwiftUI 中使用 ForEach 仅更改一个核心数据项的切换?
- javascript - 从 NodeJS 中的本地文件路径返回一个 File 对象
- reactjs - 使用样式化组件在 React App 中滚动切换基于主题的图像
- javascript - 在javascript中将Oct转换为字符串中的文本