@@ -10,6 +10,8 @@ namespace Microsoft.Extensions.DependencyInjection
10
10
/// <summary>
11
11
/// Extension methods for adding configuration related options services to the DI container via <see cref="OptionsBuilder{TOptions}"/>.
12
12
/// </summary>
13
+ [ UnconditionalSuppressMessage ( "ReflectionAnalysis" , "IL2091:UnrecognizedReflectionPattern" ,
14
+ Justification = "Workaround for https://github.com/mono/linker/issues/1416. Outer method has been annotated with DynamicallyAccessedMembers." ) ]
13
15
public static class OptionsBuilderExtensions
14
16
{
15
17
/// <summary>
@@ -28,23 +30,14 @@ public static class OptionsBuilderExtensions
28
30
29
31
optionsBuilder . Services . AddHostedService < ValidationHostedService > ( ) ;
30
32
optionsBuilder . Services . AddOptions < ValidatorOptions > ( )
31
- . Configure < IOptionsMonitor < TOptions > > ( ( vo , options ) => ValidateOnStartHelper ( vo , options , optionsBuilder ) ) ;
33
+ . Configure < IOptionsMonitor < TOptions > > ( ( vo , options ) =>
34
+ {
35
+ // This adds an action that resolves the options value to force evaluation
36
+ // We don't care about the result as duplicates are not important
37
+ vo . Validators [ typeof ( TOptions ) ] = ( ) => options . Get ( optionsBuilder . Name ) ;
38
+ } ) ;
32
39
33
40
return optionsBuilder ;
34
41
}
35
-
36
- [ UnconditionalSuppressMessage ( "ReflectionAnalysis" , "IL2091:UnrecognizedReflectionPattern" ,
37
- Justification = "Workaround for https://github.com/mono/linker/issues/1416. Outer method has been annotated with DynamicallyAccessedMembers." ) ]
38
- private static void ValidateOnStartHelper < TOptions > ( ValidatorOptions vo , IOptionsMonitor < TOptions > options , OptionsBuilder < TOptions > optionsBuilder )
39
- where TOptions : class
40
- {
41
- // This adds an action that resolves the options value to force evaluation
42
- // We don't care about the result as duplicates are not important
43
- vo . Validators [ typeof ( TOptions ) ] = ( ) => GetOptionsMethod ( options , optionsBuilder ) ;
44
- }
45
-
46
- [ UnconditionalSuppressMessage ( "ReflectionAnalysis" , "IL2091:UnrecognizedReflectionPattern" ,
47
- Justification = "Workaround for https://github.com/mono/linker/issues/1416. Outer method has been annotated with DynamicallyAccessedMembers." ) ]
48
- private static void GetOptionsMethod < TOptions > ( IOptionsMonitor < TOptions > options , OptionsBuilder < TOptions > optionsBuilder ) where TOptions : class => options . Get ( optionsBuilder . Name ) ;
49
42
}
50
43
}
0 commit comments