Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the UnobservedTaskException handler in dotnetcoreapp2.0

The following code, when run in a netcoreapp2.0 application, doesn't seem to end up throwing the UnobservedTaskException

using System;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp3 {

    class Program {

        public static void Main(string[] args) {

            TaskScheduler.UnobservedTaskException += (s, e) => {
                /// Try to crash the application - we wanna nail unobserved exceptions dead.
                /// Unfortunately, this code never seems to run.
                Console.WriteLine("UnobservedTaskException thrown.");
                throw e.Exception; 
            };

            var t = Task.Run(() => {
                throw new NotImplementedException();
            });

            while (!t.IsFaulted)
                Thread.Sleep(1);

            t = null;
            Console.WriteLine("Task is faulted.");

            GC.Collect();
            GC.WaitForPendingFinalizers();
            Console.ReadKey();
        }

    }
}

Here's the project file. How can I get the UnobservedTaskException handler to be fired?

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>

</Project>

In other stackoverflow posts, I've seen advice to use the following snippet in the project file, but it only works for .net framework 4.0+ projects. If there's an equivalent for the .netcore project file, that might be what I need to know.

<runtime> 
    <ThrowUnobservedTaskExceptions enabled="true"/> 
</runtime> 
like image 222
bboyle1234 Avatar asked Oct 20 '25 14:10

bboyle1234


1 Answers

I had the same issue. Just try it run in RELEASE mode. I tested it and it's working with console application .net core version 2.2.

internal class Program
{
    private static void Main(string[] args)
    {
        // REMEMBER TO RUN IN RELEASE MODE

        var handler = new EventHandler<UnobservedTaskExceptionEventArgs>(Unobserved);
        TaskScheduler.UnobservedTaskException += handler;

        Task.Run(() => { Console.WriteLine("task 1"); throw new Exception("TASK 1 EXCEPTION"); });
        Task.Run(() => { Console.WriteLine("task 2"); throw new Exception("TASK 2 EXCEPTION"); });

        Thread.Sleep(1000);

        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();

        Thread.Sleep(1000);

        Console.ReadKey();
    }

    private static void Unobserved(object o, UnobservedTaskExceptionEventArgs e)
    {
        e.SetObserved(); // optional
        foreach (var ex in e.Exception.InnerExceptions)
        {
            Console.WriteLine(ex.Message);
        }
    }
}
like image 121
abinkows Avatar answered Oct 23 '25 05:10

abinkows



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!