Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add list of recommended replacements for popular reflection-based serializers #36769

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions docs/core/deploying/trimming/incompatibilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,24 @@ Automatic [COM marshalling](../../../standard/native-interop/cominterop.md) has

## WPF

The Windows Presentation Foundation (WPF) framework makes substantial use of reflection and some features are heavily reliant on run-time code inspection. In .NET 6, it's not possible for trimming analysis to preserve all necessary code for WPF applications. Unfortunately, almost no WPF apps are runnable after trimming, so trimming support for WPF has been disabled in the .NET 6 SDK.
The Windows Presentation Foundation (WPF) framework makes substantial use of reflection and some features are heavily reliant on run-time code inspection. In .NET 6, it's not possible for trimming analysis to preserve all necessary code for WPF applications. Unfortunately, almost no WPF apps are runnable after trimming, so trimming support for WPF is currently disabled in the .NET SDK. See [WPF is not trim-compatible](https://github.com/dotnet/wpf/issues/3811) issue for progress on enabling trimming for WPF.

## Windows Forms

The Windows Forms framework makes minimal use of reflection, but is heavily reliant on built-in COM marshalling. In .NET 6, it has not yet been converted to use ComWrappers. Unfortunately, almost no Windows Forms apps are runnable without built-in COM marshalling, so trimming support for Windows Forms apps has been disabled in the .NET 6 SDK.
The Windows Forms framework makes minimal use of reflection, but is heavily reliant on built-in COM marshalling. Unfortunately, almost no Windows Forms apps are runnable without built-in COM marshalling, so trimming support for Windows Forms apps is disabled in the .NET SDK currently. See [Make WinForms trim compatible](https://github.com/dotnet/winforms/issues/4649) issue for progress on enabling trimming for Windows Forms.

## Reflection-based serializers

Alternative: Reflection-free serializers, like source-generated [System.Text.Json](../../../standard/serialization/system-text-json/source-generation.md).
Alternative: Reflection-free serializers.

Many uses of reflection can be made trimming-compatible, as described in [Introduction to trim warnings](fixing-warnings.md). However, serializers tend to have very complex uses of reflection. Many of these uses cannot be made analyzable at build time. Unfortunately, the best option is often to rewrite the system to use source generation instead.

Popular reflection-based serializers and their recommended alternatives:

- **Newtonsoft.Json**. Recommended alternative: [source generated System.Text.Json](../../../standard/serialization/system-text-json/source-generation.md)
- **System.Configuration.ConfigurationManager**. Recommended alternative: [source generated Microsoft.Extensions.Configuration](https://github.com/dotnet/runtime/issues/44493)
- **System.Runtime.Serialization.Formatters.Binary.BinaryFormatter** Recommended alternative: [Migrate away from BinaryFormatter serialization due to its security and reliability flaws.](../../compatibility/serialization/7.0/binaryformatter-apis-produce-errors.md#recommended-action)

## Dynamic assembly loading and execution

Trimming and dynamic assembly loading is a common problem for systems that support plugins or extensions, usually through APIs like <xref:System.Reflection.Assembly.LoadFrom(System.String)>. Trimming relies on seeing all assemblies at build time, so it knows which code is used and cannot be trimmed away. Most plugin systems load third-party code dynamically, so it's not possible for the trimmer to identify what code is needed.
2 changes: 1 addition & 1 deletion docs/core/deploying/trimming/trim-warnings/il2104.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ f1_keywords:

## Cause

The assembly 'assembly' produced trim analysis warnings in the context of the app. This means the assembly has not been fully annotated for trimming. Consider contacting the library author to request they add trim annotations to the library. To see detailed warnings for this assembly, turn off grouped warnings by passing either `--singlewarn-` ILLink command line option to show detailed warnings for all assemblies, or `--singlewarn- "assembly"` to show detailed warnings for that assembly. For more information on annotating libraries for trimming, see [Prepare .NET libraries for trimming](../prepare-libraries-for-trimming.md).
The assembly 'assembly' produced trim analysis warnings in the context of the app. This means the assembly has not been fully annotated for trimming. Consider contacting the library author to request they add trim annotations to the library. To see detailed warnings, turn off grouped warnings by setting [`<TrimmerSingleWarn>false</TrimmerSingleWarn>`](../trimming-options.md#show-detailed-warnings) property in your project file. For more information on annotating libraries for trimming, see [Prepare .NET libraries for trimming](../prepare-libraries-for-trimming.md).
Loading