Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ValidateOnStart calls configure options twice #71112

Answered by mshann03
mshann03 asked this question in Q&A
Discussion options

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.

You must be logged in to vote

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

Comment options

Sorry, I can't transfer this discussion to the aspnetcore repo, so I'll just mark this as answered and copy to there. Apologies.

You must be logged in to vote
0 replies
Answer selected by danmoseley
Comment options

You must be logged in to vote
0 replies
Comment options

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.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /