ConfigurationBinder.Bind
and ConfigurationBinder.Get
trimming warning
#59230
Answered
by
davidfowl
eduherminio
asked this question in
Q&A
-
Given a project with : <PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<PublishTrimmed>true</PublishTrimmed>
<TrimMode>link</TrimMode>
</PropertyGroup> public class GeneralSettings {}
var settingsInstance = new GeneralSettings();
var config = new ConfigurationBuilder().Build(); When I use config.GetRequiredSection(nameof(EngineSettings)).Bind(settingsInstance); I get the following warning: warning IL2026:
Using method 'Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(Microsoft.Extensions.Configuration.IConfiguration, object)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Cannot statically analyze the type of instance so its members may be trimmed. If I use settingsInstance = config.GetRequiredSection(nameof(GeneralSettings)).Get<GeneralSettings>(); Using method 'Microsoft.Extensions.Configuration.ConfigurationBinder.Get<Lynx.GameParameters>(Microsoft.Extensions.Configuration.IConfiguration)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed. In both cases the code works after the trimming. Should I suppress the warning? I've read Customizing Trimming in .NET 5, but I'm unsure what's the best way to proceed for these specific methods. |
Beta Was this translation helpful? Give feedback.
Answered by
davidfowl
Oct 4, 2021
Replies: 1 comment 16 replies
-
These methods aren't trimming friendly. The linker can't tell what properties it should keep when you use these methods. Your options are:
|
Beta Was this translation helpful? Give feedback.
16 replies
Answer selected by
eduherminio
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These methods aren't trimming friendly. The linker can't tell what properties it should keep when you use these methods. Your options are:
GeneralSettings
are used (using the dynamic dependency annotation)GeneralSettings
properties.