question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to execute Unit Tests in Release mode (.NET Native) when Microsoft.UI.Xaml is used

See original GitHub issue

Describe 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:

  1. Open the repro project
  2. Select ‘Release’ configuration and ‘x64’ platform
  3. Make sure .NET Native tool chain is enabled in project settings
  4. Select the tests project as the startup project in the list
  5. Click on “Start on local machine” or “Run” in Tests Explorer

See the application crash or the ‘Tests’ output to see the error.

UTReproUWPApp.zip

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:closed
  • Created 3 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kmahonecommented, May 28, 2020

@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:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary>
        <RevealBorderBrush x:Key="myrevealbrush" xmlns="using:Microsoft.UI.Xaml.Media" />
    </ResourceDictionary>
    <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
</ResourceDictionary.MergedDictionaries>

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:

Windows.UI.Xaml.dll!OnFailureEncountered(HRESULT failedFrameHR, void * directCallerReturnAddress, _CONTEXT * contextRecord)
Windows.UI.Xaml.dll!OnFailure<1319>(HRESULT failedFrameHR)
Windows.UI.Xaml.dll!ctl::ComObjectBase::CreateInstanceBase(ctl::ComBase * pNewInstance, unsigned int)
Windows.UI.Xaml.dll!ctl::ComObject<DirectUI::FrameworkApplication>::CreateInstance<DirectUI::FrameworkApplication>(IInspectable * pOuter, DirectUI::FrameworkApplication * * ppNewInstance, unsigned int)
Windows.UI.Xaml.dll!ctl::AggregableActivationFactory<DirectUI::FrameworkApplication>::ActivateInstanceStatic(IInspectable * pOuter, IInspectable * * instance, bool)
Windows.UI.Xaml.dll!ctl::ValidateFactoryCreateInstanceWithAggregableActivationFactory<DirectUI::FrameworkApplication,Windows::UI::Xaml::IApplication>(IInspectable * const pOuter, IInspectable * * const ppInner, IUnknown * * ppInstance, KnownTypeIndex)
Windows.UI.Xaml.dll!DirectUI::FrameworkApplicationFactory::CreateInstance(IInspectable * pOuter, IInspectable * * ppInner, Windows::UI::Xaml::IApplication * * ppInstance)
[Managed to Native Transition]	
UnitTestProject1.McgInterop.dll!__Interop.ComCallHelpers.Call(System.__ComObject __this, System.RuntimeTypeHandle __typeHnd, int __targetIndex, System.IntPtr arg0, void* arg1, void* arg2)	C#
UnitTestProject1.McgInterop.dll!__Interop.ForwardComStubs.Stub_5<System.__Canon>(System.__ComObject __this, System.IntPtr baseInterface, out System.IntPtr innerInterface, int __targetIndex)	C#
UnitTestProject1.McgInterop.dll!Windows.UI.Xaml.Application.__Factory_Windows_UI_Xaml__IApplicationFactory_CreateInstance(string typeName)	C#
UnitTestProject1.McgInterop.dll!Windows.UI.Xaml.Application.Application()	C#
UTReproUWPApp.dll!UTReproUWPApp.App.App()
UnitTestProject1.exe!UnitTestProject1.UnitTestProject1_XamlTypeInfo.XamlTypeInfoProvider.OtherProviders.get()
UnitTestProject1.exe!UnitTestProject1.UnitTestProject1_XamlTypeInfo.XamlTypeInfoProvider.CheckOtherMetadataProvidersForName(string typeName)
UnitTestProject1.exe!UnitTestProject1.UnitTestProject1_XamlTypeInfo.XamlTypeInfoProvider.GetXamlTypeByName(string typeName)
UnitTestProject1.exe!UnitTestProject1.App.GetXamlType(string fullName)
UnitTestProject1.McgInterop.dll!__Interop.ReverseComStubs.Stub_6<System.__Canon>(object __this, System.Runtime.InteropServices.HSTRING unsafe_fullName, void** unsafe_result__retval, System.IntPtr __methodPtr)	C#
UnitTestProject1.McgInterop.dll!Windows.UI.Xaml.Markup.IXamlMetadataProvider__Impl.Vtbl.GetXamlTypeByFullName__n(System.IntPtr pComThis, System.Runtime.InteropServices.HSTRING unsafe_fullName, void** unsafe_result__retval)	C#
[Native to Managed Transition]	
Windows.UI.Xaml.dll!Windows::UI::Xaml::Phone_XamlTypeInfo::XamlLibMetadataProvider::GetXamlTypeByFullName(HSTRING__ * fullName, Windows::UI::Xaml::Markup::IXamlType * * value)
Windows.UI.Xaml.dll!DirectUI::MetadataAPI::ImportClassInfoFromMetadataProvider(const xstring_ptr_view & strTypeFullName, const CClassInfo * * ppType)
Windows.UI.Xaml.dll!DirectUI::MetadataAPI::GetClassInfoByTypeName(Windows::UI::Xaml::Interop::TypeName typeName, const CClassInfo * * ppType)
Windows.UI.Xaml.dll!DirectUI::MetadataAPI::ProcessRegistrations()
Windows.UI.Xaml.dll!DirectUI::MetadataAPI::EnsureDependencyPropertyInitialized(const CDependencyProperty * prop)
[Inline Frame] Windows.UI.Xaml.dll!CCustomDependencyProperty::EnsureTypesResolved()
[Inline Frame] Windows.UI.Xaml.dll!DirectUI::DependencyPropertyHandle::GetDP()
Windows.UI.Xaml.dll!DirectUI::DependencyObject::GetValue(Windows::UI::Xaml::IDependencyProperty * pDP, IInspectable * * ppValue)
Microsoft.UI.Xaml.dll!winrt::impl::consume_Windows_UI_Xaml_IDependencyObject<XamlControlsResources>::GetValue(const winrt::Windows::UI::Xaml::DependencyProperty & dp)
Microsoft.UI.Xaml.dll!XamlControlsResourcesProperties::UseCompactResources()
Microsoft.UI.Xaml.dll!XamlControlsResources::UpdateSource()
[Inline Frame] Microsoft.UI.Xaml.dll!XamlControlsResources::{ctor}()
Microsoft.UI.Xaml.dll!winrt::impl::heap_implements<XamlControlsResources>::heap_implements<XamlControlsResources>()	C++
[Inline Frame] Microsoft.UI.Xaml.dll!winrt::make()
[Inline Frame] Microsoft.UI.Xaml.dll!winrt::Microsoft::UI::Xaml::Controls::factory_implementation::XamlControlsResourcesT<winrt::Microsoft::UI::Xaml::Controls::XamlControlsResourcesFactory,XamlControlsResources>::ActivateInstance()
Microsoft.UI.Xaml.dll!winrt::impl::produce<winrt::Microsoft::UI::Xaml::Controls::XamlControlsResourcesFactory,winrt::Windows::Foundation::IActivationFactory>::ActivateInstance(void * * instance)
[Managed to Native Transition]	
System.Private.Interop.dll!System.Runtime.InteropServices.McgMarshal.ActivateInstance(string typeName)
UnitTestProject1.McgInterop.dll!Microsoft.UI.Xaml.Controls.XamlControlsResources.XamlControlsResources()	C#
UnitTestProject1.exe!UnitTestProject1.UnitTestProject1_XamlTypeInfo.XamlTypeInfoProvider.Activate_0_XamlControlsResources()
UTReproUWPApp.dll!UTReproUWPApp.UTReproUWPApp_XamlTypeInfo.XamlUserType.ActivateInstance()
UnitTestProject1.McgInterop.dll!__Interop.ReverseComStubs.Stub_10(object __this, void** unsafe_result__retval, System.IntPtr __methodPtr)	C#
UnitTestProject1.McgInterop.dll!Windows.UI.Xaml.Markup.IXamlType__Impl.Vtbl.ActivateInstance__n(System.IntPtr pComThis, void** unsafe_result__retval)	C#
[Native to Managed Transition]	
Windows.UI.Xaml.dll!DirectUI::ActivationAPI::ActivateInstance(const CClassInfo * pType, IInspectable * pOuter, IInspectable * * ppInstance)
[Inline Frame] Windows.UI.Xaml.dll!DirectUI::ActivationAPI::ActivateInstance(const CClassInfo *)
Windows.UI.Xaml.dll!DirectUI::XamlParserCallbacks::XamlManagedRuntimeRPInvokes_CreateInstance(XamlTypeToken tokType, XamlQualifiedObject * pqoInstance)
[Inline Frame] Windows.UI.Xaml.dll!CFxCallbacks::XamlManagedRuntimeRPInvokes_CreateInstance(XamlTypeToken)
Windows.UI.Xaml.dll!XamlManagedRuntime::CreateInstance(const XamlTypeToken & inXamlType, std::shared_ptr<XamlQualifiedObject> & returnQo)
[Inline Frame] Windows.UI.Xaml.dll!XamlType::CreateInstance(std::shared_ptr<XamlQualifiedObject> &)
Windows.UI.Xaml.dll!ObjectWriterRuntime::CreateTypeImpl(const XamlLineInfo & lineInfo, const std::shared_ptr<XamlType> & spType, const std::shared_ptr<ObjectWriterCallbacksDelegate> & spCallback, const std::shared_ptr<XamlQualifiedObject> & spRootObjectInstance, std::shared_ptr<XamlQualifiedObject> & spInstance)
[Inline Frame] Windows.UI.Xaml.dll!ObjectWriterCommonRuntime::CreateType(const XamlLineInfo &)
Windows.UI.Xaml.dll!BinaryFormatObjectWriter::CreateInstanceFromType(const ObjectWriterNode & node)
Windows.UI.Xaml.dll!BinaryFormatObjectWriter::WriteNode(const ObjectWriterNode & inNode)
Windows.UI.Xaml.dll!CParser::LoadXamlCore(CCoreServices * pCore, const CParserSettings & parserSettings, const Parser::XamlBuffer & buffer, CDependencyObject * * ppDependencyObject, const xstring_ptr_view & strSourceAssemblyName, const std::array<unsigned char,64> &)
[Inline Frame] Windows.UI.Xaml.dll!CParser::LoadXaml(CCoreServices * ppDependencyObject, const CParserSettings &)
Windows.UI.Xaml.dll!CCoreServices::ParseXamlWithExistingFrameworkRoot(const Parser::XamlBuffer & buffer, CDependencyObject * pExistingFrameworkRoot, const xstring_ptr_view & strSourceAssemblyName, const xstring_ptr_view & strXamlResourceUri, const bool expandTemplatesDuringParse, CDependencyObject * * ppDependencyObject)
Windows.UI.Xaml.dll!CApplication::LoadComponent(CCoreServices * pCore, CDependencyObject * pComponent, IPALUri * pUri)
Windows.UI.Xaml.dll!Application_LoadComponent(CCoreServices * pCore, CDependencyObject * pComponent, const xstring_ptr & strUri, ComponentResourceLocation::Enum resourceLocation)
Windows.UI.Xaml.dll!DirectUI::FrameworkApplication::LoadComponent(IInspectable * pComponent, const xstring_ptr & strUri, Windows::UI::Xaml::Controls::Primitives::ComponentResourceLocation resourceLocation)
Windows.UI.Xaml.dll!DirectUI::DXamlCore::EnsureCoreApplicationInitialized()
Windows.UI.Xaml.dll!DirectUI::FrameworkView::Initialize(Windows::ApplicationModel::Core::ICoreApplicationView * pCoreApplicationView)
twinapi.appcore.dll!Windows::ApplicationModel::Core::CoreApplicationView::CreateAndInitializeFrameworkView(Microsoft::WRL::ComPtr<Windows::ApplicationModel::Core::IFrameworkViewSource> & frameworkViewSource)
twinapi.appcore.dll!Windows::ApplicationModel::Core::CoreApplicationViewAgileContainer::InitializeView()
[Inline Frame] twinapi.appcore.dll!Windows::ApplicationModel::Core::CoreApplicationViewAgileContainer::RuntimeClassInitialize::__l2::unsigned long <lambda>(void *)::__l2::<lambda_ea98b257b67097ec7d8a3cb81255f6a2>::operator()()
twinapi.appcore.dll!Windows::ApplicationModel::Core::CoreApplicationViewAgileContainer::RuntimeClassInitialize::__l2::<lambda>(void * pv)
SHCore.dll!_WrapperThreadProc(void * pv)
kernel32.dll!BaseThreadInitThunk(unsigned long RunProcessInit, long(*)(void *) StartAddress, void * Argument)
ntdll.dll!RtlUserThreadStart(long(*)(void *) StartAddress, void * Argument)

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.

0reactions
stevenbrixcommented, Nov 19, 2020

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to compile UWP with .Net Native in release mode
I setup by default.rd.xml like this, I thought some of these error ( error : Type 'Windows.UI.Xaml.Duration', Windows.
Read more >
Run unit tests with Test Explorer - Visual Studio (Windows)
Use Test Explorer to run unit tests from Visual Studio or ... Native C/C++ code projects must be tested by using a C++...
Read more >
NavigationView - Windows apps
NavigationView is an adaptive control that implements top-level navigation patterns for your app.
Read more >
Configure and use Live Unit Testing - Visual Studio
Using persisted data allows Live Unit Testing to offer superior performance while running your tests dynamically in response to code changes.
Read more >
Live Unit Testing Preview: Better and Faster - Visual Studio ...
Live Unit Testing will help you catch if new code changes cause tests to fail as you develop so you won't forget to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found