ASP.NET MVC涉及到的5个同步与异步,你是否傻傻分不清楚?[下篇]
关于ASP.NET MVC对请求的处理方式(同步或者异步)涉及到的五个组件,在《上篇》中我们谈了三个(MvcHandler、Controller和ActionInvoker),现在我们来谈余下的两个,即ControllerDescriptor和ActionDescriptor,这五个组件的执行并非孤立的,而是具有议定的关系。相信读者认真阅读了这两篇文章后,会对整个请求的处理方式有一个深刻的理解。[本文已经同步到《How ASP.NET MVC Works?》中]
目录
一、MvcHandler的同步于异步
二、Controller的同步与异步
三、ActionInvoker的同步与异步
四、ControllerDescriptor的同步与异步
五、ActionDescriptor的同步与异步
1、实例演示:AsyncActionInvoker对ControllerDescriptor的创建
2、AsyncController、AsyncControllerActionInvoker与AsyncActionDescriptor
3、Action方法的执行
四、ControllerDescriptor的同步与异步
如果采用ControllerActionInvoker,Action总是以同步的方式来直接,但是当AsyncControllerActionInvoker作为Controller的ActionInvoker时,并不意味着总是以异步的方式来执行所有的Action。至于这两种类型的ActionInvoker具体采用对Action的怎样的执行方式,又涉及到两个描述对象,即用于描述Controller和Action的ControllerDescriptor和ActionDescriptor。
通过前面“Model的绑定”中对这两个对象进行过相应的介绍,我们知道在ASP.NET MVC应用编程接口中具有两个具体的ControllerDescriptor,即ReflectedControllerDescriptor和ReflectedAsyncControllerDescriptor,它们分别代表同步和异步版本的ControllerDescriptor。
1: public class ReflectedControllerDescriptor : ControllerDescriptor
2: {
3: //省略成员
4: }
5:
6: public class ReflectedAsyncControllerDescriptor : ControllerDescriptor
7: {
8: //省略成员
9: }
ReflectedControllerDescriptor和ReflectedAsyncControllerDescriptor并非对分别实现了IController和IAyncController接口的Controller的描述,而是对直接继承自抽象类Controller和AsyncController的Controller的描述。它们之间的区别在于创建者的不同,在默认情况下ReflectedControllerDescriptor和ReflectedAsyncControllerDescriptor分别是通过ControllerActionInvoker和AsyncControllerActionInvoker来创建的。ActionInvoker和ControllerDescriptor之间的关系可以通过如下图所示的UML来表示。
ActionInvoker与ControllerDescriptor之间的关系可以通过一个简单的实例来验证。在通过Visual Studio的ASP.NET MVC项目模板创建的空Web应用中,我们自定义了如下两个分别继承自ControllerActionInvoker和AsyncControllerActionInvoker的ActionInvoker类型。在这两个自定义ActionInvoker中,定义了公有的GetControllerDescriptor方法覆盖了基类的同名方法(受保护的虚方法),并直接直接调用基类的同名方法根据提供的Controller上下文的到相应的ControllerDescriptor对象。
1: public class FooActionInvoker : ControllerActionInvoker
2: {
3: public new ControllerDescriptor GetControllerDescriptor(ControllerContext controllerContext)
4: {
5: return base.GetControllerDescriptor(controllerContext);
6: }
7: }
8:
9: public class BarActionInvoker : AsyncControllerActionInvoker
10: {
11: public new ControllerDescriptor GetControllerDescriptor(ControllerContext controllerContext)
12: {
13: return base.GetControllerDescriptor(controllerContext);
14: }
15: }
然后我们定义了两个Controller类型,它们均是抽象类型Controller的直接继承者。如下面的代码片断所示,这两Controller类(FooController和BarController)都重写了虚方法CreateActionInvoker,而返回的ActionInvoker类型分别是上面我们定义的FooActionInvoker和BarActionInvoker。在默认的Action方法Index中,我们利用当前的ActionInvoker得到用于描述本Controller的ControllerDescriptor对象,并将其类型呈现出来。
1: public class FooController : Controller
2: {
3: protected override IActionInvoker CreateActionInvoker()
4: {
5: return new FooActionInvoker();
6: }
7:
8: public void Index()
9: {
10: ControllerDescriptor controllerDescriptor = ((FooActionInvoker)this.ActionInvoker).GetControllerDescriptor(ControllerContext);
11: Response.Write(controllerDescriptor.GetType().FullName);
12: }
13: }
14:
15: public class BarController : Controller
16: {
17: protected override IActionInvoker CreateActionInvoker()
18: {
19: return new BarActionInvoker();
20: }
21:
22: public void Index()
23: {
24: ControllerDescriptor controllerDescriptor = ((BarActionInvoker)this.ActionInvoker).GetControllerDescriptor(ControllerContext);
25: Response.Write(controllerDescriptor.GetType().FullName);
26: }
27: }
现在我们运行我们的程序,并在浏览器中输入相应的地址对定义在FooController和BarController的默认Action方法Index发起访问,相应的ControllerDescriptor类型名称会以下图所示的形式呈现出来,它们的类型分别是ReflectedControllerDescriptor和ReflectedAsyncControllerDescriptor。
五、ActionDescriptor的执行
Controller包含一组用于描述Action方法的ActionDescriptor对象。由于Action方法可以采用同步和异步执行方法,异步Action对应的ActionDescriptor直接或者间接继承自抽象类AsyncActionDescriptor,后者是抽象类ActionDescriptor的子类。如下面的代码片断所示,同步和异步Action的执行分别通过调用Execute和BeginExecute/EndExecute方法来完成。值得一提的是,AsyncActionDescriptor重写了Execute方法并直接在此方法中抛出一个InvalidOperationException异常,也就是说AsyncActionDescriptor对象只能采用 异步执行方式。
1: public abstract class ActionDescriptor : ICustomAttributeProvider
2: {
3: //其他成员
4: public abstract object Execute(ControllerContext controllerContext, IDictionary<string, object> parameters);
5: }
6:
7: public abstract class AsyncActionDescriptor : ActionDescriptor
8: {
9: //其他成员
10: public abstract IAsyncResult BeginExecute(ControllerContext controllerContext, IDictionary<string, object> parameters, AsyncCallback callback, object state);
11: public abstract object EndExecute(IAsyncResult asyncResult);
12: }
通过前面“Model的绑定”我们知道,在ASP.NET MVC应用编程接口中采用ReflectedControllerDescriptor来描述同步Action。异步Action方法具有两种不同的定义方式:其一,通过两个匹配的方法XxxAsync/XxxCompleted定义;其二,通过返回类型为Task的方法来定义。这两种异步Action方法对应的AsyncActionDescriptor类型分别是ReflectedAsyncActionDescriptor和TaskAsyncActionDescriptor。
对于ReflectedControllerDescriptor来说,包含其中的ActionDescriptor类型均为ReflectedActionDescriptor。而ReflectedAsyncControllerDescriptor描述的Controller可以同时包含同步和异步的Action方法,ActionDescriptor的类型取决于Action方法定义的方式。ControllerDescriptor与ActionDescriptor之间的关系如下图所示的UML来表示。
实例演示:AsyncActionInvoker对ControllerDescriptor的创建
为了让读者对ActionInvoker对ControllerDescriptor的解析机制具有一个深刻的理解,同时也作为对该机制的验证,我们做一个简单的实例演示。通过前面的介绍我们知道在默认的情况下Controller采用AsyncControllerActionInvoker进行Action方法的执行,这个例子就来演示一下它生成的ControllerDescriptor是个怎样的对象。我们通过Visual Studio的ASP.NET MVC项目模板创建一个空Web应用,并创建一个默认的HomeController,然后对其进行如下的修改。
1: public class HomeController : AsyncController
2: {
3: public void Index()
4: {
5: MethodInfo method = typeof(AsyncControllerActionInvoker).GetMethod("GetControllerDescriptor", BindingFlags.Instance | BindingFlags.NonPublic);
6: ControllerDescriptor controllerDescriptor = (ControllerDescriptor)method.Invoke(this.ActionInvoker, new object[] { this.ControllerContext });
7: Response.Write(controllerDescriptor.GetType().FullName + "<br/>");
8: CheckAction(controllerDescriptor, "Foo");
9: CheckAction(controllerDescriptor, "Bar");
10: CheckAction(controllerDescriptor, "Baz");
11:
12: }
13: private void CheckAction(ControllerDescriptor controllerDescriptor,string actionName)
14: {
15: ActionDescriptor actionDescriptor = controllerDescriptor.FindAction(this.ControllerContext, actionName);
16: Response.Write(string.Format("{0}: {1}<br/>",actionName,actionDescriptor.GetType().FullName));
17: }
18:
19: public void Foo() { }
20: public void BarAsync() { }
21: public void BarCompleted() { }
22: public Task<ActionResult> Baz()
23: {
24: throw new NotImplementedException();
25: }
26: }
我们首先将HomeController的基类从Controller改为AsyncController,并定义了Foo、BarAsync/BarCompleted和Baz四个方法,我们知道它们对应着Foo、Bar和Baz三个Action,其中Foo是同步Action,Bar和Baz分别是两种不同定义形式(XxxAsync/XxxCompleted和Task)的异步Action。
CheckAction用于根据指定的Action名称从ControllerDescriptor对象中获取用于表示对应Action的ActionDescriptor对象,最终将类型名称呈现出来。在Index方法中,我们通过反射的方式调用当前ActionInvoker(一个AsyncControllerActionInvoker对象)的受保护方法GetControllerDescriptor或者用于描述当前Controller(HomeController)的ControllerDescriptor的对象,并将类型名称呈现出来。最后通过调用CheckAction方法将包含在创建的ControllerDescriptor对象的三个ActionDescriptor类型呈现出来。
当我们运行该程序的时候,在浏览器中会产生如下的输出结果,从中可以看出ControllerDescriptor类型为ReflectedAsyncControllerDescriptor。同步方法Foo对象的ActionDescriptor是一个ReflectedActionDescriptor对象;以XxxAsync/XxxCompleted形式定义的异步方法Bar对应的ActionDescriptor是一个ReflectedAsyncActionDescriptor对象;而返回类型为Task的方法Baz对应的ActionDescriptor类型则是TaskAsyncActionDescriptor。
AsyncController、AsyncControllerActionInvoker与AsyncActionDescriptor
不论我们采用哪种形式的定义方式,异步Action方法都只能定义在继承自AsyncController的Controller类型中,否则将被认为是同步方法。此外,由于通过ControllerActionInvoker只能创建包含ReflectedActionDescriptor的ReflectedControllerDescriptor,如果我们在AsyncController中采用ControllerActionInvoker对象作为ActionInvoker,所有的Action方法也将被认为是同步的。
我们同样可以采用一个简单的实例演示来证实这一点。在通过Visual Studio的ASP.NET MVC项目模板创建的空Web应用中我们定义如下三个Controller(FooController、BarController和BazController)。我们重写了它们的CreateActionInvoker方法,返回的ActionInvoker类型(FooActionInvoker和BarActionInvoker)定义在上面,在这里我们将FooActionInvoker和BarActionInvoker看成是ControllerActionInvoker和AsyncControllerActionInvoker(默认使用的ActionInvoker)。
1: public class FooController : AsyncController
2: {
3: protected override IActionInvoker CreateActionInvoker()
4: {
5: return new BarActionInvoker();
6: }
7: public void Index()
8: {
9: BarActionInvoker actionInvoker = (BarActionInvoker)this.ActionInvoker;
10: Utility.DisplayActions(controllerContext=>actionInvoker.GetControllerDescriptor(ControllerContext),ControllerContext);
11: }
12:
13: public void DoSomethingAsync()
14: { }
15: public void DoSomethingCompleted()
16: { }
17: }
18:
19: public class BarController : Controller
20: {
21: protected override IActionInvoker CreateActionInvoker()
22: {
23: return new BarActionInvoker();
24: }
25: public void Index()
26: {
27: BarActionInvoker actionInvoker = (BarActionInvoker)this.ActionInvoker;
28: Utility.DisplayActions(controllerContext => actionInvoker.GetControllerDescriptor(ControllerContext), ControllerContext);
29: }
30: public void DoSomethingAsync()
31: { }
32: public void DoSomethingCompleted()
33: { }
34: }
35:
36: public class BazController : Controller
37: {
38: protected override IActionInvoker CreateActionInvoker()
39: {
40: return new FooActionInvoker();
41: }
42: public void Index()
43: {
44: FooActionInvoker actionInvoker = (FooActionInvoker)this.ActionInvoker;
45: Utility.DisplayActions(controllerContext => actionInvoker.GetControllerDescriptor(ControllerContext), ControllerContext);
46: }
47: public void DoSomethingAsync()
48: { }
49: public void DoSomethingCompleted()
50: { }
51: }
52:
53: public static class Utility
54: {
55: public static void DisplayActions(Func<ControllerContext, ControllerDescriptor> controllerDescriptorAccessor, ControllerContext controllerContext)
56: {
57: ControllerDescriptor controllerDescriptor = controllerDescriptorAccessor(controllerContext);
58: string[] actionNames = { "DoSomething", "DoSomethingAsync", "DoSomethingCompleted" };
59: Array.ForEach(actionNames, actionName =>
60: {
61: ActionDescriptor actionDescriptor = controllerDescriptor.FindAction(controllerContext,actionName);
62: if (null != actionDescriptor)
63: {
64: HttpContext.Current.Response.Write(string.Format("{0}: {1}<br/>", actionDescriptor.ActionName, actionDescriptor.GetType().Name));
65: }
66: });
67: }
68: }
我们在三个Controller中以异步Action的形式定义了两个方法DoSomethingAsync和DoSomethingCompleted。FooController继承自AsyncController,使用AsyncControllerActionInvoker作为其ActionInvoker,这是正常的定义;BarController虽然采用AsyncControllerActionInvoker,但是将抽象类Controller作为其基类;而BazController虽然继承自ActionInvoker,但ActionInvoker类型为ControllerActionInvoker。在默认的Action方法Index中,我们将通过DoSomethingAsync和DoSomethingCompleted方法定义的Action的名称和对应的ActionDescriptor类型输出来。
如果我们运行该程序,并在浏览器中输入相应的地址对定义在三个Controller的默认Action方法Index发起访问,会呈现出如下图所示的结果。我们可以清楚地看到,对于以XxxAsync/XxxCompleted形式定义的“异步”Action方法定义,只有针对AsyncController并且采用AsyncControllerActionInvoker的情况下才会被解析为一个异步Action。如果不满足这两个条件,它们会被视为两个普通的同步Action。
Action方法的执行
目标Action方法的最终执行由被激活的Controller的ActionInvoker决定,ActionInvoker最终通过调用对应的ActionDescriptor来执行被它描述的Action方法。如果采用ControllerActionInvoker,被它创建的ControllerDescriptor(ReflectedControllerDescriptor)只包含同步的ActionDescriptor(ReflectedActionDescriptor),所以Action方法总是以同步的方式被执行。
如果目标Controller是抽象类Controller的直接继承者,这也是通过Visual Studio的Controller创建向导的默认定义方式,ActionInvoker(ControllerActionInvoker/AsyncControllerActionInvoker)的选择只决定了创建的ControllerDescriptor的类型(ReflectedControllerDescriptor/ReflectedAsyncControllerDescriptor),ControllerDescriptor包含的所有ActionDescriptor依然是同步的(ReflectedActionDescriptor),所以Action方法也总是以同步的方式被执行。
以异步方式定义的Action方法(XxxAsync/XxxCompleted或采用Task返回类型)只有定义在继承自AsyncController的Controller类型中,并且采用AsyncControllerActionInvoker作为其ActionInvoker,最终才会创建AsyncActionDescriptor来描述该Action。也只有同时满足这两个条件,Action方法才能以异步的方式执行。
ASP.NET MVC涉及到的5个同步与异步,你是否傻傻分不清楚?[上篇]
ASP.NET MVC涉及到的5个同步与异步,你是否傻傻分不清楚?[下篇]
转载于:https://www.cnblogs.com/artech/archive/2012/06/22/sync-and-async-02.html
ASP.NET MVC涉及到的5个同步与异步,你是否傻傻分不清楚?[下篇]相关推荐
- ASP.NET MVC涉及到的5个同步与异步,你是否傻傻分不清楚?[上篇]
Action方法的执行具有两种基本的形式,即同步执行和异步执行,而在ASP.NETMVC的整个体系中涉及到很多同步/异步的执行方式,虽然在前面相应的文章中已经对此作了相应的介绍,为了让读者对此有一个整 ...
- How ASP.NET MVC Works?
一.ASP.NET + MVC IIS与ASP.NET管道 MVC.MVP以及Model2[上篇] MVC.MVP以及Model2[下篇] ASP.NET MVC是如何运行的[1]: 建立在" ...
- 为ASP.NET MVC扩展异步Action功能(下)
本文分为上下两部分,您也可以从<Extend ASP.NET MVC for Asynchronous Action>获得全部内容. 执行Action方法 对于执行同步Action的Syn ...
- 如何使用jQuery向asp.net Mvc传递复杂json数据
jQuery提供的ajax方法能很方便的实现客户端与服务器的异步交互,在asp.net mvc 框架使用jQuery能很方便地异步获取提交数据,给用户提供更好的体验! 调用jQuery的ajax方法时 ...
- ASP.NET MVC 入门11、使用AJAX
本系列文章基于ASP.NET MVC beta.本示例Blog系统同步更新的演示站点:http://4mvcblog.qsh.in/ 在ASP.NET MVC beta发布之前,M$就宣布支持开源的J ...
- asp.net mvc 5 identity 2.0 注册时密码强度验证
asp.net mvc 5 identity 2.0 注册时密码强度验证 密码强度验证分两部分,一部分是客户端,即浏览器中进行验证,另一部分是 identity 的 UserManager 中进行的. ...
- ASP.NET MVC 重点教程一周年版 第九回 HtmlHelper 【转】
许多时候我们会遇到如下场景 在写一个编辑数据的页面时,我们通常会写如下代码 1: <input type="text" value='<%=ViewData[" ...
- ASP.NET MVC上传文件----uploadify的使用
课程设计需要实现上传文件模块,本来ASP.NET是有内置的控件,但是ASP.NET MVC没有,所以就有两种方法:自定义和采用第三方插件.由于时间的关系,故采用第三方插件:uploadify. upl ...
- 艾伟_转载:学习 ASP.NET MVC (第五回)理论篇
本系列文章导航 学习 ASP.NET MVC (第一回)理论篇 学习 ASP.NET MVC (第二回)实战篇 学习 ASP.NET MVC (第三回)实战篇 学习 ASP.NET MVC (第四回) ...
最新文章
- 西瓜创客+Leangoo敏捷实践企业案例分享
- centos 安装 py pyhs2
- 赌徒都应该明白的道理:通过简单计算告诉你为什么久赌必输
- 数据表_事物码(Transaction Code)信息
- 20165230 2017-2018-2 《Java程序设计》第9周学习总结
- 单点登录在项目中的实现 转.
- [评论]为什么中国的程序员技术偏低
- 《30天自制操作系统》笔记(04)——显示器256色
- 域名到期续费删除的相关规则
- 阿里大数据分析与应用(part4)--一站式大数据平台DataWorks
- 移动超级sim卡 无法下载卡_中国移动发布超级SIM卡:全变了
- JavaScript 数组 API 全解析
- android 顶部标签切换,Android实现底部切换标签
- 多线程十 Timer
- GnuPG 的PGP使用
- Ext js框架模拟Windows桌面菜单管理模板
- 并发控制技术手段之时间戳(二)
- 在Codeblocks下配置GoogleTest单元测试工具
- 左耳朵耗子的技术书籍建议
- FastReport .NET 2022.3.6-新版Crack