我有一个项目,其中有一个动作目录。在每个动作的核心中,可能会有一个或多个网络调用。因此,核心方法看起来像以下内容:
public async Task<Result> evaluate(){
//some setup chunk
try
{
responsefromUri = await requestManager.Post(resourceUri, "");
}
// The non-retryable exception will directly trickle up.
catch (RetryableException exception)
{
return BuildFailedCheckResult(
StatusCode.Abandoned,
Check1 + " abandoned. Failed to read the response for resource: " + resourceUri + " with exception: " + exception.Message);
}
if (string.IsNullOrEmpty(responsefromUri))
{
return BuildFailedCheckResult(
StatusCode.Failed,
Check1 + " failed. Empty response for resource: " + resourceUri);
}
try
{
responseJson = JObject.Parse(responsefromUri);
}
catch (JsonReaderException e)
{
throw new InvalidOperationException(Check1 + " check failed parsing resource: " + resourceUri, e);
}
// use responseJson to get data and process further
}
对于每个网络调用,都有这个块。我希望将其提取出来。现在,我无法这样做,因为存在一个await,如果要提取它,我需要一个异步方法;但是要返回失败的检查结果,我需要一个out变量,而在异步方法中不允许使用。重构此代码的正确方法是什么?当存在多个网络调用时,该方法会变得非常冗长。