Skip to content

ConfigurationManager is not trim compatible #49062

@eerhardt

Description

@eerhardt

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 #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.

cc @marek-safar @sbomer

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions