简体   繁体   English

带有async / await的ASP.NET Webforms

[英]ASP.NET Webforms with async/await

My Webforms application which is based on .Net 4.6 has to use the async/await-functionality quite extensively. 我的基于.Net 4.6的Webforms应用程序必须非常广泛地使用async / await-functions。 Because I'm quite new to this async/await topic I read quite a lot of best practices like this or this . 因为我对这个async / await主题很陌生,所以我读了很多像这样或者这样的最佳实践。 But I still have some questions for which I haven't found any clear informations. 但我还有一些问题,我没有找到任何明确的信息。

  1. Regarding Page-Lifecycle-Events: I know that eg for the Page_Load-Event it's best practice to avoid async void-methods and register such methods like this: 关于Page-Lifecycle-Events:我知道例如对于Page_Load-Event,最好的做法是避免异步void-methods并注册这样的方法:

     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(); } 

    My problem is that I want to call the async-method as soon as I registered it and not after the OnPreRender-event. 我的问题是我想在注册后立即调用async-method,而不是在OnPreRender事件之后调用。 This should be achieved by calling the ExecuteRegisteredAsyncTasks()-method. 这应该通过调用ExecuteRegisteredAsyncTasks() - 方法来实现。 But in my case this has no effect and the async-method is still invoked after the PreRender-event. 但在我的情况下,这没有任何效果,并且在PreRender事件之后仍然会调用async-method。 But why? 但为什么?

  2. 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: 关于Control-Events:以上面的代码示例中提到的方式注册异步方法更好,还是可以使用async-void签名:

     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. 我找到了两个例子,但没有明确的信息,这是更好的解决方案和原因。

  3. 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 eg when manipulating GUI elements. 关于Context和ConfigureAwait,似乎最佳做法是使用await SomeAsyncMethod.ConfigureAwait(false)以获得更好的性能,并且上下文不重要,而不是在上下文中使用它,例如在操作GUI元素时。 But in my case it seems to make no difference if I call await SomeAsyncMethod.ConfigureAwait(false) in my click-event. 但在我的情况下,如果我在click事件中调用await SomeAsyncMethod.ConfigureAwait(false)似乎没有任何区别。 I can still manipulate my GUI-elements wihtout any problems. 我仍然可以在没有任何问题的情况下操纵我的GUI元素。 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; } } 

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. 所以我想知道为什么GUI元素的操作仍然有效,如果我真的应该在每个上下文不重要的异步方法上使用ConfigureAwait(false),这非常繁琐。 I wonder if this has something to do with the usage of the Ajax-Functionality by Telerik which I use for my Webapplication. 我想知道这是否与我用于Web应用程序的Telerik使用Ajax功能有关。 But this is just an assumption. 但这只是一个假设。

ASP.NET WebForms has it's own asynchronous execution engine. ASP.NET WebForms拥有自己的异步执行引擎。 Please refere to the documentation . 参阅文档

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 . 在任何情况下,您通常希望(或需要)返回事件处理程序等方法的当前同步上下文,因此不应在button1_Click内调用ConfigureAwait(false) ,但应在SomeAsyncMethod调用它。

My simple method for Asp.net 4.5 webforms: 我对Asp.net 4.5 webforms的简单方法:

1) declare aspx page with this attribute 1)使用此属性声明aspx页面

<%@ Page ..... Async="true" ValidateRequest="false" EnableEventValidation="false" %>

2) Create this void method in code: 2)在代码中创建这个void方法:

void MyAsyncMethod( ... list parameters )
{
  //Insert this on end method code
  var objThread = Session["MyAsyncMethod"] as Thread;
  if (objThread != null) objThread.Abort();
}

3) Call method MyAsyncMethod: 3)调用方法MyAsyncMethod:

var objThread = new Thread(
              () => MyAsyncMethod(parameters..)) {IsBackground = true};
objThread.Start();
Session["MyAsyncMethod"] = objThread;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM