CommunityToolkit / Microsoft.Toolkit.Win32

ARCHIVE - This repository contained XAML Islands wrapper controls and tooling for XAML Islands with WinUI 2, see readme for more info about XAML Islands with WinUI 3 and the WindowsAppSDK.
https://aka.ms/windowsappsdk
Other
383 stars 89 forks source link

System.TypeInitializationException when using Attached Properties with XAML Islands #275

Closed mayankkrjain closed 4 years ago

mayankkrjain commented 4 years ago

Describe the bug

I have a C# UI library which is a Windows Runtime Component. It has some CustomControls. I have defined a few attached properties which are being used across these controls. I am using this UI library in a separate C# class-library which is then being used in a Winforms Application. I have also created a packaging project for the same.

When I launch the packaging project in VS, I observe the below-mentioned exception in the output window. Sometimes it may work for couple of launches even with the exception logs. After that, it stops launching and simply crashes and never works which is very weird. The crash happens only If I define an attached property on it. Without it, the control works fine.

This works fine if I simply launch the Winforms application from VS instead of using the WAP. This also works fine if the same setup is consumed via a UWP application instead of a Winforms app. What am I missing here? Is there an issue with the way I have defined the properties? Or do I need to do something extra with the WAP?

I have modified the Winforms XAML-Island sample with my use-case. It reproduces the issue. It can take a couple of launches. You can see the commit here.

Steps to Reproduce

Steps to reproduce the behavior:

  1. Go to Xaml-Islands Sample
  2. Clone the master branch and open the Winforms sample.
  3. Set WAP as the startup project.
  4. Launch and see an error

Expected behavior

The exception should not occur

Crash Log

Exception thrown at 0x00007FFF009FA799 (KernelBase.dll) in WinForms_XAMLIslands_v1.exe: WinRT originate error - 0x80131534 : 'System.TypeInitializationException: The type initializer for 'UILibrary.Controls.AttachedProperties' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified.
   at UILibrary.Controls.AttachedProperties..cctor()
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.RuntimeHelpers._Ru'.
onecore\com\combase\winrt\error\restrictederror.cpp(1014)\combase.dll!00007FFF027A35B0: (caller: 00007FFF0278CAEC) ReturnHr(1) tid(8690) 8007007E The specified module could not be found.
'WinForms_XAMLIslands_v1.exe' (Win32): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.2\System.Resources.ResourceManager.dll'. 
'WinForms_XAMLIslands_v1.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.2\System.Resources.ResourceManager.dll'. 
Exception thrown: 'Windows.UI.Xaml.Markup.XamlParseException' in MyClassLibrary.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in System.Private.CoreLib.dll
An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in System.Private.CoreLib.dll
Exception has been thrown by the target of an invocation.

Environment

NuGet Package(s): 

Package Version(s): 

Project .NET Version:
- [ ] .NET Framework (version: )
- [x] .NET Core 3
- [ ] .NET Core 3.1 Preview (version: )

Windows 10 Build Number:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [x] May 2019 Update (18362)
- [ ] Insider Build (build number: )

App min and target version:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [x] May 2019 Update (18362)
- [ ] Insider Build (xxxxx)

Device form factor:
- [x] Desktop
- [ ] Xbox
- [ ] Surface Hub
- [ ] IoT

Visual Studio 
- [ ] 2017 (version: )
- [ ] 2019 (version: ) 
- [x] 2019 Preview (version: 16.7.0 Preview 2.0)

Additional context

I am also seeing the following compilation failures frequently. I have to rebuild to fix this issue. .nuget\packages\microsoft.net.uwpcoreruntimesdk\2.2.9\tools\CoreRuntime\Microsoft.Net.CoreRuntime.targets(238,5): error : CopyWin32Resources failed with exit code 500

ghost commented 4 years ago

Hello mayankkrjain, thank you for opening an issue with us!

I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌

mayankkrjain commented 4 years ago

I think the issue is not just related to AttachedProperties. It affects all the use-cases where we are defining just a class (either based on DepenendencyObjct or on XamlCompositionBrushBase) and using it in XAML. I feel I am missing an extra step here that needs to be done to make the WAP project aware of such definitions.

michael-hawker commented 4 years ago

FYI @marb2000 @ocalvo, any thoughts here?

ocalvo commented 4 years ago

FYI @marb2000 @ocalvo, any thoughts here?

This may have been fixed by in 6.1.1

@mayankkrjain, could you upgrade to version 6.1.1 and see if the issue still repro?

mayankkrjain commented 4 years ago

@mayankkrjain, could you upgrade to version 6.1.1 and see if the issue still repro?

I did that here. Unfortunately, I am still seeing the crash.

marb2000 commented 4 years ago

@ocalvo I tested it and this is still failing. Any idea why?

ocalvo commented 4 years ago

@ocalvo I tested it and this is still failing. Any idea why?

Yes, this is an issue with .Net 3. The fix is expected in version 3.1.7 schedule to release this month.

marb2000 commented 4 years ago

Waiting for the update of .NET Core 3.1.7.

ocalvo commented 4 years ago

I have verified that this issue is now fixed in the release version of .NET Core 3.1.7.

mayankkrjain commented 4 years ago

@ocalvo - I installed the release version. I am still seeing a crash in Xaml-Islands Sample

Below is the output for dotnet --info

.NET Core SDK (reflecting any global.json): Version: 3.1.401 Commit: 39d17847db

Runtime Environment: OS Name: Windows OS Version: 10.0.19041 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\3.1.401\

Host (useful for support): Version: 3.1.7 Commit: fcfdef8d6b

Am I missing something here?

Crash log -

Exception thrown at 0x00007FFB87103E49 (KernelBase.dll) in WinForms_XAMLIslands_v1.exe: WinRT originate error - 0x80131534 : 'System.TypeInitializationException: The type initializer for 'UILibrary.Controls.AttachedProperties' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified. at UILibrary.Controls.AttachedProperties..cctor() --- End of inner exception stack trace --- at System.Runtime.CompilerServices.RuntimeHelpers._Ru'. onecore\com\combase\winrt\error\restrictederror.cpp(1016)\combase.dll!00007FFB8879E5C3: (caller: 00007FFB887A2A91) ReturnHr(1) tid(3364) 8007007E The specified module could not be found. 'WinForms_XAMLIslands_v1.exe' (Win32): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.7\System.Resources.ResourceManager.dll'. 'WinForms_XAMLIslands_v1.exe' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.7\System.Resources.ResourceManager.dll'. Exception thrown: 'Windows.UI.Xaml.Markup.XamlParseException' in MyClassLibrary.dll Exception thrown: 'System.Reflection.TargetInvocationException' in System.Private.CoreLib.dll An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in System.Private.CoreLib.dll

ocalvo commented 4 years ago

This specific error was fixed in version 6.1.2 of the win32 toolkit. What version are you using?

mayankkrjain commented 4 years ago

This issue seems to be fixed in the sample that I had created and shared above.

However, I am still seeing a crash in another project of mine. I will try to create a similar shareable sample and post the update here. Trying to get the exact crash logs. This crash could be new or the same. BUt controls without attached props are working fine.

mayankkrjain commented 4 years ago

@ocalvo @marb2000 -I am closing this issue now as this one is definitely fixed. I am still facing a different issue. Once I am able to figure out the exact cause, will reach out to you again via a different ticket.