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
ValidateOnStart to 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
ValidationHostedService into Host bootstrapping as a shared option infrastructure. It becomes part of default Host and doesn't need to be registered with every ValidateOnStart call.
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.
Usage of ValidateOnStart() today is located in the
Microsoft.Extensions.Hostingassembly\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. TheValidateOnStart()functionality should be moved to theMicrosoft.Extensions.Optionspackage.In order for a host to call a "Validate()" method, a new public interface needs to be added, called
IValidateOnStarthere. This interface will be injected by theValidateOnStart()functionality and then obtained by a host (for the default host, in theMicrosoft.Extensions.Hostingassembly) with a call toGetService<IValidateOnStart>()in order to trigger the validation during startup.The new interface located in the
Microsoft.Extensions.Optionsassembly:namespace Microsoft.Extensions.Options { + public interface IValidateOnStart + { + void Validate(); + } }The existing
ValidateOnStart()extension method is located on theMicrosoft.Extensions.DependencyInjection.OptionsBuilderExtensionsextension class in theMicrosoft.Extensions.Hostingassembly. TheMicrosoft.Extensions.Hostingassembly needs to forward this to theMicrosoft.Extensions.Optionsassembly:Original request
Background and motivation
Usage of
ValidateOnStarttoday is tightly coupled with Microsoft.Extensions.Hosting package. The call registers bothValidationHostedServiceand instructsValidateOnStartinfrastructure to validateT-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:
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.ValidationHostedServiceintoHostbootstrapping as a shared option infrastructure. It becomes part of defaultHostand doesn't need to be registered with everyValidateOnStartcall.API Proposal
(Shape of the API remains the same)
API Usage
(Usage of the API remains the same.)
Alternative Designs
No response
Risks
Moving types around.