带有异步/等待的 ASP.NET Webforms [英] ASP.NET Webforms with async/await

查看:24
本文介绍了带有异步/等待的 ASP.NET Webforms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的基于 .Net 4.6 的 Webforms 应用程序必须非常广泛地使用 async/await 功能.因为我对这个 async/await 主题很陌生,所以我阅读了很多最佳实践,例如 this这个.但是我还有一些问题,我还没有找到任何明确的信息.

My Webforms application which is based on .Net 4.6 has to use the async/await-functionality quite extensively. Because I'm quite new to this async/await topic I read quite a lot of best practices like this or this. But I still have some questions for which I haven't found any clear informations.

  1. 关于页面生命周期事件:我知道例如对于 Page_Load-Event 最好的做法是避免异步无效方法并注册这样的方法:

  1. Regarding Page-Lifecycle-Events: I know that e.g. for the Page_Load-Event it's best practice to avoid async void-methods and register such methods like this:

protected void Page_Load(object sender, EventArgs e)
{
   PageAsyncTask pageAsyncTask = new PageAsyncTask(SomeAsyncMethod);
   Page.RegisterAsyncTask(pageAsyncTask);
   //Method to invoke the registered async-methods immedietly and not after the PreRender-Event
   Page.ExecuteRegisteredAsyncTasks();
 }

我的问题是我想在注册后立即调用异步方法,而不是在 OnPreRender 事件之后调用.这应该通过调用 ExecuteRegisteredAsyncTasks() 方法来实现.但在我的情况下,这 没有效果,并且在 PreRender 事件之后仍会调用异步方法.但为什么?

My problem is that I want to call the async-method as soon as I registered it and not after the OnPreRender-event. This should be achieved by calling the ExecuteRegisteredAsyncTasks()-method. But in my case this has no effect and the async-method is still invoked after the PreRender-event. But why?

关于控制事件:是按照我在上面的代码示例中提到的相同方式注册异步方法更好,还是可以使用像这样的异步无效签名:

Regarding Control-Events: Is it better to register async-methods the same way I mentioned in the code-example above or can I use the async-void signature like:

protected async void OnClick(object sender, EventArgs e)
{
await SomeAsyncMethod();
}

我找到了两个例子,但没有明确的信息哪个是更好的解决方案以及为什么.

I found both examples but no clear informations which is the better solution and why.

关于 Context 和 ConfigureAwait,最佳实践似乎是使用 await SomeAsyncMethod.ConfigureAwait(false) 以获得更好的性能,并且在上下文不重要的情况下不使用它上下文例如操作 GUI 元素时.但就我而言,如果我在点击事件中调用 await SomeAsyncMethod.ConfigureAwait(false) 似乎没有区别.我仍然可以在没有任何问题的情况下操作我的 GUI 元素.我使用的例子是这样的:

Regarding Context and ConfigureAwait, it seems to be best practice to use await SomeAsyncMethod.ConfigureAwait(false) for a better performance and where the context is not important and not to use it where the context e.g. when manipulating GUI elements. But in my case it seems to make no difference if I call await SomeAsyncMethod.ConfigureAwait(false) in my click-event. I can still manipulate my GUI-elements wihtout any problems. The example which I uses was this:

private async void button1_Click(object sender, EventArgs e)
{
  button1.Enabled = false;
  try
  {
    await SomeAsyncMethod().ConfigureAwait(false);
  }
  finally
  {
    //Manipulating still works even it's another context
    button1.Enabled = true;
  }
}

所以我想知道为什么对 GUI 元素的操作仍然有效,我是否真的应该在上下文不重要的每个异步方法上使用 ConfigureAwait(false),这很乏味.我想知道这是否与我用于 Web 应用程序的 Telerik 使用 Ajax 功能有关.但这只是一个假设.

So I wonder why the manipulating of the GUI-elements still work and if I really should use ConfigureAwait(false) on every async-method where the context is not important, which is quite tedious. I wonder if this has something to do with the usage of the Ajax-Functionality by Telerik which I use for my Webapplication. But this is just an assumption.

推荐答案

ASP.NET WebForms 有它自己的异步执行引擎.请参考 文档.

ASP.NET WebForms has it's own asynchronous execution engine. Please refer to the documentation.

在任何情况下,您通常都希望(或需要)在诸如事件处理程序之类的方法上返回到当前同步上下文,因此您不应在 内部调用 ConfigureAwait(false)button1_Click,但您应该在 SomeAsyncMethod 中调用它.

In any case, you typically want (or need) to get back to the current synchronization context on methods such as event handlers, so you shouldn't call ConfigureAwait(false) inside button1_Click, but you should call it inside SomeAsyncMethod.

这篇关于带有异步/等待的 ASP.NET Webforms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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