Understanding Async Void in ASP.Net: Outstanding Operations Count
Issue: Async void methods within ASP.Net applications raise the following exception:
System.InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending
Explanation:
Asynchronous methods in ASP.Net increment the count of outstanding operations upon invocation and decrement it upon completion. Async void methods directly notify the ASP.Net SynchronizationContext, bypassing the MVC framework.
Task Return vs. Async Void:
Unlike async void methods, returning a Task from asynchronous methods:
In the example provided:
"Fire and Forget" Misconception:
Async void methods should not be considered "fire and forget" in ASP.Net. While they may appear to be fire-and-forget in some scenarios, the error handling semantics indicate that they are not truly forgetful.
Additional Considerations:
The above is the detailed content of Why Does Async Void Cause `System.InvalidOperationException` in ASP.NET?. For more information, please follow other related articles on the PHP Chinese website!