-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Description
Usage of ValidateOnStart() today is located in the Microsoft.Extensions.Hosting assembly\package. This is problematic as it greatly inflates dependency tree of packages that need to validate their options eagerly when the hosting package is not otherwise needed. The ValidateOnStart() functionality should be moved to the Microsoft.Extensions.Options package.
In order for a host to call a "Validate()" method, a new public interface needs to be added, called IValidateOnStart here. This interface will be injected by the ValidateOnStart() functionality and then obtained by a host (for the default host, in the Microsoft.Extensions.Hosting assembly) with a call to GetService<IValidateOnStart>() in order to trigger the validation during startup.
The new interface located in the Microsoft.Extensions.Options assembly:
namespace Microsoft.Extensions.Options
{
+ public interface IValidateOnStart
+ {
+ void Validate();
+ }
}The existing ValidateOnStart() extension method is located on the Microsoft.Extensions.DependencyInjection.OptionsBuilderExtensions extension class in the Microsoft.Extensions.Hosting assembly. The Microsoft.Extensions.Hosting assembly needs to forward this to the Microsoft.Extensions.Options assembly:
+[assembly: System.Runtime.CompilerServices.TypeForwardedTo(
+ typeof(Microsoft.Extensions.DependencyInjection.OptionsBuilderExtensions))]Original request
Background and motivation
Usage of ValidateOnStart today is tightly coupled with Microsoft.Extensions.Hosting package. The call registers both ValidationHostedService and instructs ValidateOnStart infrastructure to validate T-type upon startup. This is problematic as it greatly inflates dependency tree of packages that need to validate their options eagerly.
Following measures should be considered:
- Move
ValidateOnStartto Microsoft.Extensions.Options package. This decouple registration from implementation. Code that expresses the desire for eager validation doesn't involve registration of ValidationHostedService in the same call. The new API enables a cleaner separation of concern. - Incorporate
ValidationHostedServiceintoHostbootstrapping as a shared option infrastructure. It becomes part of defaultHostand doesn't need to be registered with everyValidateOnStartcall.
API Proposal
namespace Microsoft.Extensions.Options
{
public static class OptionsBuilderExtensions
{
public static OptionsBuilder<TOptions> ValidateOnStart<TOptions>(this OptionsBuilder<TOptions> builder)
where TOptions : class;
}(Shape of the API remains the same)
API Usage
(Usage of the API remains the same.)
Alternative Designs
No response
Risks
Moving types around.