ASP.NET mvc4 Controllder 同步还是异步
从抽象类Controller 的定义可以看出他 同时实现了 IAsyncController, IController
public abstract class Controller : ControllerBase, IActionFilter, IAuthorizationFilter, IDisposable, IExceptionFilter, IResultFilter, IAsyncController, IController, IAsyncManagerContainer
{
protected Controller();
}
IAsyncController 就表示异步。
通过属性 DisableAsyncSupport 属性来判断是同步还是异步执行 DisableAsyncSupport=true 表示同步 DisableAsyncSupport=false 表示异步。
但是即使执行BeginExecute/EndExecute 方法,Controller也不一定是 异步方式执行,如下代码片段。
public class HomeController : Controller
{
protected override bool DisableAsyncSupport
{
get { return false; }
} public new HttpResponse Response
{
get { return System.Web.HttpContext.Current.Response; }
} /// <summary>
/// 同步执行
/// </summary>
/// <param name="requestContext"></param>
protected override void Execute(RequestContext requestContext)
{
Response.Write("Execute(); <br/>");
base.Execute(requestContext);
} /// <summary>
/// 同步执行
/// </summary>
protected override void ExecuteCore()
{
Response.Write("ExecuteCore(); <br/>");
base.ExecuteCore();
} /// <summary>
/// 同步异步都会执行
/// </summary>
protected override IAsyncResult BeginExecute(RequestContext requestContext,
AsyncCallback callback, object state)
{
Response.Write("BeginExecute(); <br/>");
return base.BeginExecute(requestContext, callback, state);
} /// <summary>
/// 同步异步都会执行
/// </summary>
/// <param name="asyncResult"></param>
protected override void EndExecute(IAsyncResult asyncResult)
{
Response.Write("EndExecute(); <br/>");
base.EndExecute(asyncResult);
} /// <summary>
/// 异步执行
/// </summary>
protected override IAsyncResult BeginExecuteCore(AsyncCallback callback,
object state)
{
Response.Write("BeginExecuteCore(); <br/>");
return base.BeginExecuteCore(callback, state);
} /// <summary>
/// 异步执行
/// </summary>
/// <param name="asyncResult"></param>
protected override void EndExecuteCore(IAsyncResult asyncResult)
{
Response.Write("EndExecuteCore(); <br/>");
base.EndExecuteCore(asyncResult);
} public ActionResult Index()
{
return Content("Index();<br/>");
}
}
Controller 还继承了一个IAsyncController 接口,这个异步接口并不是指 Controller 是否异步, 而是 Action 方法是否异步执行,在MVC4以前 Action 的异步是通过
定义XxxAsync/XxxCompleted 形式定义异步Action,新版本中为了兼容旧版 所以引用了IAsyncController
在4.0后 Action 通过返回Task的方式来定义是否异步执行Action。
ASP.NET mvc4 Controllder 同步还是异步的更多相关文章
- ASP.NET sync over async(异步中同步,什么鬼?)
async/await 是我们在 ASP.NET 应用程序中,写异步代码最常用的两个关键字,使用它俩,我们不需要考虑太多背后的东西,比如异步的原理等等,如果你的 ASP.NET 应用程序是异步到底的, ...
- 异步多线程 ASP.NET 同步调用异步 使用Result产生死锁
一个方法调用了async方法,要将这个方法本身设计为async. public class BlogController : Controller { public async Task<Act ...
- C# ASP.NET Core使用HttpClient的同步和异步请求
引用 Newtonsoft.Json // Post请求 public string PostResponse(string url,string postData,out string status ...
- ASP.NET WebAPi(selfhost)之文件同步或异步上传
前言 前面我们讲过利用AngularJs上传到WebAPi中进行处理,同时我们在MVC系列中讲过文件上传,本文结合MVC+WebAPi来进行文件的同步或者异步上传,顺便回顾下css和js,MVC作为客 ...
- Asp.Net MVC4新特性指南(2):新特性介绍
上一章讲解了最基本的MVC4说明.今天就介绍下几种新特性的使用例子: 就当大家有MVC3的基础了.在这个基础上在看下面的介绍就容易多了.1.Web API MVC4包括一个更好的解决方案:A ...
- Asp.Net MVC4 + Oracle + EasyUI 学习 第二章
Asp.Net MVC4 + Oracle + EasyUI 第二章 --使用Ajax提升网站性能 本文链接:http://www.cnblogs.com/likeli/p/4236723.html ...
- Asp.Net MVC4 + Oracle + EasyUI 学习 序章
Asp.Net MVC4 + Oracle + EasyUI 序章 -- 新建微软实例 本文链接:http://www.cnblogs.com/likeli/p/4233387.html 1. 简 ...
- ASP.NET MVC EF 中使用异步控制器
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 为什么使用异步操作/线程池 ASP.NET MVC ...
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序使用异步及存储过程
这是微软官方教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第九篇:为ASP.NET MVC应用程序 ...
随机推荐
- Shiro的校验Session是否过期处理的过程
首先开启定时扫描活跃的session进行校验 <!-- shiro会话管理 --> <!-- 即用户登录后就是一次会话,在没有退出之前,它的所有信息都在会话中:会话可以是普通 Jav ...
- 第六章 Validating with the Validation API
CHAPTER 6 Validating with the Validation API Defining and Triggering Validation: An Overview 你可以使用以下 ...
- 网卡流量监控脚本 ( Shell )
#!/bin/bash # Traffic Monitor # author: Xiao Guaishou get_traffic_info(){ recv=`cat /proc/net/dev | ...
- 服务器意外重启导致storm报错的问题处理
解决方法 cat /opt/storm-0.8.2/conf/storm.yaml中找到storm.local.dir设定的目录,备份supervisor和workers两个文件夹,#nohup su ...
- C++ Assert()函数
assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义: #include <assert.h> void assert( i ...
- iOS多线程编程之NSOperation和NSOperationQueue的使用
前一篇 <iOS多线程编程之NSThread的使用> 介绍三种多线程编程和NSThread的使用,这篇介绍NSOperation的使用. 使用 NSOperation的方式有两种, 一种是 ...
- Nginx实现ssl一级、二级域名证书部署并用https访问代理转发服务器
1. 规划 域名 解析IP Nginx代理 htpps://www.devcult.com 47.88.10.155 htpps://auto.devcult.com 47.88.10.155 ...
- CSS的编写规范
一.前言 如上图,页面在渲染和画图时,耗时还是比较多的,这就对我们的编写要求愈加严格. 我们有很多方法来减少上图所示的页面加载耗时的,比如 但是更多的还是在于平时的编写规范,我们需要了解浏览器,让他更 ...
- 【Unity】Collider随骨骼动画运动
Collider位置和角色的动作不一致会导致Mesh互相镶嵌,让游戏失真. 想象一扇门的Collider没随它打开的动画移动,结果就是你看着门开着却穿不过去. 而我遇到的情况是: 角色在执行跑 ...
- springboot用于web开发
1.使用SpringBoot:1)创建SpringBoot应用,选中我们需要的模块:2)SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运行起来3)自己编写业务代码 ...