Asp.Net Core 3.1+ 启动过程
Net Core 启动过程
新建一个新的 Core3.1 项目(注意,不要创建 Net 6.x);
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
Console.WriteLine("ConfigureWebHostDefaults");
webBuilder.UseStartup<Startup>();
});
}
CreateHostBuilder 方法返回 IHostBuilder 类型,看看这个类型里面有什么:
有 6 个方法,我们需要关注的是橙色圈出来的那 3 个;
他们的执行顺序是怎样的?
我们修改 Program.cs 如下,同样,将 console输出 添加到 Startup.cs 中:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(config =>
{
Console.WriteLine("ConfigureAppConfiguration"); // 3
})
.ConfigureHostConfiguration(build =>
{
Console.WriteLine("ConfigureHostConfiguration"); // 2
})
.ConfigureServices(services =>
{
Console.WriteLine("ConfigureServices"); // 4
})
.ConfigureWebHostDefaults(webBuilder =>
{
Console.WriteLine("ConfigureWebHostDefaults"); // 1
webBuilder.UseStartup<Startup>();
});
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Console.WriteLine("ctor 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();
});
}
}
运行输出:
其实,Net Core 在运行中,其执行顺序如下图所示:
- ConfigureWebHostDefaults:注册我们应用程序必要的几个组件(如:配置组件、容器组件等)
- ConfigureHostConfiguration:配置我们应用程序启动时必要的配置(如:监听的端口、监听URL地址,同样也能嵌入我们自己的配置内容注入到我们的配置框架里面去)
- ConfigureAppConfiguration:嵌入我们自己的配置文件供应用程序来读取,这些配置文件将来就会在后续的应用程序执行过程中间每个组件读取;
- 下一组:它们都是用来往容器里面注入我们的应用的组件
- 最后一个:用来注入我们的中间件,处理HttpContext整个请求过程的;
Starup.cs 类文件不是必要的
我们可以将 Starup 里面的内容放到 Programs.cs 中去,项目也能正常运行,如下:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureServices(services =>
{
Console.WriteLine("Startup.ConfigureServices"); // 6
services.AddControllers();
});
webBuilder.Configure(app =>
{
Console.WriteLine("Startup.Configure"); // 7
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
});
Console.WriteLine("ConfigureWebHostDefaults"); // 1
//webBuilder.UseStartup<Startup>();
});
}