dotnet
dotnet copied to clipboard
Possible bug in [RelayCommand] async task
Discussed in https://github.com/CommunityToolkit/dotnet/discussions/355
Originally posted by DarthMazut July 26, 2022
Hello,
when I mark my command with [RelayCommand]
and use async Task
signature then when synchronouse part of code throws exception this exception is swallow and another criptic exception is thrown instead. Let me give you an excample:
In my view I bind Button's command to my VM command, like so:
<Button Command="{Binding CommandCommand}">Click Me</Button>
In my VM I do the following:
[RelayCommand]
private async Task Command()
{
throw new NotImplementedException();
await Task.Delay(100);
}
this simulates any method that has some async call, but before awaiting it, an exception has been thrown by prior part of code.
In such case, after pressing button I receive:
Unhandled exception at 0x76B757B1 (combase.dll) in TestApp1.exe: 0xC000027B: Wystąpił wewnętrzny wyjątek aplikacji (parameters: 0x16B993B0, 0x00000001).
and eventually this:
Exception thrown at 0x00DBE538 in TestApp1.exe: 0xC0000005: Access violation executing location 0x00DBE538.
This happens only when my method signature is async Task
, when changing it to async void
the proper exception is thrown.
My question is whether this is expected or it's a bug? Should I use async void
? I though async Task
is recommended?
This is obviously problematic because if I throw meaningfull exception from my code I'd like to see those instead of this Access violation
internal stuff.
@DarthMazut This is weird, and I'm not sure I see how such an exception could be caused by the MVVM Toolkit. Do you have a minimal repro? Is this on WinUI 3, by any chance? If so, what version of it?
Yes, I'm using WinUI 3. This happens in both of my projects, including the test one (which has only 1 Page + 1 VM) My setup is: Original project Application assembly
<PackageReference Include="CommunityToolkit.WinUI.UI" Version="7.1.2" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.1.3" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.1" />
ViewModels assembly
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0-preview4" />
Test project (to make sure it's not something specific to my code)
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0-preview4" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.1.3" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.25163-preview" />
Repro steps for me is:
- Create new WinUI Blank App, Packaged project
- Install
CommunityToolkit.Mvvm
- Create VM that implements
ObservableObject
- Mark
async Task
method with [RelayCommand] atribute, throw inside - In View setup
DataContext
to created VM, bind command to button - Run app (Package), click button, see strange exception
I suspect this is related to https://github.com/microsoft/microsoft-ui-xaml/issues/5221. @DarthMazut could you update the WASDK version you're using to Microsoft.WindowsAppSDK.1.2.221209.1
(or greater) and see if you can still repro?
Thank you! 🙂