I wonder why exception handling behaves differently in asp.net and console for async/await calls. Consider this method:
private static async Task Test()
{
try
{
await Task.Run(() =>
{
throw new Exception("Test Exception");
});
}
catch (Exception)
{
throw;
}
}
Catch does not hit in ASP.NET non-async controller like this working under IIS, neither in Test() nor in TestAction():
[HttpGet]
public virtual ActionResult TestAction()
{
try
{
Test().Wait();
}
catch (Exception)
{
throw;
}
}
Instead http request hangs. Same Console code or WindowsForms code work fine hitting catch in both places:
class Program
{
static void Main(string[] args)
{
try
{
Test().Wait();
}
catch (Exception)
{
throw;
}
}
}
UPDATE:
This code work fine though (catch is called) in asp.net controller:
[HttpGet]
public virtual ActionResult TestAction()
{
try
{
Task.Run(() =>
{
throw new Exception("Test Exception");
}).Wait();
}
catch (Exception)
{
throw;
}
}