Exception thrown when using CommunityToolkit and the cancellation token
See original GitHub issueDescribe the bug
I am trying to use the cancellation token as described in documentation here RelayCommand
[RelayCommand]
private async Task GreetUserAsync(CancellationToken token)
{
try
{
User user = await userService.GetCurrentUserAsync(token);
Console.WriteLine($"Hello {user.Name}!");
}
catch (OperationCanceledException)
{
}
}
However, whenever I add the cancellation token to the method - the program starts throwing the Object reference not set to an instance of an object
exception when invoking the command.
To Reproduce Steps to reproduce the behavior:
-
using .Net 6.0 Avalonia MVVM app on Windows 10 x64,
CommunityToolkit.Mvvm v.8.1.0
, rest of the information along with full repro code you will find in a repo https://github.com/pavlexander/avaloniaExceptionTest1 -
Create fresh
Avalogia
app, add the community toolkitnuget
package -
Add a new dummy window
-
add 2 buttons to the main widnow.
<StackPanel>
<Button Command="{Binding OpenNewWindowWithToken}">Open new window (with CancellationToken)</Button>
<Button Command="{Binding OpenNewWindow}">Open new window</Button>
</StackPanel>
- create 2 commands in the main view model, one with cancellation token, one without
[RelayCommand]
public async Task OpenNewWindowWithToken(CancellationToken token)
{
var newWindow = new Window1();
newWindow.Show();
}
[RelayCommand]
public async Task OpenNewWindow()
{
var newWindow = new Window1();
newWindow.Show();
}
- Launch the app. First click on the button that uses command without cancellation token. Works Ok.
- Now click on the button that has cancellation token as an input parameter. Observe exception.
Stacktrace:
at Avalonia.Controls.Button.OnClick()\r\n at Avalonia.Controls.Button.OnPointerReleased(PointerReleasedEventArgs e)\r\n at System.Reactive.Subjects.Subject`1.OnNext(T value)\r\n at Avalonia.Interactivity.EventRoute.RaiseEventImpl(RoutedEventArgs e)\r\n at Avalonia.Interactivity.EventRoute.RaiseEvent(IInteractive source, RoutedEventArgs e)\r\n at Avalonia.Interactivity.Interactive.RaiseEvent(RoutedEventArgs e)\r\n at Avalonia.Input.MouseDevice.MouseUp(IMouseDevice device, UInt64 timestamp, IInputRoot root, Point p, PointerPointProperties props, KeyModifiers inputModifiers)\r\n at Avalonia.Input.MouseDevice.ProcessRawEvent(RawPointerEventArgs e)\r\n at Avalonia.Input.InputManager.ProcessInput(RawInputEventArgs e)\r\n at Avalonia.Win32.WindowImpl.AppWndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)\r\n at Avalonia.Win32.WindowImpl.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)\r\n at Avalonia.Win32.Interop.UnmanagedMethods.DispatchMessage(MSG& lpmsg)\r\n at Avalonia.Win32.Win32Platform.RunLoop(CancellationToken cancellationToken)\r\n at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken)\r\n at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args)\r\n at AvaloniaApplication2.Program.Main(String[] args) in C:\repos\avaloniaExceptionTest1\AvaloniaApplication2\Program.cs:line 14
Expected behavior I expect the method to work, no exceptions to be thrown, as documented.
Screenshots
Desktop (please complete the following information):
- OS: Windows 10 x64
- Avalonia Version=“0.10.18”
- Avalonia.Desktop Version=“0.10.18”
- Microsoft Visual Studio Community 2022 (64-bit) - Current Version 17.4.3
- CommunityToolkit.Mvvm v.8.1.0
Additional context I have originally reported this bug to CommunityToolkit github page https://github.com/CommunityToolkit/dotnet/issues/579, however, from the response of the toolkit maintainer I understood that the problem is within the Avalonia itself, rather than the toolkit… Hence this issue is essentialy a duplicate of what I have reported earlier.
The code that reproduces the problem is located in repo https://github.com/pavlexander/avaloniaExceptionTest1
Please let me know if some additional input is needed.
Issue Analytics
- State:
- Created 8 months ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
Here is a minimal repro, without the MVVM Toolkit at all:
If you change that
Func<CancellationToken, Task>
toFunc<Task>
and adjust the method signature accordingly, the issue goes away. No idea why that parameter is causing the issue, but hopefully this helps the investigation in Avalonia 🙂I will close this issue, and open another more specific https://github.com/AvaloniaUI/Avalonia/issues/10075