Using Asynchronous Methods in ASP.NET MVC 4

asp.net mvc中的异步只能增加系统的性能,原来需要500个线程的,现在需要50个就够了,对一些常规的程序运行的时间上并没有减少,但是如何IO操作或者其他操作是非常耗时的,就可以使用异步来跳过,减少程序的反应时间

简单理解:

同步controller

public ActionResult GetSomething(){}

同步controller里面可以使用异步方法来加快速度,但是这种情况是在接下来的程序里不需要这个异步程序的返回值,或者很晚才需要这个异步返回的值

异步controller

public async Task(ActionResult> GetSomething(){}

异步controller是为了那些只写了异步的方法来设置的,比如某些方法只有异步的实现,没有同步的实现,而接下来的函数又需要这些异步的返回值,怎么办?只能await等待这些值返回,所以就需要使用async task await配对来调用异步的函数并等待他们返回,如果不需要返回值,那就不需要async task这个前缀了。由此可见,这个async task 是为了实现异步函数的同步实现的,其本身又是一个异步的task,可以被别人异步的执行,这样就好理解了

截取文章中的内容:什么时候用同步的方法,什么时候用异步的方法

Choosing Synchronous or Asynchronous Action Methods

This section lists guidelines for when to use synchronous or asynchronous action methods. These are just guidelines; examine each application individually to determine whether asynchronous methods help with performance.

In general, use synchronous methods for the following conditions:

  1. The operations are simple or short-running.
  2. Simplicity is more important than efficiency.
  3. The operations are primarily CPU operations instead of operations that involve extensive disk or network overhead. Using asynchronous action methods on CPU-bound operations provides no benefits and results in more overhead.

In general, use asynchronous methods for the following conditions:

  1. You're calling services that can be consumed through asynchronous methods, and you're using .NET 4.5 or higher.
  2. The operations are network-bound or I/O-bound instead of CPU-bound.
  3. Parallelism is more important than simplicity of code.
  4. You want to provide a mechanism that lets users cancel a long-running request.
  5. When the benefit of switching threads out weights the cost of the context switch. In general, you should make a method asynchronous if the synchronous method waits on the ASP.NET request thread while doing no work. By making the call asynchronous, the ASP.NET request thread is not stalled doing no work while it waits for the web service request to complete.

Testing shows that the blocking operations are a bottleneck in site performance and that IIS can service more requests by using asynchronous methods for these blocking calls.

The downloadable sample shows how to use asynchronous action methods effectively. The sample provided was designed to provide a simple demonstration of asynchronous programming in ASP.NET MVC 4 using .NET 4.5. The sample is not intended to be a reference architecture for asynchronous programming in ASP.NET MVC. The sample program calls ASP.NET Web API methods which in turn call Task.Delay to simulate long-running web service calls. Most production applications will not show such obvious benefits to using asynchronous action methods.

asp.net--mvc--异步编程的更多相关文章

  1. c#异步编程(三)—ASP.NET MVC 异步控制器及EF异步操作

    ASP.NET MVC 异步控制器及EF异步操作 异步控制器 ASP.NET MVC2后开始了对异步请求管道的支持,异步请求管道的作用是允许web服务器处理长时间运行的请求,比如 那些花费大量时间等待 ...

  2. asp.net mvc异步查询

    对于asp.net mvc异步查询 如何做MVC异步查询,做列表页面. 查询是项目中必不可少的工作,而且不同的项目不同的团队,都有自己的简单方法.Asp.net mvc 有自己独特的优势,下面是结合m ...

  3. ASP.NET MVC异步验证是如何工作的03,jquery.validate.unobtrusive.js是如何工作的

    在上一篇"ASP.NET MVC异步验证是如何工作的02,异步验证表单元素的创建"中了解了ASP.NET异步验证是如何创建表单元素的,本篇体验jquery.validate.uno ...

  4. ASP.NET MVC异步验证是如何工作的02,异步验证表单元素的创建

    在上一篇"ASP.NET MVC异步验证是如何工作的01,jQuery的验证方式.错误信息提示.validate方法的背后"中,了解了jQuery如何验证,如何显示错误信息,本篇要 ...

  5. ASP.NET MVC异步验证是如何工作的01,jQuery的验证方式、错误信息提示、validate方法的背后

    ASP.NET MVC借助jQuery的验证机制,提供了一套从客户端到服务端异步验证的解决方案,通常情况下,用起来相当方便.但面对一些相对特殊的情况,可能会遇到验证失效的场景,比如在使用ajax动态异 ...

  6. ASP.Net Core异步编程

    ASP.Net Core异步编程 概念 什么是异步编程? 异步编程是可以让程序并行运行的一种手段,其可以让程序中的一个工作单元与主应用程序线程分开独立运行,并且在工作单元运行结束后,会通知主应用程序线 ...

  7. .Net Mvc 异步编程

    关于在mvc/webapi 中 async/await 异步编程的探究和整理 你可以用双手玩转多个球 查看调试器windbg和sos.dll调试器扩展或挖掘W3SVC日志 设置minWorkerThr ...

  8. 对于asp.net mvc异步查询

    如何做MVC异步查询,做列表页面. 查询是项目中必不可少的工作,而且不同的项目不同的团队,都有自己的简单方法.Asp.net mvc 有自己独特的优势,下面是结合mvc实现一个产品列表的Demo. 问 ...

  9. ASP.NET MVC简单编程之(二)经典路由篇

    话题:请求从路由开始 在实际的ASP.NET MVC开发中,URL访问规则----路由的定义是非常重要的.因为任何一个请求都离不开路由.理解它,我们将能理解MVC处理请求的整个过程,灵活地定义系统各种 ...

  10. Asp.Net Mvc异步上传文件的方式

    今天试了下mvc自带的ajax,发现上传文件时后端action接收不到文件, Request.Files和HttpPostedFileBase都接收不到.....后来搜索了下才知道mvc自带的Ajax ...

随机推荐

  1. 湖南集训day8

    难度:☆☆☆☆☆☆☆ /* 可以先考虑一维,可知 模k意义下相同的前缀和任意两个相减都是k的倍数 问题等价于统计前缀何种模k相同的数的对数. 多维的时候二维前缀和,压行或者压列,n^3可以解决. */ ...

  2. codevs1574广义斐波那契数列

    1574 广义斐波那契数列  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond     题目描述 Description 广义的斐波那契数列是指形如an=p* ...

  3. Nginx 配置https请求

    通过阿里云生成指定的https证书文件xxxx.key 和 xxxx.pem文件 在阿里云上申请的https证书的是pem格式,转成cer 先在终端cd到文件目录下 然后 openssl x509 - ...

  4. c的二级指针

    ----"c 语言的精华在于指针的灵活性.学好指针的目的在于尽可能少的使用指针." 在敲binary search tree(二叉查找树),遇到了问题.在删除的时候,如果删除的是r ...

  5. 3 工欲善其事必先利其器,C#开发环境准备

    1.       为什么选择C#作为入门的编程语言开始学习? Java和C#是比较主流的两大开发语言.(除了这两种还有什么比较流行的开发语言?)相对于Java,C#的入门要容易一些.学习java开发需 ...

  6. SublimeText学习(二)-基本操作

    1.查看已安装的插件 看到已经安装的插件,看到了在上一篇中安装的Emmet 2.设置主题与字体 方法一: 方法二: 工具栏中 [Preference-浏览程序包]找到[Default文件夹]-用Sub ...

  7. Html学习-File控件学习

    情况一:不设置enctype HTML内容 <form action="02Upload.ashx" method="post"> <inpu ...

  8. 笔记《精通css》第3章 盒模型,定位,浮动,清理

    第3章    盒模型,定位,浮动,清理 1.盒模型用到的属性width,height,padding,border,margin 普通文档流的上下垂直margin会叠加 2.块级框 与 行内框, 利用 ...

  9. 01--Java集合知识

    一.Java集合概览 Java中集合分2大块,Collection和Map,分别代表不同功能的集合类对象,整体结构图如下: Collection├List│├LinkedList│├ArrayList ...

  10. 挂载硬盘,提示 mount: unknown filesystem type 'LVM2_member'的解决方案

    问题现象:由于重装linux,并且加了固态硬盘,直接将系统装在固态硬盘中.启动服务器的时候, 便看不到原来机械硬盘的挂载目录了,不知如何访问机械硬盘了.直接用命令 mount /dev/sda3 /s ...