FlowExceptionsToTaskScheduler not bubbling up to TaskScheduler.UnobservedTaskException
I'm trying to use FlowExceptionsToTaskScheduler within RelayCommand, but it doesn't seem to be working.
Early in my app I have this:
...stuff...
TaskScheduler.UnobservedTaskException += HandleUnobservedTaskException;
}
private void HandleUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
{
ExceptionHandler.LogException(e.Exception);
}
Then I have in a view model:
[RelayCommand(FlowExceptionsToTaskScheduler = true)]
private async Task Refresh()
{
_ = await DataManager.RefreshOrderHistoryAsync();
IsRefreshing = false;
}
For my testing I'm intentionally throwing an exception within DataManager.RefreshOrderHistoryAsync, but that seems to be silently swallowed somewhere and HandleUnobservedTaskException is never called.
Should this scenario work, or am I doing something wrong?
FYI @Sergio0694
@Qythyx that handler is not called immediately when the exception is thrown, but rather it's invoked by the Task finalizer. It's possible you're not seeing that just because a GC hasn't triggered just yet and that Task is still alive. Try forcing a GC collect and check whether that does work? We're using that option in several cases and I've never had any issues with it 🤔
@Sergio0694 , thanks for the suggestion, but I tried forcing a GC and still didn't see my handler get called.
Would you be able to provide a minimal repro for this in a standalone .NET console app? Basically a unit test we can also use to check. Because we do have some and they're passing 🤔