Answer accepted by question author
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
AppContainersandbox
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.
-
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)")] -
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.
DataContractJsonSerializerrelies 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
.csprojand 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
DataContractJsonSerializerand switching toSystem.Text.Jsonwith 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; theTrimmerRootAssemblyworkaround will keep things running in the meantime.Hope this helps.
-
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,
-
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
DataContractJsonSerializerrely 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.Jsonusing 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.
-
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
