其中ASP.NET生命周期事件可以异步? [英] Which ASP.NET lifecycle events can be async?

查看:108
本文介绍了其中ASP.NET生命周期事件可以异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个自定义的ASP.NET控件,我刚刚更新它有一个异步加载事件处理程序。现在,我得到这个错误:

I've written a custom ASP.NET control and I just updated it to have an async Load event handler. Now I'm getting this error:

这是异步操作不能在这个时间开始。异步操作只能在异步处理程序或模块内或在在页面生命周期的某些事件开始。如果在执行一个页面出现了这种异常,保证页面被标记为LT;%@页面异步=真正的%>。

An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>.

页面的确实的有&LT;%@页面异步=真正的%GT;已标记。所以,我认为,控件不能拥有异步加载事件处理程序。

The page does have the <%@ Page Async="true" %> tag already. So I take it that controls can't have async load event handlers.

我在哪里可以找到一个COM prehensive被允许以异步的ASP.NET Web表单的生命周期事件的列表?

Where can I find a comprehensive list of events in the ASP.NET webforms lifecycle that are allowed to be async?

推荐答案

达米安·爱德华兹从ASP.NET队给了这样的回答:

Damian Edwards from the ASP.NET team gave this answer:

异步无效的事件处理程序只支持某些
  事件,你已经找到,但实际上只适用于简单
  任务。我们建议使用PageAsyncTask任何真正的异步的任何工作
  复杂性。

Async void event handlers in web forms are only supported on certain events, as you've found, but are really only intended for simplistic tasks. We recommend using PageAsyncTask for any async work of any real complexity.

从ASP.NET团队列维·布罗德里克了这样的回答:

Levi Broderick from the ASP.NET team gave this answer:

在Web应用程序的异步事件本质上是奇兽。异步
  虚空是为火,忘记的编程模型。这在
  Windows用户界面的应用,因为该应用程序坚持四周,直至
  OS杀死它,所以每当异步回调运行有保障
  是一个UI线程它可以与交互。在Web应用程序,
  这种模式分崩离析,因为请求被定义短暂的。如果
  异步回调发生在请求完成后运行,
  但不保证该数据结构回调需要
  互动与仍处于良好的状态。因此,为什么射后不理
  (和异步无效)本质上是在Web应用程序是一个坏主意。

Async events in web applications are inherently strange beasts. Async void is meant for a fire and forget programming model. This works in Windows UI applications since the application sticks around until the OS kills it, so whenever the async callback runs there is guaranteed to be a UI thread that it can interact with. In web applications, this model falls apart since requests are by definition transient. If the async callback happens to run after the request has finished, there is no guarantee that the data structures the callback needs to interact with are still in a good state. Thus why fire and forget (and async void) is inherently a bad idea in web applications.


  说,我们疯狂的体操,力图使很简单的东西像
  Page_Load中的工作,但code支持这个极为复杂
  并不能很好测试超出基本方案什么。所以,如果你
  需要的可靠性我与RegisterAsyncTask坚持。

That said, we do crazy gymnastics to try to make very simple things like Page_Load work, but the code to support this is extremely complicated and not well-tested for anything beyond basic scenarios. So if you need reliability I’d stick with RegisterAsyncTask.

所以我认为,回答我的问题是:这是一个错误的问题

So I think the answer to my question is: "That's the wrong question."

正确的问题是我应该如何异步在我的ASP.NET Web窗体应用程序?答案是插入你的aspx code-隐藏文件里面的这个片段:

The right question would be "How should I be async in my ASP.NET Web Forms application?" And the answer is to insert this snippet inside your aspx code-behind file:

this.RegisterAsyncTask(new PageAsyncTask(async cancellationToken => {
    var result = await SomeOperationAsync(cancellationToken);
    // do something with result.
}));

这同样的伎俩工作机制ASP.NET自定义控件,只要使用 this.Page.RegisterAsyncTask 代替。

This same trick works inside ASP.NET custom controls, just use this.Page.RegisterAsyncTask instead.

这篇关于其中ASP.NET生命周期事件可以异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆