Why you should use async tasks in .NET 4.5 and Entity Framework 6
Improve response times and handle more users with parallel processing
Building a web application using non blocking calls to the data layer is a great way to increase the scalability of your system. Performing a task asynchronously frees up the worker thread to accept another request while work is being done in the background. Until recently, designing your system this way was significantly more complicated. With the .NET framework 4.5 and Entity Framework 6, the task has become trivial.
These talks will help you reshape how you approach work and see your career in a new light.
If you’re starting out building a new ASP .NET MVC 5 application, the new best practice is to use Async tasks for basically everything, unless you have a good reason to do otherwise. Your view controllers can be implemented as async methods, your generic collections have async functions, and with EF6, your data layer can take advantage of the technique as well. This functionality has existed in different forms in the past (PLINQ, Task Parallel Library, etc.) but the coding overhead and added thread management caused many to opt out from implementing it. Today, the System.Threading.Tasks library greatly simplifies the process.
In a .NET web application running on an IIS server, the application pool for the app only has so many threads it can spin up to use for request handling. When a user makes a request for a relatively long running task, the synchronous thread will begin to execute and will be blocked while it’s waiting for data to return, let’s say from an IO operation. If a new request comes in during that time, a new thread will be required to fulfill that request as the existing thread is waiting on IO. Spinning up these new threads requires additional memory and processing, and if your application is heavily used the app pool can quickly run out of available threads and result in an unresponsive application or even a timeout error for the user.
With asynchronous tasks, once the thread reaches the long running task that you’ve called asynchronously, the thread will be released and become available to process further requests while the task is being executed. This will keep your application responsive to the user and it will be able to accommodate significantly more simultaneous requests. In terms of benchmarked performance, I’ve seen load tests show 300% improvement in response times and concurrent connections boost almost 8x [I can’t find the link, but I promise I saw it] over the synchronous counterparts.
It’s not a silver bullet however. There is a time and place for async tasks, knowing when not to use them is just as important as knowing when you should. For a low level guide on how it works, have a look at this MSDN article detailing the process. Basically, the rule of thumb is to use an async task when your operation is accessing a slow medium like the network, disk, or database. If your operation is using data that you know is already in memory (memory stream, cached object, etc.), you’re better off using a synchronous task. Another vital consideration is that async tasks are not thread safe and they are not compatible with lazy loading. To keep your code from imploding, you need to ensure that you never execute two async tasks in parallel using the same data context. You also need to make sure you eager load / include any relational data your object might have to fetch from your async call.
There are some caveats, and even though it’s suggested that you use async tasks as the de facto going forward, you need to consider your application thoroughly before diving in. The performance benefits are too great to ignore, so while it may add a bit of complexity to your software, it should pay off in spades when a few thousand people decide to use it at the same time. Pay close attention to the functions you use it on, structure your data layer in a way that promotes compartmentalization of the context, and know when to use a synchronous call instead. With those things in mind you should be on your way to providing a snappy user experience and software your server administrators will love.
3 reasons to use code first design with Entity Framework
Does relying on an IDE for development make you a bad programmer?
Matthew Mombrea is a software engineer, founder ofCypress North, and a technology enthusiast.
The opinions expressed in this blog are those of the author and do not necessarily represent those of ITworld, its parent, subsidiary or affiliated companies.
Why you should use async tasks in .NET 4.5 and Entity Framework 6的更多相关文章
- [转]Upgrading to Async with Entity Framework, MVC, OData AsyncEntitySetController, Kendo UI, Glimpse & Generic Unit of Work Repository Framework v2.0
本文转自:http://www.tuicool.com/articles/BBVr6z Thanks to everyone for allowing us to give back to the . ...
- Entity Framework 6.0 Tutorials(2):Async query and Save
Async query and Save: You can take advantage of asynchronous execution of .Net 4.5 with Entity Frame ...
- Async/Await - Best Practices in Asynchronous Programming z
These days there’s a wealth of information about the new async and await support in the Microsoft .N ...
- 编程概念--使用async和await的异步编程
Asynchronous Programming with Async and Await You can avoid performance bottlenecks and enhance the ...
- 《C#并发编程经典实例》学习笔记—异步编程关键字 Async和Await
C# 5.0 推出async和await,最早是.NET Framework 4.5引入,可以在Visual Studio 2012使用.在此之前的异步编程实现难度较高,async使异步编程的实现变得 ...
- C#的多线程——使用async和await来完成异步编程(Asynchronous Programming with async and await)
https://msdn.microsoft.com/zh-cn/library/mt674882.aspx 侵删 更新于:2015年6月20日 欲获得最新的Visual Studio 2017 RC ...
- Ext JS学习第十六天 事件机制event(一) DotNet进阶系列(持续更新) 第一节:.Net版基于WebSocket的聊天室样例 第十五节:深入理解async和await的作用及各种适用场景和用法 第十五节:深入理解async和await的作用及各种适用场景和用法 前端自动化准备和详细配置(NVM、NPM/CNPM、NodeJs、NRM、WebPack、Gulp/Grunt、G
code&monkey Ext JS学习第十六天 事件机制event(一) 此文用来记录学习笔记: 休息了好几天,从今天开始继续保持更新,鞭策自己学习 今天我们来说一说什么是事件,对于事件 ...
- C#扫盲篇(四):.NET Core 的异步编程-只讲干货(async,await,Task)
关于async,await,task的用法和解释这里就不要说明了,网上一查一大堆.至于为啥还要写这篇文章,主要是其他文章水分太多,不适合新手学习和理解.以下内容纯属个人理解,如果有误,请高手指正.本文 ...
- ASP.NET 中的 Async/Await 简介
本文转载自MSDN 作者:Stephen Cleary 原文地址:https://msdn.microsoft.com/en-us/magazine/dn802603.aspx 大多数有关 async ...
随机推荐
- 20172302 《Java软件结构与数据结构》第九周学习总结
2018年学习总结博客总目录:第一周 第二周 第三周 第四周 第五周 第六周 第七周 第八周 第九周 教材学习内容总结 第十五章 图 1.图:图(graph)是由一些点(vertex)和这些点之间的连 ...
- Hadoop学习博客
http://zy19982004.iteye.com/blog/monthblog_more
- QT.Qt qmake报错(TypeError: Property 'asciify' of object Core::Internal::UtilsJsExtension)
出错信息 打开左边的"项目" 把右侧的"构建目录"修改成你项目所在的文件夹 再次运行试试 成功!
- iOS 6 新的快捷初始化写法
转:http://www.2cto.com/kf/201408/328974.html 下列简化写法是IOS6.0以后的新写法 NSNumber //简化前的写法: NSNumber *value1; ...
- SharePoint 2019 图文安装教程
前言 SharePoint 2019刚刚发布,很多群友在寻找安装教程,霖雨正好也下载了进行体验,就把完整的安装过程做成图文教程,分享给大家了,有需要的人可以有个参考. 文章从创建虚拟机开始,可能有点啰 ...
- .Net Core AES加密解密
一.AES说明 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准用来替 ...
- postgre 导出单表和导入
pg除了可以通过dump的方式导入和导出.如果只是导出数据,可以直接使用copy 导出 COPY user TO '/tmp/data/test.csv' WITH csv; COPY user(na ...
- 【CSS Demo】纯 CSS 打造 Flow-Steps 导航
low-Steps 导航效果常用于需要表示执行步骤的交互页面,效果如下: 步骤一 步骤二 步骤三 通常使用图片来实现 Flow-Steps 效果,但此方法的灵活性不足,当内容变化较大时就可能需要重 ...
- :app:compileDebugJavaWithJavac
org.gradle.initialization.ReportedException: org.gradle.internal.exceptions.LocationAwareException: ...
- oracle完全删除实例
问题描述: 重建oracle库实例,因此要删除数据库实例 问题解决: 利用自用自带的dbca库管理,实现图形化删除实例 01.登录安装oracle数据库的linux 打开xshell企业版的Xman ...