Skip to content

Resolve ILLink warnings in Microsoft.Extensions.Hosting #55048

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

Merged
merged 1 commit into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Microsoft.Extensions.DependencyInjection
/// <summary>
/// Extension methods for adding configuration related options services to the DI container via <see cref="OptionsBuilder{TOptions}"/>.
/// </summary>
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2091:UnrecognizedReflectionPattern",
Justification = "Workaround for https://github.com/mono/linker/issues/1416. Outer method has been annotated with DynamicallyAccessedMembers.")]
public static class OptionsBuilderExtensions
{
/// <summary>
Expand All @@ -28,23 +30,14 @@ public static class OptionsBuilderExtensions

optionsBuilder.Services.AddHostedService<ValidationHostedService>();
optionsBuilder.Services.AddOptions<ValidatorOptions>()
.Configure<IOptionsMonitor<TOptions>>((vo, options) => ValidateOnStartHelper(vo, options, optionsBuilder));
.Configure<IOptionsMonitor<TOptions>>((vo, options) =>
{
// This adds an action that resolves the options value to force evaluation
// We don't care about the result as duplicates are not important
vo.Validators[typeof(TOptions)] = () => options.Get(optionsBuilder.Name);
});

return optionsBuilder;
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2091:UnrecognizedReflectionPattern",
Justification = "Workaround for https://github.com/mono/linker/issues/1416. Outer method has been annotated with DynamicallyAccessedMembers.")]
private static void ValidateOnStartHelper<TOptions>(ValidatorOptions vo, IOptionsMonitor<TOptions> options, OptionsBuilder<TOptions> optionsBuilder)
where TOptions : class
{
// This adds an action that resolves the options value to force evaluation
// We don't care about the result as duplicates are not important
vo.Validators[typeof(TOptions)] = () => GetOptionsMethod(options, optionsBuilder);
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2091:UnrecognizedReflectionPattern",
Justification = "Workaround for https://github.com/mono/linker/issues/1416. Outer method has been annotated with DynamicallyAccessedMembers.")]
private static void GetOptionsMethod<TOptions>(IOptionsMonitor<TOptions> options, OptionsBuilder<TOptions> optionsBuilder) where TOptions : class => options.Get(optionsBuilder.Name);
}
}