[.NET Core development of practical learning notes] StartUp: understand the startup program

Start recording Xiaowei Yu teacher learning courses developed practical learning [.NET Core], recording at each lesson summary form

Section StartUp: understand the startup program

Creating a ASP.NET Core Web project, you can see two core classes Program and StartUp, called CreateHostBuilder method in the Main method of the Program, and returns a IHostBuilder interface, which is the core interface carrying the entire project.

In CreateHostBuilder method, ConfigureWebHostDefaults method Build Host process calls another core classes StartUp,

In fact, all configuration can be set in the Program, call the other core classes StartUp better management code structure

In the whole process of implementation, execution of the method is performed in a certain order, such as the following code, and does not write sequential code to perform their own functions entrusted instead to a fixed order to perform

Program.CreateHostBuilder defined as follows

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureHostConfiguration(configurationBuilder =>
                {
                    Console.WriteLine("执行方法:ConfigureHostConfiguration");
                })
                .ConfigureServices(services =>
                {
                    Console.WriteLine("执行方法:ConfigureServices");
                })
                .ConfigureLogging(loggingBuilder =>
                {
                    Console.WriteLine ( " execution method: ConfigureLogging " ); 
                }) 
                .ConfigureAppConfiguration ((hostBuilderContext, configurationBinder) => 
                { 
                    Console.WriteLine ( " execution method: ConfigureAppConfiguration " ); 
                }) 
                .ConfigureWebHostDefaults (WebBuilder => 
                { 
                    Console.WriteLine ( " execution method: ConfigureWebHostDefaults " ); 
                    webBuilder.UseStartup <the Startup> (); 
                });

The method as defined StartUp

public Startup(IConfiguration configuration)
        {
            Console.WriteLine("执行方法:Startup");
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            Console.WriteLine("执行方法:Startup.ConfigureServices");
            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            Console.WriteLine("执行方法:Startup.Configure");
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

FIG execution results are as follows

 

Visible results of the implementation is carried out in a specific order, and will not call interface has changed the order of execution

 

This also shows that the order of execution, is replaced in the process CreateHostBuilder when using a third-party logging framework, rather than in the StartUp

Guess you like

Origin www.cnblogs.com/c-supreme/p/12459414.html