Libraries Feature Switches

July 31, 2021 ยท View on GitHub

Starting with .NET 5 there are several feature-switches available which can be used to control the size of the final binary. They are available in all configurations but their defaults might vary as any SDK can set the defaults differently.

Available Feature Switches

MSBuild Property NameAppContext SettingDescription
DebuggerSupportSystem.Diagnostics.Debugger.IsSupportedAny dependency that enables better debugging experience to be trimmed when set to false
EnableUnsafeUTF7EncodingSystem.Text.Encoding.EnableUnsafeUTF7EncodingInsecure UTF-7 encoding is trimmed when set to false
EnableUnsafeBinaryFormatterSerializationSystem.Runtime.Serialization.EnableUnsafeBinaryFormatterSerializationBinaryFormatter serialization support is trimmed when set to false
EventSourceSupportSystem.Diagnostics.Tracing.EventSource.IsSupportedAny EventSource related code or logic is trimmed when set to false
InvariantGlobalizationSystem.Globalization.InvariantAll globalization specific code and data is trimmed when set to true
PredefinedCulturesOnlySystem.Globalization.PredefinedCulturesOnlyDon't allow creating a culture for which the platform does not have data
UseSystemResourceKeysSystem.Resources.UseSystemResourceKeysAny localizable resources for system assemblies is trimmed when set to true
HttpActivityPropagationSupportSystem.Net.Http.EnableActivityPropagationAny dependency related to diagnostics support for System.Net.Http is trimmed when set to false
UseNativeHttpHandlerSystem.Net.Http.UseNativeHttpHandlerHttpClient uses by default platform native implementation of HttpMessageHandler if set to true.
StartupHookSupportSystem.StartupHookProvider.IsSupportedStartup hooks are disabled when set to false. Startup hook related functionality can be trimmed.
AutoreleasePoolSupportSystem.Threading.Thread.EnableAutoreleasePoolWhen set to true, creates an NSAutoreleasePool for each thread and thread pool work item on applicable platforms.
CustomResourceTypesSupportSystem.Resources.ResourceManager.AllowCustomResourceTypesUse of custom resource types is disabled when set to false. ResourceManager code paths that use reflection for custom types can be trimmed.
EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerializationSystem.ComponentModel.TypeConverter.EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerializationBinaryFormatter serialization support is trimmed when set to false.
BuiltInComInteropSupportSystem.Runtime.InteropServices.BuiltInComInterop.IsSupportedBuilt-in COM support is trimmed when set to false.
EnableCppCLIHostActivationSystem.Runtime.InteropServices.EnableCppCLIHostActivationC++/CLI host activation code is disabled when set to false and related functionality can be trimmed.
MetadataUpdaterSupportSystem.Reflection.Metadata.MetadataUpdater.IsSupportedMetadata update related code to be trimmed when set to false
_EnableConsumingManagedCodeFromNativeHostingSystem.Runtime.InteropServices.EnableConsumingManagedCodeFromNativeHostingGetting a managed function from native hosting is disabled when set to false and related functionality can be trimmed.
VerifyDependencyInjectionOpenGenericServiceTrimmabilityMicrosoft.Extensions.DependencyInjection.VerifyOpenGenericServiceTrimmabilityWhen set to true, DependencyInjection will verify trimming annotations applied to open generic services are correct
NullabilityInfoContextSupportSystem.Reflection.NullabilityInfoContext.IsSupportedNullable attributes can be trimmed when set to false
_AggressiveAttributeTrimmingSystem.AggressiveAttributeTrimmingWhen set to true, aggressively trims attributes to allow for the most size savings possible, even if it could result in runtime behavior changes

Any feature-switch which defines property can be set in csproj file or on the command line as any other MSBuild property. Those without predefined property name the value can be set with following XML tag in csproj file.

<RuntimeHostConfigurationOption Include="<AppContext-Setting>"
                                Value="false"
                                Trim="true" />

Adding New Feature Switch

The primary goal of features switches is to produce smaller output by removing code which is unreachable under feature condition. The typical approach is to introduce static bool like property which is used to guard the dependencies which can be trimmed when the value is flipped. Ideally, the static property should be located in type which does not have any static constructor logic. Once you are done with the code changes following steps connects the code with trimming settings.

Add XML settings for the features switch to assembly substitution. It's usually located in src/ILLink/ILLink.Substitutions.xml file for each library. The example of the syntax used to control EnableUnsafeUTF7Encoding property is following.

<method signature="System.Boolean get_EnableUnsafeUTF7Encoding()" body="stub" value="false" feature="System.Text.Encoding.EnableUnsafeUTF7Encoding" featurevalue="false" />

Add MSBuild integration by adding new RuntimeHostConfigurationOption entry. The file is located in Microsoft.NET.Sdk.targets file and includes all other public feature-switches. You can add a new one by simply adding a new XML tag

<RuntimeHostConfigurationOption Include="<AppContext-Setting>"
            Condition="'$(<msbuild-property-name>)' != ''"
            Value="$(<msbuild-property-name>)"
            Trim="true" />

Please don't forget to update the table with available features-switches when you are done.