.Net core 源码解析
启动代码
namespace SmapleWeb
{
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.UseStartup<Startup>();
});
}
}
将上述代码
CreateHostBuilder(args).Build().Run();
拆分成:
IHostBuilder builder= CreateHostBuilder(args); //产生一个IHostBuilder实例builder
IHost host=builder.Build();//产生一个Ihost实例host
host.Run();//开始运行web项目,这时候就由Console转换成Web项目
创建并配置主机Builder
拆解CreateHostBuilder
public static IHostBuilder CreateHostBuilder(string[] args) {
IHostBuilder builder = Host.CreateDefaultBuilder(args);//创建IHostBuilder实例
Action<IWebHostBuilder> configAction = delegate(IWebHostBuilder webBuilder){
webBuilder.UseStartup<Startup>();
};
builder = builder.ConfigureWebHostDefaults(configAction); //将IHostBuilder实例转变为web主机性质的IWebHostBuilder
return builder;
}
CreateDefaultBuilder分析
CreateDefaultBuilder主要用于创建IHostBuilder对象实例,有了这个实例才可以继续后续的加载更多的配置操作。通过IHostBuilder.Build()生产IHost实例。最终通过IHost.Run()运行项目。
Host类-用于产生初始的builder静态类
using Microsoft.Extensions.Hosting;
namespace Microsoft.Extensions.Hosting
{
public static class Host
{
public static IHostBuilder CreateDefaultBuilder();
public static IHostBuilder CreateDefaultBuilder(string[] args);
}
}
IHostBuilder转变成IWebHostBuilder
建立了IHostBuilder实例后,将通过builder.ConfigureWebHostDefaults(Action )方法将IHostBuilder实例转变为一个web主机性质的webbuilder(IWebHostBuilder)
Configures a IHostBuilder with defaults for hosting a web app.
public static Microsoft.Extensions.Hosting.IHostBuilder ConfigureWebHostDefaults (this Microsoft.Extensions.Hosting.IHostBuilder builder, Action<Microsoft.AspNetCore.Hosting.IWebHostBuilder> configure)
{
Action<IWebHostBuilder> webconfigure = delegate(IWebHostBuilder webHostBuilder)
{
WebHost.ConfigureWebDefaults(webHostBuilder); //配置Web默认值
configure(webHostBuilder);
};
return builder.ConfigureWebHost(webconfigure);
}
引用:
https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.generichostbuilderextensions.configurewebhostdefaults?view=aspnetcore-3.0
其实是利用IHostBuilder的另一个扩展方法ConfigureWebHost(Action)(此扩展法方定义在Microsoft.Extensions.Hosting.GenericHostWebHostBuilderExtensions静态类中)。
把IHostBuilder转变为IWebHostBuilder: builder.ConfigureWebHost(webconfigure)
# builder.ConfigureWebHost(webconfigure) Microsoft.Extensions.Hosting.GenericHostWebHostBuilderExtensions(GenericHostWebHostBuilderExtensions.cs)
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Internal;
namespace Microsoft.Extensions.Hosting
{
public static class GenericHostWebHostBuilderExtensions
{
public static IHostBuilder ConfigureWebHost(this IHostBuilder builder, Action<IWebHostBuilder> configure)
{
//实现在asp.net core 2.1 里IWebHostBuilder 在asp.net core 3.1里把泛型IHostBuilder生产webhost的builder的
var webhostBuilder = new GenericWebHostBuilder(builder);
configure(webhostBuilder);
return builder;
}
}
}
通过Microsoft.AspNetCore.Hosting.Internal.GenericWebHostBuilder类实例。GenericWebHostBuilder是一个内部类。这个类网上资源极少,甚至在官方文档里找不到说明,但可以参考其源码GenericWebHostBuilder.cs源代码。此类声明和其构造函数声明如下:
internal class GenericWebHostBuilder : IWebHostBuilder, ISupportsStartup, ISupportsUseDefaultServiceProvider
{
public GenericWebHostBuilder(IHostBuilder builder);
}
在传入了那么多层委托后,终于我们扒到底了,在实现了IHostBuilder转变为IWebHostBuilder后,所有config有关的委托终于得以执行,config具体包含的方法为
WebHost.ConfigureWebDefaults(webHostBuilder); //配置Web默认值
webBuilder.UseStartup();
WebHost.ConfigureWebDefualts(IWebHostBuilder)方法
此方法在Asp.net core 2.1中 是WebHost.CreateDefaultBuilder(args) 里最后调用的方法,而在Asp.net core 3.1中,把IWebHostBuilder该为泛型IHostBuilder后,在执行IHostBuilder.ConfigureWebHost()时回调委托执行。此方法作用类似与Host.CreateDefaultBuilder(args),使用预配置配置一些关于Web方式的参数。主要是注入一些web相关的服务,配置主机地址等。此方法没有官方文档说明,建议直接查看源码WebHost.cs源代码(参考文档5)。
webBuilder.UseStartup()方法
UseStartup()是webbuilder的扩展发放,定义在Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions静态类中(WebHostBuilderExtensions.cs),此方法申明如下
public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder, Type startupType)
从方法名就可以看出是使用Startup类,这个方法主要使用反射技术,反射Startup类,响应startup类中的配置的信息
总结
1.IHostBuilder builder = Host.CreateDefaultBuilder(args);
创建一个基础builder(读取app.json)
2.IWebHostBuilder webHostBuilder= new GenericWebHostBuilder(builder);
转换为webbuilder
3.WebHost.ConfigureWebDefaults(webHostBuilder);
给webbuilder使用预先的默认值配置
4.webBuilder.UseStartup<Startup>();
读取startup类配置信息
引用
asp.net core 3.1 入口:Program.cs中的Main函数
深入ASP.NET Core源代码之 - HOST