runtime: ConfigurationManager is not trim compatible
Using the latest 6.0 SDK
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0-preview.3.21153.4" />
</ItemGroup>
</Project>
using System;
using System.Configuration;
namespace ConsoleApp7
{
class Program
{
static void Main(string[] args)
{
string sAttr = ConfigurationManager.AppSettings.Get("Key0");
Console.WriteLine("hello" + sAttr);
}
}
}
❯ dotnet publish -c Release -r win-x64 -p:PublishTrimmed=true -p:TrimMode=link
❯ C:\Users\eerhardt\source\repos\ConsoleApp7\ConsoleApp7\bin\Release\net6.0\win-x64\publish\ConsoleApp7.exe
Unhandled exception. System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize
---> System.MissingMethodException: Cannot dynamically create an instance of type 'System.Configuration.ClientConfigurationHost'. Reason: No parameterless constructor defined.
at System.RuntimeType.ActivatorCache..ctor(RuntimeType rt)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Configuration.TypeUtil.CreateInstance(Type type)
at System.Configuration.Internal.ConfigSystem.System.Configuration.Internal.IConfigSystem.Init(Type typeConfigHost, Object[] hostInitParams)
at System.Configuration.ClientConfigurationSystem..ctor()
at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
--- End of inner exception stack trace ---
at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
at System.Configuration.ConfigurationManager.PrepareConfigSystem()
at System.Configuration.ConfigurationManager.GetSection(String sectionName)
at System.Configuration.ConfigurationManager.get_AppSettings()
at ConsoleApp7.Program.Main(String[] args) in C:\Users\eerhardt\source\repos\ConsoleApp7\ConsoleApp7\Program.cs:line 12
Because https://github.com/dotnet/runtime/pull/48428 marked all assemblies building from dotnet/runtime
as IsTrimmable=true
, System.Configuration.ConfigurationManager is now getting aggressively trimmed. However, since we haven’t addressed the ILLink warnings in the assembly, it is not trim compatible.
We should probably remove IsTrimmable
from this assembly until we have addressed its warnings.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 6
- Comments: 15 (12 by maintainers)
Commits related to this issue
- Disable trimming on ConfigurationManager Contributes to #49062 — committed to eerhardt/runtime by eerhardt 3 years ago
- Disable trimming on ConfigurationManager (#49085) Contributes to #49062 — committed to dotnet/runtime by eerhardt 3 years ago
There are several different configuration systems available in .NET. Some are compatible with trimming and AOT and others are not.
ASP.NET Core uses
Microsoft.Extensions.Configuration
system that is trim and AOT compatible starting with .NET 8 (https://github.com/dotnet/runtime/issues/44493). If you need compatibility with trimming and AOT,Microsoft.Extensions.Configuration
would be the configuration system to use.