Skip to content
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

Options validate onstart #39254

Merged
merged 3 commits into from
Jan 25, 2024
Merged
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
44 changes: 41 additions & 3 deletions docs/core/extensions/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
author: IEvangelist
description: Learn the options pattern to represent groups of related settings in .NET apps. The options pattern uses classes to provide strongly-typed access to settings.
ms.author: dapine
ms.date: 06/23/2023
ms.date: 01/24/2024
---

# Options pattern in .NET
Expand Down Expand Up @@ -299,7 +299,7 @@

The `ValidateDataAnnotations` extension method is defined in the [Microsoft.Extensions.Options.DataAnnotations](https://www.nuget.org/packages/Microsoft.Extensions.Options.DataAnnotations) NuGet package.

The following code displays the configuration values or the validation errors:
The following code displays the configuration values or reports validation errors:

:::code language="csharp" source="snippets/configuration/console-json/ValidationService.cs":::

Expand All @@ -321,7 +321,45 @@
}, "VerbosityLevel must be > than Scale.");
```

### IValidateOptions for complex validation

Check failure on line 324 in docs/core/extensions/options.md

View workflow job for this annotation

GitHub Actions / lint

Multiple consecutive blank lines [Expected: 1; Actual: 2]
The validation occurs at run time, but you can configure it to occur at startup by instead chaining a call to `ValidateOnStart`:

```csharp
builder.Services
.AddOptions<SettingsOptions>()
.Bind(Configuration.GetSection(SettingsOptions.ConfigurationSectionName))
.ValidateDataAnnotations()
.Validate(config =>
{
if (config.Scale != 0)
{
return config.VerbosityLevel > config.Scale;
}

return true;
}, "VerbosityLevel must be > than Scale.")
.ValidateOnStart();
```

Starting with .NET 8, you can use an alternate API, <xref:Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions.AddOptionsWithValidateOnStart%60%601(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.String)>, that enables validation on start for a specific options type:

```csharp
builder.Services
.AddOptionsWithValidateOnStart<SettingsOptions>()
.Bind(Configuration.GetSection(SettingsOptions.ConfigurationSectionName))
.ValidateDataAnnotations()
.Validate(config =>
{
if (config.Scale != 0)
{
return config.VerbosityLevel > config.Scale;
}

return true;
}, "VerbosityLevel must be > than Scale.");
```

### `IValidateOptions` for complex validation

The following class implements <xref:Microsoft.Extensions.Options.IValidateOptions%601>:

Expand Down
Loading