Async Await异步调用WebApi
先铺垫一些基础知识
//sync method sample
public static void DownLoadWebPage()
{
//TODO cost 5s
Console.WriteLine( "DownLoadWebPage on Thread:{0}", Thread.CurrentThread.ManagedThreadId);
Thread.Sleep();
Console.WriteLine( "End downloading the page.." );
} public static void LoadDatafromDB()
{
//TODO cost 5s
Console.WriteLine( "LoadDataFromDB on Thread:{0}", Thread.CurrentThread.ManagedThreadId);
Thread.Sleep();
Console.WriteLine( "End loading Data.." );
}
public static void OurSyncJob()
{
Console.WriteLine( "start doing things sync" );
DownLoadWebPage();
LoadDatafromDB();
//do some other things
Console.WriteLine( "do some other things" );
}
public static async Task OurAsyncJobTask()
{
Console.WriteLine( "start doing things async" );
var taskA= Task.Run(() => { DownLoadWebPage(); });
var taskB= Task.Run(() => { LoadDatafromDB(); });
await Task.WhenAll(taskA,taskB);
Console.WriteLine( "do some other things" );
}
public class ProductController : ApiController
{
public productRepo repo = new productRepo();
public IEnumerable< Product> getProducts()
{
Thread.Sleep();
return repo.GetAll();
}
}
public class WidgetController : ApiController
{
public widgetRepo repo = new widgetRepo();
public IEnumerable< Widget> getWidgets()
{
Thread.Sleep();
return repo.GetAll();
}
}
public static List <Product > TaskGetProduct()
{
using( HttpClient client= new HttpClient())
{
client.BaseAddress = new Uri( "http://localhost:52593/" );
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue ("application/json" ));
string json = client.GetString("api/Product/Products" );
return JsonConvert.DeserializeObject< List< Product>>(json);
}
}
public static async Task< List< Product>> TaskGetProduct()
{
using( HttpClient client= new HttpClient())
{
client.BaseAddress = new Uri( "http://localhost:52593/" );
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue ("application/json" ));
string json = await client.GetStringAsync("api/Product/Products" );
return JsonConvert.DeserializeObject< List< Product>>(json);
}
} public static async Task< pwgVM> RunTaskGetAll()
{
var task1 = TaskGetItem< Product>();
var task2 = TaskGetItem< Gizmos>();
var task3 = TaskGetItem< Widget>();
await Task.WhenAll(task1,task2,task3);
pwgVM vm = new pwgVM(task1.Result,task2.Result,task3.Result);
return vm;
}

Async Await异步调用WebApi的更多相关文章
- .NET Web应用中为什么要使用async/await异步编程
前言 什么是async/await? await和async是.NET Framework4.5框架.C#5.0语法里面出现的技术,目的是用于简化异步编程模型. async和await的关系? asy ...
- 【转】C# Async/Await 异步编程中的最佳做法
Async/Await 异步编程中的最佳做法 Stephen Cleary 近日来,涌现了许多关于 Microsoft .NET Framework 4.5 中新增了对 async 和 await 支 ...
- 将 async/await 异步代码转换为安全的不会死锁的同步代码
在 async/await 异步模型(即 TAP Task-based Asynchronous Pattern)出现以前,有大量的同步代码存在于代码库中,以至于这些代码全部迁移到 async/awa ...
- C#中 Thread,Task,Async/Await 异步编程
什么是异步 同步和异步主要用于修饰方法.当一个方法被调用时,调用者需要等待该方法执行完毕并返回才能继续执行,我们称这个方法是同步方法:当一个方法被调用时立即返回,并获取一个线程执行该方法内部的业务,调 ...
- spring boot中使用@Async实现异步调用任务
本篇文章主要介绍了spring boot中使用@Async实现异步调用任务,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 什么是“异步调用”? “异步调用”对应的是“同步 ...
- async/await异步处理demo
async/await异步处理demo 下载地址: async/await异步处理demo
- spring boot 学习(十一)使用@Async实现异步调用
使用@Async实现异步调用 什么是”异步调用”与”同步调用” “同步调用”就是程序按照一定的顺序依次执行,,每一行程序代码必须等上一行代码执行完毕才能执行:”异步调用”则是只要上一行代码执行,无需等 ...
- Spring Boot使用@Async实现异步调用
原文:http://blog.csdn.net/a286352250/article/details/53157822 项目GitHub地址 : https://github.com/FrameRes ...
- 深入理解协程(四):async/await异步爬虫实战
本文目录: 同步方式爬取博客标题 async/await异步爬取博客标题 本片为深入理解协程系列文章的补充. 你将会在从本文中了解到:async/await如何运用的实际的爬虫中. 案例 从CSDN上 ...
随机推荐
- Apache-通过CGI执行脚本
1.配置服务器,开启注释 vim /etc/httpd/conf/httpd.conf 292 # (You will also need to add "ExecCGI" to ...
- 【Spark调优】:RDD持久化策略
[场景] Spark对RDD执行一系列算子操作时,都会重新从头到尾计算一遍.如果中间结果RDD后续需要被被调用多次,可以显式调用 cache()和 persist(),以告知 Spark,临时保存之前 ...
- 微信小程序之模版的使用(template)
WXML提供模板(template),可以在模板中定义代码片段,然后在不同的地方调用. 分为两部分,定义模板和使用模板 (1).定义模板:使用 name 属性,作为模板的名字.然后在<templ ...
- odoo开发笔记 -- 用户配置界面如何增加模块访问权限
在odoo设置界面,点击用户,进入用户配置界面,会看到: 访问权 | 个人资料菜单 在访问权 page菜单界面,可以看到系统预制的一些模块都会显示在这里, 那么,我们自己开发的模块如何显示在这块呢,从 ...
- 业余实现一个统计A股数据工具
自己瞎捣鼓了几天 python,数据来源新浪财经,每天收盘启动爬虫抓取一遍,web 端呈现日线与周线数据:实时图表显示上证指数与个股指数等.技术点:scrapy apscheduler sqlalch ...
- php数组方法
查找.筛选与搜索数组元素是数组操作的一些常见功能.下面来介绍一下几个相关的函数. in_array()函数 in_array()函数在一个数组汇总搜索一个特定值,如果找到这个值返回true,否则返回f ...
- 怎么样imageview实现铺满全屏
<ImageView android:layout_width="match_parent" android:layout_height="match_parent ...
- Python基础之好玩的字符串格式化之类C风格
今天白月黑羽和大家说说字符串格式化,在python3中,字符串格式化主要有2种方法,今天先和大家介绍类C风格的printf. printf 风格 这种方式 和 传统的C语言printf函数使用一样的格 ...
- css text-align文字两端对齐
text-align:start | end | left | right | center | justify | match-parent | justify-all justify: 内容两端对 ...
- j2ee高级开发技术课程第八周
介绍一. hashCode()方法和equal()方法的作用其实一样,在Java里都是用来对比两个对象是否相等一致,那么equal()既然已经能实现对比的功能了,为什么还要hashCode()呢? 因 ...