【问题标题】:Asp.net Web Api [duplicate]Asp.net Web Api [重复]
【发布时间】:2018-07-08 03:40:57
【问题描述】:

我正在通过一些代码来学习如何使用 Web API。

public async Task<List<TodoItem>> RefreshDataAsync ()
{

  // RestUrl = http://developer.xamarin.com:8081/api/todoitems/
  var uri = new Uri (string.Format (Constants.RestUrl, string.Empty));
  var response = await client.GetAsync (uri);
  if (response.IsSuccessStatusCode) {
      var content = await response.Content.ReadAsStringAsync ();
      Items = JsonConvert.DeserializeObject <List<TodoItem>> (content);
  }
}

我找到了这段代码。我只想知道为什么我们使用 AsyncAwait。 正如我所观察到的,当函数被封装为 Async 关键字时,方法主体中的 Await 是强制性的。

【问题讨论】:

  • 这不是特定于 web api 的,而是关于为什么应该在 asp.net 中使用 async-await 的更多问题。尝试详细说明这个问题的标题

标签: asp.net-web-api


【解决方案1】:

这取决于您的应用程序。对于 GUI 应用程序,它可以防止用户界面冻结,并允许该线程在进行调用时执行其他操作。对于 api,如果您有阻塞 IO 操作,它可以允许可扩展性。这是一个类似的问题,有一些很好的解释:Why should I create async WebAPI operations instead of sync ones?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    相关资源
    最近更新 更多