-
Notifications
You must be signed in to change notification settings - Fork 5.5k
-
Hello,
I've got a scenario where I'm using OptionsBuilderExtensions.ValidateOnStart to validate some settings to fail fast if there are any values missing etc.
I've wired in an IValidateOptions<> implementation to do the validation, and I have an IConfigureOptions<> implementation to change some of the settings on load.
The issue I'm experiencing is both the configure and validate implementations get called twice, and if I change any of the setting values in the first call to configure, it resets on the second call. Is this expected? I would have thought he validate and configure would run once, both on startup.
Here is a cutdown sample that illustrates my point:
Simple program.cs setup:
using Microsoft.Extensions.Options;
using OptionsValidationExample;
var builder = WebApplication.CreateBuilder(args);
builder
.Services
.AddOptions<GreetingSettings>()
.Configure(settings => settings.Name = "Initial Name")
.ValidateOnStart();
builder.Services.AddSingleton<IValidateOptions<GreetingSettings>, SettingsValidateOptions>();
builder.Services.AddSingleton<IConfigureOptions<GreetingSettings>, SettingsConfigureOptions>();
var app = builder.Build();
app.MapGet("/", (IOptions<GreetingSettings> settings) =>
{
return $"Hello {settings.Value.Name}!";
});
app.Run();
And my GreetingSettings.cs file complete with validateoptions and configureoptions implementations:
using Microsoft.Extensions.Options;
using System.Diagnostics;
namespace OptionsValidationExample;
public class GreetingSettings
{
public string Name { get; set; } = null!;
}
public class SettingsConfigureOptions : IConfigureOptions<GreetingSettings>
{
public void Configure(GreetingSettings options)
{
// This method gets called twice due to ValidateOnStartup for the settings.
Debugger.Break();
// The second time it should come through as "Changed Name".
Debug.Assert(options.Name == "Initial Name");
options.Name = "Changed Name";
}
}
public class SettingsValidateOptions : IValidateOptions<GreetingSettings>
{
public ValidateOptionsResult Validate(string name, GreetingSettings options)
{
// This method gets called twice: once for ValidateOnStartup and once for first usage.
Debugger.Break();
return ValidateOptionsResult.Success;
}
}
Any suggestions on how to fix this?
Many thanks in advance,
Mark.
Beta Was this translation helpful? Give feedback.
All reactions
Sorry, I can't transfer this discussion to the aspnetcore repo, so I'll just mark this as answered and copy to there. Apologies.
Replies: 3 comments
-
Sorry, I can't transfer this discussion to the aspnetcore repo, so I'll just mark this as answered and copy to there. Apologies.
Beta Was this translation helpful? Give feedback.
All reactions
-
Beta Was this translation helpful? Give feedback.
All reactions
-
Hello,
I've got a scenario where I'm using
OptionsBuilderExtensions.ValidateOnStartto validate some settings to fail fast if there are any values missing etc.I've wired in an
IValidateOptions<>implementation to do the validation, and I have anIConfigureOptions<>implementation to change some of the settings on load.The issue I'm experiencing is both the configure and validate implementations get called twice, and if I change any of the setting values in the first call to configure, it resets on the second call. Is this expected? I would have thought he validate and configure would run once, both on startup.
Here is a cutdown sample that illustrates my point:
Simple
program.cssetup:using Microsoft.Extensions.Options; using OptionsValidationExample; var builder = WebApplication.CreateBuilder(args); builder .Services .AddOptions<GreetingSettings>() .Configure(settings => settings.Name = "Initial Name") .ValidateOnStart(); builder.Services.AddSingleton<IValidateOptions<GreetingSettings>, SettingsValidateOptions>(); builder.Services.AddSingleton<IConfigureOptions<GreetingSettings>, SettingsConfigureOptions>(); var app = builder.Build(); app.MapGet("/", (IOptions<GreetingSettings> settings) => { return $"Hello {settings.Value.Name}!"; }); app.Run();And my GreetingSettings.cs file complete with validateoptions and configureoptions implementations:
using Microsoft.Extensions.Options; using System.Diagnostics; namespace OptionsValidationExample; public class GreetingSettings { public string Name { get; set; } = null!; } public class SettingsConfigureOptions : IConfigureOptions<GreetingSettings> { public void Configure(GreetingSettings options) { // This method gets called twice due to ValidateOnStartup for the settings. Debugger.Break(); // The second time it should come through as "Changed Name". Debug.Assert(options.Name == "Initial Name"); options.Name = "Changed Name"; } } public class SettingsValidateOptions : IValidateOptions<GreetingSettings> { public ValidateOptionsResult Validate(string name, GreetingSettings options) { // This method gets called twice: once for ValidateOnStartup and once for first usage. Debugger.Break(); return ValidateOptionsResult.Success; } }Any suggestions on how to fix this?
Many thanks in advance,
Mark.
Beta Was this translation helpful? Give feedback.