-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move startup validation to the options assembly (#88546)
- Loading branch information
1 parent
1b2664d
commit 693eef6
Showing
13 changed files
with
225 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/libraries/Microsoft.Extensions.Hosting/src/Microsoft.Extensions.Hosting.Forwards.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(Microsoft.Extensions.DependencyInjection.OptionsBuilderExtensions))] |
57 changes: 0 additions & 57 deletions
57
src/libraries/Microsoft.Extensions.Hosting/src/ValidationHostedService.cs
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
src/libraries/Microsoft.Extensions.Hosting/src/ValidatorOptions.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/libraries/Microsoft.Extensions.Options/src/IStartupValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.Extensions.Options | ||
{ | ||
/// <summary> | ||
/// Interface used by hosts to validate options during startup. | ||
/// Options are enabled to be validated during startup by calling <see cref="DependencyInjection.OptionsBuilderExtensions.ValidateOnStart{TOptions}(OptionsBuilder{TOptions})"/>. | ||
/// </summary> | ||
public interface IStartupValidator | ||
{ | ||
/// <summary> | ||
/// Calls the <see cref="IValidateOptions{TOptions}"/> validators. | ||
/// </summary> | ||
/// <exception cref="OptionsValidationException">One or more <see cref="IValidateOptions{TOptions}"/> return failed <see cref="ValidateOptionsResult"/> when validating.</exception> | ||
void Validate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/libraries/Microsoft.Extensions.Options/src/StartupValidatorOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.Extensions.Options | ||
{ | ||
internal sealed class StartupValidatorOptions | ||
{ | ||
// Maps each pair of a) options type and b) options name to a method that forces its evaluation, e.g. IOptionsMonitor<TOptions>.Get(name) | ||
public Dictionary<(Type, string), Action> _validators { get; } = new Dictionary<(Type, string), Action>(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/libraries/Microsoft.Extensions.Options/src/ValidateOnStart.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.ExceptionServices; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Microsoft.Extensions.Options | ||
{ | ||
internal sealed class StartupValidator : IStartupValidator | ||
{ | ||
private readonly StartupValidatorOptions _validatorOptions; | ||
|
||
public StartupValidator(IOptions<StartupValidatorOptions> validators) | ||
{ | ||
_validatorOptions = validators.Value; | ||
} | ||
|
||
public void Validate() | ||
{ | ||
List<Exception>? exceptions = null; | ||
|
||
foreach (Action validator in _validatorOptions._validators.Values) | ||
{ | ||
try | ||
{ | ||
// Execute the validation method and catch the validation error | ||
validator(); | ||
} | ||
catch (OptionsValidationException ex) | ||
{ | ||
exceptions ??= new(); | ||
exceptions.Add(ex); | ||
} | ||
} | ||
|
||
if (exceptions != null) | ||
{ | ||
if (exceptions.Count == 1) | ||
{ | ||
// Rethrow if it's a single error | ||
ExceptionDispatchInfo.Capture(exceptions[0]).Throw(); | ||
} | ||
|
||
if (exceptions.Count > 1) | ||
{ | ||
// Aggregate if we have many errors | ||
throw new AggregateException(exceptions); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.