-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[NativeAot] List<T> in options pattern not work in nativeaot mode #74436
Comments
Tagging subscribers to this area: @dotnet/area-extensions-options Issue DetailsDescriptionIf the option class have Reproduction StepsUse powershell to execute the following command Replace Program.cs with the following code: using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices((context, services) =>
{
services.Configure<TestOption>(context.Configuration.GetRequiredSection("App"));
});
builder.ConfigureAppConfiguration(configuration =>
{
configuration.AddCommandLine(args);
});
var app = builder.Build();
var option = app.Services.GetRequiredService<IOptions<TestOption>>();
Console.WriteLine($"Items.Count:{option.Value.Items.Count}");
await app.RunAsync();
public class TestOption
{
public List<TestItem> Items { get; set; }
}
public class TestItem
{
public int Id { get; set; }
}
Expected behaviorThe console output Items.Count:1 Actual behaviorThe console output Items.Count:0 Regression?If I change the the ilcompiler version to 7.0.0-preview.5.22301.12,it works. Known WorkaroundsNo response ConfigurationNo response Other informationNo response
|
Note that in 7.0 you will get a warning that this line
Is not supported in NativeAOT. Note the Lines 23 to 26 in 0f74875
Moving to the |
I doubt this is AOT issue. It's probably a trimming issue. AOT issue pretty much always have a runtime exception unless there's code somewhere that catches and swallows it. Trimming issues typically have this failure mode where something is just missing without an exception. If I run the repro, I'm getting I do see a build time warning saying "TOptions's dependent types may have their members trimmed." which would explain why TestOption's dependent type TestItem has no members. |
Ah that's probably it. We are trimming the parameterless ctor on An exception is getting thrown:
But that is getting swallowed here: runtime/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs Lines 634 to 649 in 0469020
I believe we can close this as "by design". You are getting a trimming warning. Using Configuration Binder in a trimmed app today requires you manually preserve the necessary members, or don't use it at all. There isn't anything we can do to make this "safe" short of dotnet/linker#1087 or #44493. |
I was able to get the app to run with passing |
Sorry,the command {
"App": {
"Items": [
{
"Id": 1
}
]
}
} If I add another a TestItem property to TestOption ,I can get the value of the TestItem Property,but can't get the You can test it use this code: using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices((context, services) =>
{
services.Configure<TestOption>(context.Configuration.GetRequiredSection("App"));
});
builder.ConfigureAppConfiguration(configuration =>
{
configuration.AddCommandLine(args);
});
var app = builder.Build();
var option = app.Services.GetRequiredService<IOptions<TestOption>>();
Console.WriteLine($"Items.Count:{option.Value.Items.Count}");
Console.WriteLine($"Item.Id:{option.Value.Item.Id}");
await app.RunAsync();
public class TestOption
{
public TestItem Item { get; set; }
public List<TestItem> Items { get; set; }
}
public class TestItem
{
public int Id { get; set; }
} rd.xml
OptionsPatternTestApp.csproj
Run app use this command: |
Use this code instead of an rd.xml: using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System.Diagnostics.CodeAnalysis;
class Program
{
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(TestOption))] // use this
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(TestItem))] // use this
public static async Task Main(string[] args)
{
var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices((context, services) =>
{
services.Configure<TestOption>(context.Configuration.GetRequiredSection("App"));
});
builder.ConfigureAppConfiguration(configuration =>
{
configuration.AddCommandLine(args);
});
var app = builder.Build();
var option = app.Services.GetRequiredService<IOptions<TestOption>>();
Console.WriteLine($"Items.Count:{option.Value.Items.Count}");
Console.WriteLine($"Item.Id:{option.Value.Item.Id}");
await app.RunAsync();
}
}
public class TestOption
{
public TestItem Item { get; set; }
public List<TestItem> Items { get; set; }
}
public class TestItem
{
public int Id { get; set; }
} Running that with PublishTrimmed=true works fine. |
However, running that same app with PublishTrimmed=true:
PublishAot=true:
|
Add |
Thank you for your help,this is a trimming issue and can be fixed by adding DynamicDependencyAttribute |
We're likely not keeping the |
Description
If the option class have
List<T>
property,I can not get the value of theList<T>
property fromIOptions<TOption>
Reproduction Steps
Use powershell to execute the following command
> dotnet new console -o OptionsPatternTestApp
> cd OptionsPatternTestApp
> dotnet add package Microsoft.Extensions.Hosting --version 6.0.1
> dotnet add package Microsoft.DotNet.ILCompiler --version 7.0.0-preview.7.22375.6
Replace Program.cs with the following code:
> dotnet publish -c Release -r win-x64
> ./bin/Release/net6.0/win-x64/native/OptionsPatternTestApp.exe App__Items__0__Id=1
The console will output
Items.Count:0
Expected behavior
The console output Items.Count:1
Actual behavior
The console output Items.Count:0
Regression?
If I change the the ilcompiler version to 7.0.0-preview.5.22301.12,it works.
Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: