UWP app throws unhandled exception when installed from .msix package

WoodManEXP 80 Reputation points

This is a UWP app with Visual Studio Insiders [11709.129]. When deployed from Visual Studio, started with or without debugging to a Windows 10, an Xbox, or a Windows 11, it runs fine and is passing the tests.

However, when the project is published to an App Package then the generated .msix file is taken to a Windows 11 machine and installed, the app fails. It shows, by attaching remote debugger to the installed App Package, that an unhandled exception (Windows.UI.Xaml.dll) is being thrown.

The app does quite a bit of work before the exception is thrown. It seems like the exception may be related to JSON serialization taking place that then writes a file to Windows.Storage.ApplicationData.Current.LocalFolder. But cannot determine that for sure.

The stack trace from the exception is not so helpful. Any thoughts about how to debug this? Can this exception be caught to try to get an idea of the cause?

As follow on let me ask about remote debugging from an .msix install. The remote debugger can attach to the Installed App Package, but there are no debug symbols over there for setting breakpoints and such. It might be easier to debug this if there were a way to create the debug info for the Installed App Package were available to the debugger. Is such a thing possible?

 TY!

0 comments No comments

Sign in to comment

Answer accepted by question author

Jay Pham (WICLOUD CORPORATION) 3,800 Reputation points β€’ Microsoft External Staff β€’ Moderator

Hi @WoodManEXP ,

I understand how frustrating this issue is. Since the app works in F5 debugging but crashes after MSIX installation, the most likely causes are:

  • .NET Native compilation stripping reflection metadata (common with JSON serialization in Release builds)
  • Synchronous file I/O (.Result / .Wait()) causing crashes inside the AppContainer sandbox

You can verify this by:

  • Switch to Release mode
  • Enable .NET Native toolchain
  • Run the app locally with F5

If it crashes, it is likely a reflection metadata issue.

For Newtonsoft.Json, add metadata preservation in Default.rd.xml. Also ensure all file operations use async/await only.

To capture the exact exception:

  • Enable CLR Exceptions in Exception Settings
  • Use Debug Installed App Package in Visual Studio

Please try the Release + .NET Native test and share any exception details if available. Hope this helps! If my explanation and the information I provided were helpful, I would greatly appreciate it if you could follow the instructions here so others with the same problem can benefit as well.

  1. WoodManEXP 80 Reputation points

    Hi Jay,

    TY for your response. Microsoft’s VS/UWP/.Net is a vast ecosystem yet to be mastered by me, in spite of multiple year’s usage, apologies for silly questions.

    It is an intriguing option to get the crash to happen on the local dev environ (Windows 10 Home with Visual Studio Insiders [11709.129]).

    For this item: - Enable .NET Native toolchain:

    With the release configuration, under Right-click the project in Solution Explorer and select Properties/Build there is presented no option β€œCompile with .NET Native toolchain.”

    Can you provide a bit more guidance to be able to more closely follow your idea?

    TY,

    WoodManEXP

    Also, The C# compiler has suggested these mark-ups for the method performing the serialization, might they be related?

    [RequiresUnreferencedCode("Calls System.Runtime.Serialization.Json.DataContractJsonSerializer.DataContractJsonSerializer(Type)")]
    [RequiresDynamicCode("Calls System.Runtime.Serialization.Json.DataContractJsonSerializer.DataContractJsonSerializer(Type)")]
    
  2. Jay Pham (WICLOUD CORPORATION) 3,800 Reputation points β€’ Microsoft External Staff β€’ Moderator

    Hi @WoodManEXP ,

    I see the compiler warnings you shared confirm the root cause. DataContractJsonSerializer relies on runtime reflection, and the trimmer strips that metadata when publishing to .msix. That is why F5 works but the installed package crashes.

    Regarding the .NET Native option: I apologize for the confusion. Your project uses the newer SDK-style format where IL trimming replaces .NET Native. The option I mentioned does not apply here.

    To reproduce locally, add this to your .csproj and run Release + F5:

    
    <PropertyGroup Condition="'$(Configuration)' == 'Release'">
    
     <PublishTrimmed>true</PublishTrimmed>
    
    </PropertyGroup>
    
    

    The fastest way to verify this is to tell the trimmer to keep the serialization types. Add the following to your .csproj:

    
    <ItemGroup>
    
     <TrimmerRootAssembly Include="System.Runtime.Serialization" />
    
     <TrimmerRootAssembly Include="System.Runtime.Serialization.Json" />
    
    </ItemGroup>
    
    

    If that stops the crash, you have confirmed the cause. This is a fine short-term fix.

    Longer term, I would suggest moving away from DataContractJsonSerializer and switching to System.Text.Json with source generators ([JsonSerializable]). Source generators handle serialization at compile time, so they do not depend on reflection and work cleanly with trimming. No rush on that; the TrimmerRootAssembly workaround will keep things running in the meantime.

    Hope this helps.


  3. WoodManEXP 80 Reputation points

    Hi Jay,

    TY for this good information!

    In general, do you recommend these statements be kept or removed (I have never quite understood what they accomplish) ?

    [RequiresUnreferencedCode("Calls System.Runtime.Serialization.Json.DataContractJsonSerializer.DataContractJsonSerializer(Type)")]
    [RequiresDynamicCode("Calls System.Runtime.Serialization.Json.DataContractJsonSerializer.DataContractJsonSerializer(Type)")]
    

    TY,

  4. Jay Pham (WICLOUD CORPORATION) 3,800 Reputation points β€’ Microsoft External Staff β€’ Moderator

    Hello @WoodManEXP ,

    I recommend keeping these attributes for now if you continue using DataContractJsonSerializer, but removing them once you switch to a modern, trim-safe alternative.

    To give you a quick picture of what they actually do:

    • [RequiresUnreferencedCode] flags that your method relies on dynamic reflection. Modern .NET features "trimming" to shrink binary size by removing unused code. Because reflection is dynamic, the compiler cannot statically guarantee which classes the serializer needs at runtime, and might strip them out. This attribute warns consumers that trimming this path will likely trigger runtime crashes.
    • [RequiresDynamicCode] flags that the method generates code on the fly. Under Native AOT, code is compiled completely ahead of time, meaning there is no JIT compiler at runtime. Legacy serializers like DataContractJsonSerializer rely on dynamic IL generation, which is fundamentally unsupported in AOT environments.

    Here is what I suggest you do with your codebase:

    If you plan to stick with DataContractJsonSerializer, you must keep both attributes. Removing them only silences the compiler warnings; it does not solve the underlying issue and will lead to silent runtime crashes for anyone publishing your code with trimming or Native AOT enabled.

    To resolve these warnings properly, I highly recommend migrating to System.Text.Json using source generators. This shifts all serialization logic to compile-time, eliminating reflection and dynamic code generation entirely. This makes your code fully compatible with modern .NET optimizations:

    using System.Text.Json.Serialization;
    
    public class UserData
    {
     public string Name { get; set; }
    }
    
    [JsonSerializable(typeof(UserData))]
    internal partial class SourceGenerationContext : JsonSerializerContext { }
    
    // Usage:
    var json = JsonSerializer.Serialize(myUserData, SourceGenerationContext.Default.UserData);
    
    

    I would greatly appreciate it if you could follow the instructions here so others with the same problem can benefit as well.

  5. WoodManEXP 80 Reputation points

    Hi Jay,

    Again, TY for the useful info. It seems like a good course of action is to move JSOM (de)serialization to the modern, nonreflective form and so this project will be moved in that direction.

    In the meantime, my idea that the crash may be that something is happening relative to JSON serialization may be wrong. The ideas recommended from your May 22 response have been tried but did not yield the results we hoped.

    This would not create the crash on the dev environment

    <PropertyGroup Condition="'$(Configuration)' == 'Release'">
      <PublishTrimmed>true</PublishTrimmed>
    </PropertyGroup>
    

    And this did not alleviate the crash from the .msix build/install

    <ItemGroup>
      <TrimmerRootAssembly Include="System.Runtime.Serialization" />
      <TrimmerRootAssembly Include="System.Runtime.Serialization.Json" />
    </ItemGroup>
    

    Let me try to upgrade to the more modern JSON stuff and see if the crash continues. What do you think?

    TY!


Sign in to comment

4 additional answers

  1. AI answer

    Use the installed package and remote debugging to narrow this down, then collect deployment/runtime diagnostics from the MSIX side.

    1. Debug the installed app package
    • On the target Windows 11 machine, install the .msix.
    • In Visual Studio, use Debug β†’ Other β†’ Debug Installed App Packages.
    • Select the app, then either:
      • Start it directly from this dialog, or
      • Select Do not launch, but debug my code when it starts, then launch the app normally so the debugger attaches as soon as it starts.
    • This lets breakpoints and exception settings work against the installed package, not just the F5-deployed build.

    If the app is on another device (Xbox, another PC, etc.), use the same Debug Installed App Packages flow with remote debugging. Visual Studio will install the correct remote debugger on the target device and attach to the installed package.

    1. Attach to the process if needed
    • Alternatively, use Debug β†’ Attach to Process to attach to the running UWP process.
    • For full debugging features (breakpoints, correct call stacks), ensure the installed binaries and PDBs match the build open in Visual Studio. The compiled app on the target must come from the same build as the local one.
    1. Capture deployment/runtime diagnostics for the MSIX Because the failure only occurs when installed from the .msix, also check packaging/deployment logs:
    • Open Event Viewer β†’ Applications and Services Logs β†’ Microsoft β†’ Windows.
    • Inspect:
      • AppXDeployment-Server β†’ Microsoft-Windows-AppXDeploymentServer/Operational
      • AppxPackagingOM β†’ Microsoft-Windows-AppxPackaging/Operational
    • These logs can reveal issues related to package integrity, file system virtualization, or container behavior that differ between F5 deployment and installed MSIX.
    • Optionally run Get-AppxLog in PowerShell to see the most recent deployment operation and correlate with the failing run.
    1. Compare builds and symbols
    • Ensure the package being installed is a debug (or at least symbolized) build so that Visual Studio can load matching PDBs.
    • Open the same solution/build in Visual Studio that produced the .msix, then attach via Debug Installed App Packages. This improves the usefulness of the stack trace and allows stepping into code around JSON deserialization and file I/O.
    1. Consider MSIX/OS differences
    • MSIX behavior can differ by Windows version. If targeting Windows 10 and running on Windows 11, confirm that no Windows 11–only manifest extensions or MSIX features are being used incorrectly.
    • If using newer MSIX features, verify that the target OS version supports them as described in MSIX feature/limitations documentation.

    By combining Debug Installed App Packages (or Attach to Process with matching symbols) and the MSIX deployment logs, it is possible to catch the exception in managed code, inspect the JSON/file I/O logic, and see whether containerized storage or packaging differences on Windows 11 are causing the unhandled exception.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.
  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Sign in to answer

Your answer