Reputation: 2246
I added the following line (exactly as is, so you can try it too) to an asp.net WebApi action:
new SmtpClient { DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory}.SendMailAsync("[email protected]", "[email protected]", "s", "b");
and it caused the action to wait a few seconds, until throwing a 500 error of An asynchronous module or handler completed while an asynchronous operation was still pending.
Clearly asp.net detected that there was an asynchronous task running.
Yet when I add
Task.Delay(10000);
the action completes immediately. Why does asp.net not detect here that there is an asynchronous task running?
Upvotes: 0
Views: 483
Reputation: 21597
SmtpClient.SendAsync
has the HostProtectionAttribute with the ExternalThreading
flag on. Which indicates ASP.NET that new threads will be involved, so ASP.NET starts keeping count of these operations waiting for completion.
If you need to use async methods in WebForms you should use the RegisterAsyncTask method.
Upvotes: 1