CsWinRT: WinRT bug: System.InvalidOperationException:“Attempt to update previously set global instance.”

Description

When calling new PackageManager(), this error throw. Only the first time calling the constructor throws the error.

Configuration

Using .Net 5 with TargetFramework net5.0-windows10.0.19041.0. The windows OS version is 20H2 x64.

Regression?

Not sure, but .net core 3.1 with the Windows.SDK.Contract works well.

Other information

StackTrace

   at System.Runtime.InteropServices.ComWrappers.RegisterForTrackerSupport(ComWrappers instance)
   at WinRT.ComWrappersSupport.get_ComWrappers()
   at WinRT.ComWrappersSupport.TryRegisterObjectForInterface(Object obj, IntPtr thisPtr)
   at WinRT.ComWrappersSupport.RegisterObjectForInterface(Object obj, IntPtr thisPtr)
   at Windows.Management.Deployment.PackageManager..ctor()
   at Flow.Launcher.Plugin.Program.Programs.UWP.CurrentUserPackages() in D:\Document\Dotnet\Flow.Launcher\Plugins\Flow.Launcher.Plugin.Program\Programs\UWP.cs:line 200
   at Flow.Launcher.Plugin.Program.Programs.UWP.All() in D:\Document\Dotnet\Flow.Launcher\Plugins\Flow.Launcher.Plugin.Program\Programs\UWP.cs:line 156

Similar bug

https://github.com/microsoft/CsWinRT/issues/394

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

per discussion, will add a check in ComWrappers set accessor for overlapping sets, and ignore subsequent ones that try to set the DefaultComWrappers (which should be benign). Along with this, will move the InitializeComWrappers default check into the set accessor, which, along with the get accessor, will then reference a singleton DefaultComWrappers.Instance property without requiring additional locking. DefaultComWrappers will also need a private ctor to force accesses to the Instance singleton.