Unable to execute Unit Tests in Release mode (.NET Native) when Microsoft.UI.Xaml is used
See original GitHub issueDescribe the bug
When I execute the unit tests project in Visual Studio (F5 or with the Tests Explorer), a fatal failure occurs at startup. I encounter the same issue with VSTest in my Azure DevOps pipeline.
Steps to reproduce the bug
Steps to reproduce the behavior:
- Open the repro project
- Select ‘Release’ configuration and ‘x64’ platform
- Make sure .NET Native tool chain is enabled in project settings
- Select the tests project as the startup project in the list
- Click on “Start on local machine” or “Run” in Tests Explorer
See the application crash or the ‘Tests’ output to see the error.
Expected behavior
at System.Runtime.InteropServices.McgMarshal.ThrowOnExternalCallFailed(Int32 hr, RuntimeTypeHandle typeHnd) in f:\dd\ndp\fxcore\CoreRT\src\System.Private.Interop\src\Shared\McgMarshal.cs:line 1189 at __Interop.ComCallHelpers.Call(__ComObject __this, RuntimeTypeHandle __typeHnd, Int32 __targetIndex, IntPtr arg0, Void* arg1, Void* arg2) at __Interop.ForwardComStubs.Stub_5[TThis](__ComObject __this, IntPtr baseInterface, IntPtr& innerInterface, Int32 __targetIndex) at Windows.UI.Xaml.Application.__Factory_Windows_UI_Xaml__IApplicationFactory_CreateInstance(String typeName) at Windows.UI.Xaml.Application..ctor() at UTReproUWPApp.App..ctor() in C:\Users\chris\source\repos\UTReproUWPApp\UTReproUWPApp\App.xaml.cs:line 29 at UnitTestProject1.UnitTestProject1_XamlTypeInfo.XamlTypeInfoProvider.get_OtherProviders() in C:\Users\chris\source\repos\UTReproUWPApp\UnitTestProject1\obj\x64\Release\XamlTypeInfo.g.cs:line 367 at UnitTestProject1.UnitTestProject1_XamlTypeInfo.XamlTypeInfoProvider.CheckOtherMetadataProvidersForName(String typeName) in C:\Users\chris\source\repos\UTReproUWPApp\UnitTestProject1\obj\x64\Release\XamlTypeInfo.g.cs:line 381 at UnitTestProject1.UnitTestProject1_XamlTypeInfo.XamlTypeInfoProvider.GetXamlTypeByName(String typeName) in C:\Users\chris\source\repos\UTReproUWPApp\UnitTestProject1\obj\x64\Release\XamlTypeInfo.g.cs:line 172 at UnitTestProject1.App.GetXamlType(String fullName) in C:\Users\chris\source\repos\UTReproUWPApp\UnitTestProject1\obj\x64\Release\XamlTypeInfo.g.cs:line 49 at __Interop.ReverseComStubs.Stub_6[TResult](Object __this, HSTRING unsafe_fullName, Void** unsafe_result__retval, IntPtr __methodPtr)
Screenshots
N/A
Version Info
NuGet package version: [Microsoft.UI.Xaml 2.4.2]
Windows 10 version | Saw the problem? |
---|---|
Insider Build (19628) | Yes |
November 2019 Update (18363) | Yes |
May 2019 Update (18362) | Yes |
October 2018 Update (17763) | |
April 2018 Update (17134) | |
Fall Creators Update (16299) | |
Creators Update (15063) |
Device form factor | Saw the problem? |
---|---|
Desktop | Yes |
Mobile | |
Xbox | |
Surface Hub | |
IoT |
Additional context
I have attempt to use some ‘workaround’ that I found in other similar issues without success #539/#230 (commented in the repro project).
This issue does not allow me to use the library because the unit tests are not optional.
Thank you!
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (5 by maintainers)
Top GitHub Comments
@ChristopheLav I was able to reproduce the problem. I don’t fully understand the root cause of the problem, but there is a work-around that I was able to apply. Adding a RevealBrush (e.g. RevealBorderBrush) to a xaml file in the UnitTestProject is sufficient to fix the metadata problem. For example, in UnitTestApp.xaml:
Can you try out that work-around and see if that unblocks you?
It’s a pretty arbitrary and magical work-around though, so we should definitely try to fix the root issue.
@RealTommyKlein would you be able to take a look here? It seems to be something to do with custom Dependency Property resolution when using .net native.
The callstack of the crash looks like this:
What is happening is that we create an instance of XamlControlsResources from App.xaml. XamlControlsResources…ctor calls MUXControlsFactory::EnsureInitialized(), which registers the RevealBrush DPs. Then accessing a DP on XamlControlsResources (UseCompactResources), causes the runtime to hit MetadataAPI::ProcessRegistrations which then crashes when processing the registration for RevealBrush. Adding a dummy RevealBrush to the app’s .xaml was sufficient to avoid this crash, but that should not be necessary.
I’m glad you found a workaround to this issue. I’m going to close this out, as there is a very low probability of this issue ever being fixed since WinUI3 is moving towards .NET5. If you feel like this is in error, please reply, thanks!