Below is my code in C# Windows application, where connection with Oracle, FTP and Null Reference Exception are handled:
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;
}
With this code, I cannot use return
keyword in the middle of the function, because Polly framework does not allow it to do so.
Could you please suggest what is the better way of handling return
keyword in the middle of the function?