Skip to content

Conversation

Copy link

Copilot AI commented May 22, 2025

Problem

Applications published as msixbundles with trimming enabled (PublishTrimmed=true) crash on startup with the following exception:

System.TypeInitializationException: The type initializer for '<Module>' threw an exception.
 ---> System.MissingMethodException: Method not found: 'Void System.Environment.SetEnvironmentVariable(System.String, System.String)'.
   at Microsoft.Windows.Foundation.UndockedRegFreeWinRTCS.AutoInitialize.AccessWindowsAppSDK()

This happens because the Environment.SetEnvironmentVariable method is trimmed out during the publishing process when PublishTrimmed is set to true.

Solution

This PR implements two key improvements to fix the issue:

  1. Added a try-catch block around the Environment.SetEnvironmentVariable call to handle the MissingMethodException gracefully.
  2. Added a fallback mechanism using P/Invoke to call the Win32 SetEnvironmentVariableW function directly when the .NET method is not available.

The fallback approach ensures the environment variable gets set even in trimmed environments, allowing the application to start properly. This is important for PublishSingleFile support which relies on the MICROSOFT_WINDOWSAPPRUNTIME_BASE_DIRECTORY environment variable for SxS redirection.

Changes

  • Modified UndockedRegFreeWinRT-AutoInitializer.cs to catch the MissingMethodException
  • Added a P/Invoke declaration for the Win32 SetEnvironmentVariableW function
  • Implemented a fallback path to use the direct Win32 call when the .NET method fails

Fixes #11.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Unable to create a runnable msixbundle Fix MissingMethodException in UndockedRegFreeWinRT when creating msixbundles with trimming enabled May 22, 2025
Copilot AI requested a review from haonanttt May 22, 2025 08:37
Copilot finished work on behalf of haonanttt May 22, 2025 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to create a runnable msixbundle

2 participants