Skip to content

Microsoft.Extensions.Configuration APIs haven't been AOT-annotated #71654

@kant2002

Description

@kant2002

Consider this small application

using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Configuration;

var config = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .Build();

var appSettings = GetAppSettings(config);
Console.WriteLine($"{appSettings.Database}: {appSettings.ConnectionString}");

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern",
    Justification = "AppSettings is consists only from primitive type and thus will never be trimmed")]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL3050:UnrecognizedReflectionPattern",
    Justification = "AppSettings is consists only from primitive type and thus will never be trimmed")]
AppSettings GetAppSettings(IConfiguration config)
{
    return config.Get<AppSettings>();
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
public class AppSettings
{
    public string? ConnectionString { get; set; }

    public DatabaseServer Database { get; set; } = DatabaseServer.None;
}

public enum DatabaseServer
{
    None,
    SqlServer,
    PostgreSql,
    MySql
}

With following packages

dotnet add package Microsoft.Extensions.Configuration.Binder -v 7.0.0-*
dotnet add package Microsoft.Extensions.Configuration.Json -v 7.0.0-*

and `appsettings.json

{
    "ConnectionString": "Server=tfb-database;Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;Maximum Pool Size=1024;SslMode=None;ConnectionReset=false;ConnectionIdlePingTime=900;ConnectionIdleTimeout=0;AutoEnlist=false;DefaultCommandTimeout=0;ConnectionTimeout=0;IgnorePrepare=false;",
    "Database": "mysql"
}

ILC spill warnings

ILC : AOT analysis warning IL3050: Microsoft.Extensions.Configuration.ConfigurationBinder.AttemptBindToCollectionInterfaces(Type,IConfiguration,BinderOptions): Using member 'System.Type.MakeGenericType(Type[]
)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [D:\d\scratch\ConsoleApp7\ConsoleApp7.csproj]
ILC : AOT analysis warning IL3050: Microsoft.Extensions.Configuration.ConfigurationBinder.AttemptBindToCollectionInterfaces(Type,IConfiguration,BinderOptions): Using member 'System.Type.MakeGenericType(Type[]
)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [D:\d\scratch\ConsoleApp7\ConsoleApp7.csproj]
ILC : AOT analysis warning IL3050: Microsoft.Extensions.Configuration.ConfigurationBinder.BindArray(Type,IEnumerable,IConfiguration,BinderOptions): Using member 'System.Array.CreateInstance(Type,Int32)' which
 has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available. [D:\d\scratch\ConsoleApp7\ConsoleApp7.csproj]
ILC : AOT analysis warning IL3050: Microsoft.Extensions.Configuration.ConfigurationBinder.BindToCollection(Type,IConfiguration,BinderOptions): Using member 'System.Type.MakeGenericType(Type[])' which has 'Req
uiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. [D:\d\scratch\ConsoleApp7\ConsoleApp7.csproj]
/_/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/EnumConverter.cs(174): AOT analysis warning IL3050: System.ComponentModel.EnumConverter.ConvertTo(ITypeDescriptorContext,CultureI
nfo,Object,Type): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type a
t runtime. Use the GetValues<TEnum> overload instead. [D:\d\scratch\ConsoleApp7\ConsoleApp7.csproj]

From my understanding AppSettings is safe to map IConfiguration and I want express that. But as you can see all my silly attempts fails.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions