Home >Backend Development >C++ >How Do Async Void and Async Task Methods Differ in Handling Outstanding Operations in ASP.NET?
Count of Outstanding Operations in Async Void and Async Task in ASP.Net
In ASP.Net applications, an async void method differs from an async Task method in its handling of outstanding operations. This distinction can lead to the following exception with async void but not with async Task:
System.InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending
Understanding Async Void
Async void increments the count of outstanding operations when called and decrements it when completed. This implies that it doesn't truly result in "fire and forget" behavior, as ASP.Net tracks its completion.
Why does Async Task work?
When returning a Task, the count of outstanding operations does not increase. Instead, the framework handles waiting asynchronously for the returned Task to complete. This approach eliminates the potential for the exception mentioned above.
Example
public class HomeController : AsyncController { // Will work fine (no exception) public async Task<ActionResult> ThisPageWillLoad() { // Fire and forget task this.FireAndForgetTask(); return await Task.FromResult(this.View("Index")); } private async Task FireAndForgetTask() { var task = Task.Delay(TimeSpan.FromSeconds(3)); await task; } // Will result in an exception due to async void public async Task<ActionResult> ThisPageWillNotLoad() { this.FireAndForgetVoid(); return await Task.FromResult(this.View("Index")); } private async void FireAndForgetVoid() { var task = Task.Delay(TimeSpan.FromSeconds(3)); await task; } }
Historical Context
Microsoft prioritized backwards compatibility and "one ASP.NET" when introducing async to ASP.Net. They maintained support for EAP components but enhanced the core ASP.Net SynchronizationContext to detect and exception out any improper usage of async void in certain scenarios. In the MVC/WebAPI/SignalR world, frameworks adopted async Task naturally, eliminating the need for async void.
Conclusion
In modern ASP.Net applications, it's generally recommended to avoid async void and return Task instead. This approach ensures proper tracking of outstanding operations and eliminates the potential for the aforementioned exception.
The above is the detailed content of How Do Async Void and Async Task Methods Differ in Handling Outstanding Operations in ASP.NET?. For more information, please follow other related articles on the PHP Chinese website!