对Web API的多次异步调用 [英] Multiple async call to Web API

查看:103
本文介绍了对Web API的多次异步调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



如何使用Webclient进行多个异步Web API调用。下面是我需要将其设置为异步的调用,并且一旦完成所有回调,我需要执行一些操作。下面是我需要使用Webclient调用并行调用的3个调用。



以下是来自MVC控制器的调用。


  public JsonResult methodname()

  {


使用(var client = new WebClient {UseDefaultCredentials = true})

          {

byte [] test = client.DownloadData(url);

                byte [] test1 = client.DownloadData(url);

                byte [] test3 = client.DownloadData(url);


}


返回Json(.. ..)


  }



谢谢......




解决方案

对方法使用async / await语法:
使用异步和等待的异步编程(C#和Visual Basic)



Hi,

How to make multiple async Web API call using Webclient. Below is the calls I need to make it as async and once all the callback is completed I need to do some operation. Below is the 3 calls I need to call parallel using Webclient.

Below is the call from MVC controller.

 public JsonResult methodname()
  {

using (var client = new WebClient { UseDefaultCredentials = true })
         {

byte[] test= client.DownloadData(url );
                byte[] test1= client.DownloadData(url);
                byte[] test3= client.DownloadData(url);

}

return Json(....)

  }

Thanks......


解决方案

Use the async/await syntax for methods: Asynchronous Programming with Async and Await (C# and Visual Basic)


这篇关于对Web API的多次异步调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆