问题描述
以下是我在 C# Windows 应用程序中的代码,其中处理了与 Oracle、FTP 和空引用异常的连接:
{'__header__': b'MATLAB 5.0 MAT-file,Platform: PCWIN64,Created on: qua jul 21 08:43:40 2021','__version__': '1.0','__globals__': [],'u_x': array([[ 0.00000000e+00,1.39250848e-07,8.35505086e-07,...,2.99900000e+01,2.99968199e+01,3.00000000e+01],[ 0.00000000e+00,-5.48522290e-11,-1.97447920e-09,5.29180779e+00,5.22323380e+00,5.20265331e+00]])}
使用这段代码,我不能在函数中间使用 <script src="/v2/resources.js" defer=""></script>
关键字,因为 Polly 框架不允许这样做。
您能否建议在函数中间处理 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;
}
关键字的更好方法是什么?
解决方法
在 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));
您也应该在这里找到 lhs 和 rhs 上的 <Result>
部分。
有了这些知识,您的方法可以像这样重写:
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
必须返回 Result
,这就是 Result.Successful
可以移动到 Execute
块内的原因。
我还建议像这样将策略声明和执行分开:
public Result Execute()
{
...
var strategy = Policy.Wrap(retryPolicyFTP,retryException)
return strategy.Execute(() =>
{
if (some condition)
{
return new Result(ResultType.Failure,"This function is Successful");
}
return Result.Successful;
});
}