Language

OptionsBuilderExtensions.ValidateOnStart<TOptions> Method

Definition

Enforces options validation check on start rather than at run time.

public:
generic <typename TOptions>
 where TOptions : class[System::Runtime::CompilerServices::Extension]
 static Microsoft::Extensions::Options::OptionsBuilder<TOptions> ^ ValidateOnStart(Microsoft::Extensions::Options::OptionsBuilder<TOptions> ^ optionsBuilder);
public static Microsoft.Extensions.Options.OptionsBuilder<TOptions> ValidateOnStart<TOptions>(this Microsoft.Extensions.Options.OptionsBuilder<TOptions> optionsBuilder) where TOptions : class;
static member ValidateOnStart : Microsoft.Extensions.Options.OptionsBuilder<'Options (requires 'Options : null)> -> Microsoft.Extensions.Options.OptionsBuilder<'Options (requires 'Options : null)> (requires 'Options : null)
<Extension()>
Public Function ValidateOnStart(Of TOptions As Class) (optionsBuilder As OptionsBuilder(Of TOptions)) As OptionsBuilder(Of TOptions)

Type Parameters

TOptions

The type of options.

Parameters

optionsBuilder
OptionsBuilder<TOptions>

The OptionsBuilder<TOptions> to configure options instance.

Returns

The OptionsBuilder<TOptions> so that additional calls can be chained.

Remarks

With the built-in IOptionsFactory<TOptions>, asynchronous validation runs during startup and seeds the built-in IOptions<TOptions> and IOptionsMonitor<TOptions> instances when their caches do not already contain a value. Options requiring asynchronous validation cannot be accessed synchronously before startup completes. A value successfully created synchronously before or during startup remains the cache winner, and a faulted monitor cache entry causes startup validation to fail rather than being replaced.

A derived or replacement IOptionsFactory<TOptions> uses synchronous startup validation and does not invoke ValidateAsync(String, TOptions, CancellationToken). Default-name asynchronous validation requires the built-in IOptions<TOptions> implementation; startup throws InvalidOperationException when a custom implementation is registered. The built-in IOptionsSnapshot<TOptions> validates synchronously in per-scope caches that startup validation does not populate. The built-in options monitor also reloads synchronously and does not invoke asynchronous validation. The built-in asynchronous validators therefore cause reload to fail and prevent change listeners from being notified; no asynchronous last-known-good guarantee is provided.

Publication uses GetOrAdd(String, Func<TOptions>) so an existing monitor value is not replaced. For compatibility, this method exposes the built-in startup validator through IStartupValidator and IAsyncStartupValidator as the same singleton. A custom validator registered only as IStartupValidator takes precedence and suppresses all asynchronous startup validators. New custom startup validators should register only IAsyncStartupValidator.

Applies to