等待API调用以button_click方法完成

时间:2018-03-20 12:07:26

标签: c# wpf asynchronous

我有一个WPF应用程序,它通过POST请求将数据发送到Web应用程序。目前,这是通过PostAsync完成的,并且可行。但是,我发现某些请求在另一个请求完成之前完成,这会导致Web应用程序出错。

所以我需要等到POST请求完成后再发送下一个请求。我知道我需要使用Taskawaitasync来解决这个问题,但是我在特定情况下会挣扎。

以下代码是当前代码,没有任何asyncawait,因为由于对象的链接,我不确定如何执行此操作。

首先,我有一个按钮点击事件,它调用一个对象来导出数据:

private void exportButton_Click(object sender, RoutedEventArgs e)
{
  var dataExtractor = new DataExtractor();

  // First API call
  dataExtractor.ExportTestSteps(testblockId, testCase);

  // Second API call
  dataExtractor.ExportReportTestSteps(testCase, executionList);
}

这些调用DataExtractor类中的方法,在获取正确的数据后,这些方法调用实际发送的POST请求:

public class DataExtractor
{
   public void ExportTestSteps(string testblockId, string testCaseUniqueId)
   {
     ...
     new QualityMonitorApi().StoreReportItem(content);
   }

   public void ExportReportTestSteps(string testCaseUniqueId, ExecutionList executionList)
   {
     ...
     new QualityMonitorApi().StoreReportItem(content);
   }     
}

QualityMonitorApi看起来像这样:

public class QualityMonitorApi
{
  private string baseUrl = "http://localhost:3000/api/v1";
  private static readonly HttpClient Client = new HttpClient();

  public void StoreReportItem(string content)
  {
    string url = baseUrl + "/data_extractor/store_report_item";

    var json = new StringContent(content, Encoding.UTF8, "application/json");
    Client.PostAsync(url, json);
  }
}

由于类的链接,我很困惑如何确保API调用2等待API调用1完成?

2 个答案:

答案 0 :(得分:1)

使用async/await

您的异步方法应如下所示:

  public async Task StoreReportItem(string content)
  {
    string url = baseUrl + "/data_extractor/store_report_item";

    var json = new StringContent(content, Encoding.UTF8, "application/json");
    await Client.PostAsync(url, json);
  }

接下来在每个方法中使用async / await:

   public async Task ExportReportTestSteps(string testCaseUniqueId, ExecutionList executionList)
   {
     ...
     await new QualityMonitorApi().StoreReportItem(content);
   }

依旧......

答案 1 :(得分:0)

你不能这样做吗? async void是可以的,只要它是一个事件处理程序方法。

此外,您可以存储调用API的对象(即DataExtractor)以避免重新实例化。

private async void exportButton_Click(object sender, RoutedEventArgs e)
{
  var dataExtractor = new DataExtractor();

  // First API call
  await dataExtractor.ExportTestStepsAsync(testblockId, testCase);

  // Second API call
  await dataExtractor.ExportReportTestStepsAsync(testCase, executionList);
}

public class DataExtractor
{
   public async Task ExportTestStepsAsync(string testblockId, string testCaseUniqueId)
   {
     ...
     await new QualityMonitorApi().StoreReportItemAsync(content);
   }

   public async Task ExportReportTestStepsAsync(string testCaseUniqueId, ExecutionList executionList)
   {
     ...
    await new QualityMonitorApi().StoreReportItemAsync(content);
   }     
}

public class QualityMonitorApi
{
  private string baseUrl = "http://localhost:3000/api/v1";
  private static readonly HttpClient Client = new HttpClient();

  public async Task StoreReportItemAsync(string content)
  {
    string url = baseUrl + "/data_extractor/store_report_item";

    var json = new StringContent(content, Encoding.UTF8, "application/json");
    await Client.PostAsync(url, json);
  }
}

在此处查看有关异步编程的更多信息:https://docs.microsoft.com/en-us/dotnet/csharp/async