I was going through the following article ThrowUnobservedTaskExceptions to check the behavior of ThrowUnobservedTaskExceptions setting when the unobserved exceptions from the Tasks are thrown from the application.
I was able to simulate the application/process crash with the following setting in a Console app:
<ThrowUnobservedTaskExceptions enabled="true"/>
Here is the console app code:
Program.cs:
using System;
using System.Threading;
using System.Collections.Generic;
using System.Threading.Tasks;
//Use the following config settings to enable the throwing of unobserved exceptions.
// <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
// <ThrowUnobservedTaskExceptions enabled="true"/>
public class Example
{
static void Main()
{
Task.Run(() => { throw new InvalidOperationException("test"); });
while (true)
{
Thread.Sleep(100);
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<runtime>
<ThrowUnobservedTaskExceptions enabled="true"/>
</runtime>
</configuration>
On running the above code I can see the correct behavior with ThrowUnobservedTaskExceptions as true i.e to kill the process for unobserved task exceptions. I was able to get the following screen after running the application which matches the documentation.
The problem I am facing is to simulate the exact behavior in an ASP.NET application. I am having an ASP.NET application running targetFramework="4.8". I am not able to simulate the same behavior in my ASP.NET application i.e the process/application should be killed on facing unobserved task exception with <ThrowUnobservedTaskExceptions enabled="true"/> setting.
Here is the simple API code in controller of the application:
[Route("api/ping"), HttpGet]
public IHttpActionResult TestPing()
{
Task.Run(() => { throw new InvalidOperationException("test"); });
while (true)
{
Thread.Sleep(100);
GC.Collect();
GC.WaitForPendingFinalizers();
}
return Ok("Pong");
}
Here is the screenshot confirming that I am using <ThrowUnobservedTaskExceptions enabled="true"/> setting in the web config of that application:
When I run the above endpoint in the browser: https://localhost:44364/api/ping I am no longer seeing the ASP.NET process being killed.
I am seeing no change in my ASP.NET application for tasks unobserved exceptions whether I keep ThrowUnobservedTaskExceptions as true or as false. I am already running the code in release mode. There is no handler attached for System.Threading.Tasks.TaskScheduler.UnobservedTaskException in my ASP.NET application start up. I can also confirm that the public IHttpActionResult TestPing() method is being hit by calling the API and the proof is that https://localhost:44364/api/ping is being stuck in a loading state because while (true) in that code but the process is not crashing.
Can you share why the above behavior for ASP.NET application is different than a Console app for ThrowUnobservedTaskExceptions setting?
My goal is to simulate the crash of an ASP.NET application on unobserved tasks exception. Can you share a sample piece of code to achieve my goal?

