- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一个自定义 ASP.NET 控件,并且刚刚将其更新为具有异步 Load 事件处理程序。现在我收到此错误:
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" %>.
页面确实有 <%@ Page Async="true" %>
已经标记了。所以我认为控件不能有异步加载事件处理程序。
在哪里可以找到 ASP.NET Webforms 生命周期中允许异步的事件的完整列表?
最佳答案
ASP.NET 团队的 Damian Edwards 给出了这样的答案:
Async void event handlers in web forms are only supported on certainevents, as you've found, but are really only intended for simplistictasks. We recommend using PageAsyncTask for any async work of any realcomplexity.
来自 ASP.NET 团队的 Levi Broderick 给出了这样的答案:
Async events in web applications are inherently strange beasts. Asyncvoid is meant for a fire and forget programming model. This works inWindows UI applications since the application sticks around until theOS kills it, so whenever the async callback runs there is guaranteedto be a UI thread that it can interact with. In web applications,this model falls apart since requests are by definition transient. Ifthe async callback happens to run after the request has finished,there is no guarantee that the data structures the callback needs tointeract with are still in a good state. Thus why fire and forget(and async void) is inherently a bad idea in web applications.
Thatsaid, we do crazy gymnastics to try to make very simple things likePage_Load work, but the code to support this is extremely complicatedand not well-tested for anything beyond basic scenarios. So if youneed reliability I’d stick with RegisterAsyncTask.
所以我认为我的问题的答案是:“这是错误的问题。”
正确的问题是“我应该如何在 ASP.NET Web 表单应用程序中实现异步?”答案是将此代码片段插入到您的 aspx 代码隐藏文件中:
this.RegisterAsyncTask(new PageAsyncTask(async cancellationToken => {
var result = await SomeOperationAsync(cancellationToken);
// do something with result.
}));
同样的技巧也适用于 ASP.NET 自定义控件,只需使用 this.Page.RegisterAsyncTask
即可。
关于asp.net - 哪些 ASP.NET 生命周期事件可以是异步的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14115620/
我正在开发一个使用多个 turtle 的滚动游戏。玩家 turtle 根据按键命令在 Y 轴上移动。当危害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。我尝试定义一个名为 colliding(
我不明白为什么他们不接受这个作为解决方案,他们说这是一个错误的答案:- #include int main(void) { int val=0; printf("Input:- \n
我正在使用基于表单的身份验证。 我有一个注销链接,如下所示: 以及对应的注销方法: public String logout() { FacesContext.getCurren
在 IIS7 应用程序池中有一个设置 Idle-time out 默认是 20 分钟,其中说: Amount of time(in minutes) a worker process will rem
我是一名优秀的程序员,十分优秀!