I have a method that more or less calls three different Async methods and then returns a "Result" object containing a success message. Any of those three Async methods may throw a SshException
, so I've placed all three within a try{}catch{}
that traps the AggregateException
, handles the SshException
, and then returns a failure result object.
Since I'm on .Net 4.0, I can't use async/await (which I believe would solve my problem easily), but I would like to turn the method into one that returns a Task containing one of two results:
- "Success" if all three previous tasks completed successfully.
- "Failure" if any of the previous tasks faulted. Ideally, task execution would stop at the first canceled task too (e.g. TaskContinuationOptions.NotOnFaulted for the three work Tasks).
Here's some sample code:
public UploadResult UploadFileToDir(string dir, string text)
{
try
{
SftpClient.Cd(dir).Wait(); // This is a Task
var filename = this.GetFilename().Result; // This is also a Task
SftpClient.WriteFile(filename, text).Result; // This is the last Task
return UploadResult.Success;
}
catch(AggregateException agg)
{
if(agg.InnerException is SshException)
{
return UploadResult.Failure;
}
throw;
}
}