What is the order in which ASP.NET Core configuration options are executed
This article mainly introduces "ASP.NET Core configuration options execution order is what", in daily operation, I believe many people in ASP.NET Core configuration options execution order is what problem there are doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation method, hope to answer everyone "ASP.NET Core configuration options execution order is what" doubts help! Next, please follow the small series to learn together!
Assuming we set all three items at the same time, the order of execution of these three items is as follows:
public class MyOptions{ public string Version { get; set; } = "v1";}public class MyOptionsSetup : IConfigureOptions, IPostConfigureOptions{ public void Configure(MyOptions options) { options.Version = "Configure"; } public void PostConfigure(string name, MyOptions options) { options.Version = "PostConfigure"; }}// StartUp.csspublic class StartUp{ public void ConfigureServices(IServiceCollection services) { //Register services.TryAddTransient(); services.TryAddTransient(); var options = new MyOptions(); //default options.Version = "v1" var serviceProvider = services.BuildServiceProvider(); //Read IConfigureOptions instance and call Configure() var configureOptions = serviceProvider.GetService(); configureOptions.Configure(options); //options.Version = "Configure-v1" //does nothing, just adds a delegate for configuration services.Configure( options=> options.Version = "v2" ); //options.Version = "v2" //Note: If IConfigureOptions.Configure() is called after service.Configure(), //will only execute options=> options.Version = "v2", //does not execute IConfigureOptions.Configure() !!! var configureOptions2 = serviceProvider.GetService(); configureOptions2.Configure(options); //options.Version = "v2" instead of "Configure-v1" //Read IPostConfigureOptions instance and call PostConfigure() var postConfigureOptions = serviceProvider.GetService(); postConfigureOptions.PostConfigure(options); //options.Version = "PostConfigure-v1" }} Conclusion
Execution order of configuration:
1. new Options() Default 2. IConfigureOptions.Configure( options ) [The premise is that it must be called before service.Configure, and it needs to be called manually] 3. service.Configure()4. IPostConfigureOptions( options ) It needs to be called manually
In summary:
As long as services.Configure is called, then calling IConfigureOptions.Configure is invalid, all of which execute the delegation or configuration provided by services.Configure.
IPostConfigureOptions.Configure can be triggered whenever called.
note
If you get IOptions directly, the delegate setupAction provided by serviceConfigure( setupAction ) is not executed, new Options() is executed first, then IConfigureConfigure() is executed, and finally IPostConfigureOptions.Configure() is executed, and it is triggered only when the IOptions service instance is first obtained.
other
IOptions updates its Value property in real time, i.e. MyOptions instance,
IOptionsSnapshot does not update in real time because it is just a snapshot
At this point, the study of "what is the execution order of ASP.NET Core configuration options" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!