欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

asp.net core 3.1学习笔记——启动执行顺序

程序员文章站 2022-07-14 16:51:30
...

一、准备环境:

1.开发工具:Microsoft Visual Studio Professional 2019 版本 16.4.4;

2.SDK: dotnet-sdk-3.1.101-win-x64;

二、创建项目,选择WebAPI:

项目结构如下:

asp.net core 3.1学习笔记——启动执行顺序

然后对里面每个对象方法添加对应名称的控制台输出语句,改造如下:

Program.cs 文件内容分别如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace NetCoreApi
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        #region 改造两组执行方法,对比启动执行顺序
        //1.第一组
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration(builder =>
                {
                    Console.WriteLine("ConfigureAppConfiguration");
                })
                .ConfigureServices(builder =>
                {
                    Console.WriteLine("ConfigureServices");
                })
                .ConfigureLogging(builder =>
                {
                    Console.WriteLine("ConfigureLogging");
                })
                .ConfigureHostConfiguration(builder =>
                {
                    Console.WriteLine("ConfigureHostConfiguration");
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    Console.WriteLine("ConfigureWebHostDefaults");
                    webBuilder.UseStartup<Startup>();
                });

        /*
         //2.第二组
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                Console.WriteLine("ConfigureWebHostDefaults");
                webBuilder.UseStartup<Startup>();
            })
            .ConfigureServices(builder => {
                Console.WriteLine("ConfigureServices");
            })
            .ConfigureLogging(builder => {
                Console.WriteLine("ConfigureLogging");
            })
            .ConfigureAppConfiguration(builder => {
                Console.WriteLine("ConfigureAppConfiguration");
            })
            .ConfigureHostConfiguration(builder => {
                Console.WriteLine("ConfigureHostConfiguration");
            });
            */ 
        #endregion
    }
}

Startup.cs 文件内容如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace NetCoreApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            Console.WriteLine("Startup");
        }

        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)
        {
            services.AddControllers();
            Console.WriteLine("Startup.ConfigureServices");
        }

        // 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.UseRouting();

            app.UseAuthorization();

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

对以上文件改造完毕, 两组对比(第一组和第二组)执行结果如下:

asp.net core 3.1学习笔记——启动执行顺序

 asp.net core 3.1学习笔记——启动执行顺序

 启动执行顺序流程如下:

asp.net core 3.1学习笔记——启动执行顺序

 最后附上极客时间的学习课程,.net core 的崛起,欢迎更多的小伙伴加入学习,构建更加繁荣的生态圈;

asp.net core 3.1学习笔记——启动执行顺序