AsyncState parameter of ASP. NET

Source: Internet
Author: User

AsyncState parameter of ASP. NET

This is because the default Model Binder cannot know how to obtain an AsyncCallback object from a context. This is quite simple. We only need to construct an AsyncCallbackModelBinder, and its BindModel method only extracts the AsyncCallback object saved in AsyncMvcHandler. BeginProcessRequest and returns it:

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


When an application is started, it is registered as the default Binder of the AsyncCallback type:

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


You can use a similar method for the AsyncState parameter. However, this seems inappropriate because the object type is too broad and cannot explicitly represent the AsyncState parameter. In fact, even if you do not set a binder for asyncState, The AsyncState is always null for an asynchronous ASP. NET Request. If you must specify a binder, we recommend that you mark the following Attribute on the asyncState parameter of each Action method. It and AsyncStateModelBinder are also created in the project:

 
 
  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.     }  


The usage is as follows:

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


In fact, the Controller-based extension methods GetAsyncCallback and GetAsyncState are both public methods, you can also make the Action method not accept the two parameters and get them directly from the Controller-of course, this method reduces testability and is not worth advocating. The above describes the AsyncState parameters of ASP. NET.

  1. Introduction to ASP. NET 2.0 Virtual Hosts
  2. Introduction to ASP. NET Applications
  3. Optimize ASP. NET 2.0 Profile Provider
  4. ASP. NET pipeline Optimization
  5. Introduction to ASP. NET Routing Engine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.