ASP.NET的AsyncState参数讲解

来源:岁月联盟 编辑:zhu 时间:2009-07-22

ASP.NET的AsyncState参数讲解

这是因为默认的Model Binder无法得知如何从一个上下文环境中得到一个AsyncCallback对象。这一点倒非常简单,我们只需要构造一个AsyncCallbackModelBinder,而它的BindModel方法仅仅是将AsyncMvcHandler.BeginProcessRequest方法中保存的AsyncCallback对象取出并返回:

  1. public sealed class AsyncCallbackModelBinder : IModelBinder  
  2. {  
  3.     public object BindModel(  
  4.         ControllerContext controllerContext,  
  5.         ModelBindingContext bindingContext)  
  6.     {  
  7.         return controllerContext.Controller.GetAsyncCallback();  
  8.     }  


其使用方式,便是在应用程序启动时将其注册为AsyncCallback类型的默认Binder:

  1. protected void Application_Start()  
  2. {  
  3.     RegisterRoutes(RouteTable.Routes);  
  4.     ModelBinders.Binders[typeof(AsyncCallback)] = new AsyncCallbackModelBinder();  


对于AsyncState参数您也可以使用类似的做法,不过这似乎有些不妥,因为object类型实在过于宽泛,并不能明确代指AsyncState参数。事实上,即使您不为asyncState设置binder也没有太大问题,因为对于一个异步ASP.NET请求来说,其AsyncState永远是null。如果您一定要指定一个binder,我建议您在每个Action方法的asyncState参数上标记如下的Attribute,它和AsyncStateModelBinder也已经被一并建入项目中了:

  1. [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]  
  2. public sealed class AsyncStateAttribute : CustomModelBinderAttribute  
  3. {  
  4.     private static AsyncStateModelBinder s_modelBinder = new AsyncStateModelBinder();  
  5.  
  6.     public override IModelBinder GetBinder()  
  7.     {  
  8.         return s_modelBinder;  
  9.     }  


使用方式如下:

  1. [AsyncAction]  
  2. public ActionResult AsyncAction(AsyncCallback cb, [AsyncState]object state) { ... } 


其实,基于Controller的扩展方法GetAsyncCallback和GetAsyncState均为公有方法,您也可以让Action方法不接受这两个参数而直接从Controller中获取——当然这种做法降低了可测试性,不值得提倡。以上介绍ASP.NET的AsyncState参数