ASP.NET Core[源码分析篇] - Startup
应用启动的重要类 - Startup
在ASP.NET Core - 从Program和Startup开始这篇文章里面,我们知道了Startup这个类的重要性,它主要负责了:
- 配置应用需要的服务(服务注册,ConfigureServices方法)。
- 创建应用的请求处理处理管道(Configure方法)。
在源码分析之前补充一点,虽然我们一般是按约定把这个类名定义成了Startup,但是在真正应用中,我们不是必须要命名为Startup的,这只是一个抽象概念,我们可以命名其他的类名,只需要在UseStartup/UseStartup<TStartup>中显式注册这个启动类即可,系统会把这个启动类注册为单例,例如:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
} public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<YourStartupClass>()
.Build();
} public class YourStartupClass
{
public void ConfigureService(IServiceCollection services)
{
} public void Configure(IApplicationBuilder app)
{
}
}
Startup是如何被注册进来的?
从前面我们可以看到Startup是在UseStartup方法里面被引用进来,我们先看一下UseStartup是如何把Startup类注册进来的
/// <summary>Specify the startup type to be used by the web host.</summary>
/// <param name="hostBuilder">The <see cref="T:Microsoft.AspNetCore.Hosting.IWebHostBuilder" /> to configure.</param>
/// <param name="startupType">The <see cref="T:System.Type" /> to be used.</param>
/// <returns>The <see cref="T:Microsoft.AspNetCore.Hosting.IWebHostBuilder" />.</returns>
public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder,Type startupType)
{
string name = startupType.GetTypeInfo().Assembly.GetName().Name;
return hostBuilder.UseSetting(WebHostDefaults.ApplicationKey, name).ConfigureServices((Action<IServiceCollection>) (services =>
{
if (typeof (IStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo()))
ServiceCollectionServiceExtensions.AddSingleton(services, typeof (IStartup), startupType);
else
ServiceCollectionServiceExtensions.AddSingleton(services, typeof (IStartup), (Func<IServiceProvider, object>) (sp =>
{
IHostingEnvironment requiredService = sp.GetRequiredService<IHostingEnvironment>();
return (object) new ConventionBasedStartup(StartupLoader.LoadMethods(sp, startupType, requiredService.EnvironmentName));
}));
}));
} /// <summary>Specify the startup type to be used by the web host.</summary>
/// <param name="hostBuilder">The <see cref="T:Microsoft.AspNetCore.Hosting.IWebHostBuilder" /> to configure.</param>
/// <typeparam name="TStartup">The type containing the startup methods for the application.</typeparam>
/// <returns>The <see cref="T:Microsoft.AspNetCore.Hosting.IWebHostBuilder" />.</returns>
public static IWebHostBuilder UseStartup<TStartup>(this IWebHostBuilder hostBuilder)
where TStartup : class
{
return hostBuilder.UseStartup(typeof (TStartup));
}
_configureServicesDelegates
从上面代码我们可以看出,这里主要是调用了WebHostBuilder的ConfigureServices方法,我们看一下ConfigureServices做了什么
public IWebHostBuilder ConfigureServices( Action<IServiceCollection> configureServices)
{
if (configureServices == null)
throw new ArgumentNullException(nameof (configureServices));
return this.ConfigureServices((Action<WebHostBuilderContext, IServiceCollection>) ((_, services) => configureServices(services)));
} /// <summary>
/// Adds a delegate for configuring additional services for the host or web application. This may be called
/// multiple times.
/// </summary>
/// <param name="configureServices">A delegate for configuring the <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" />.</param>
/// <returns>The <see cref="T:Microsoft.AspNetCore.Hosting.IWebHostBuilder" />.</returns>
public IWebHostBuilder ConfigureServices( Action<WebHostBuilderContext, IServiceCollection> configureServices)
{
if (configureServices == null)
throw new ArgumentNullException(nameof (configureServices));
this._configureServicesDelegates.Add(configureServices);
return (IWebHostBuilder) this;
}
这里主要是把委托添加到_configureServicesDelegates列表里面,这个_configureServicesDelegates有什么用呢?这个属性是一个非常重要的承载角色,在后面的WebHost真正调用Build方法时,我们再详细讲解。
再次看回UseStartup,这里调用了WebHostBuilder的ConfigureServices并向_configureServicesDelegates注册了一个委托,我们看一下这个委托的实体,这里面有两个分支:
1. Startup实现IStartup接口
直接注册该Startup为单例。从这里看出,其实我们的Startup类还有另一种方式实现的,就是直接实现IStartup接口。(其实还有一种是继承StartupBase)
2. Startup没实现IStartup接口
注册类型为ConventionBasedStartup的Startup类型
public class ConventionBasedStartup : IStartup
{
private readonly StartupMethods _methods; public ConventionBasedStartup(StartupMethods methods)
{
this._methods = methods;
} public void Configure(IApplicationBuilder app)
{
try
{
this._methods.ConfigureDelegate(app);
}
catch (Exception ex)
{
if (ex is TargetInvocationException)
ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
throw;
}
} public IServiceProvider ConfigureServices(IServiceCollection services)
{
try
{
return this._methods.ConfigureServicesDelegate(services);
}
catch (Exception ex)
{
if (ex is TargetInvocationException)
ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
throw;
}
}
}
注意这个ConventionBasedStartup其实也是实现了IStartup接口,ConventionBasedStartup对象是根据一个StartupMethods对象创建的,我们来看一下这个StartupMethods类型的定义
public class StartupMethods
{
public StartupMethods(object instance,Action<IApplicationBuilder> configure, Func<IServiceCollection, IServiceProvider> configureServices)
{
this.StartupInstance = instance;
this.ConfigureDelegate = configure;
this.ConfigureServicesDelegate = configureServices;
} public object StartupInstance { get; } public Func<IServiceCollection, IServiceProvider> ConfigureServicesDelegate { get; } public Action<IApplicationBuilder> ConfigureDelegate { get; }
}
StartupMethods只提供两个注册服务和中间件的方法,这两个方法体现在由它的两个属性(ConfigureServicesDelegate和ConfigureDelegate)提供的两个委托对象。
在我们UseStartup代码里面,是通过StartupLoader.LoadMethods基于Startup类型获取到一个StartupMethods
public class StartupLoader
{
public static StartupMethods LoadMethods(IServiceProvider hostingServiceProvider,Type startupType,string environmentName)
{
ConfigureBuilder configureDelegate = StartupLoader.FindConfigureDelegate(startupType, environmentName);
ConfigureServicesBuilder servicesDelegate = StartupLoader.FindConfigureServicesDelegate(startupType, environmentName);
ConfigureContainerBuilder containerDelegate = StartupLoader.FindConfigureContainerDelegate(startupType, environmentName);
object instance1 = (object) null;
if (!configureDelegate.MethodInfo.IsStatic || servicesDelegate != null && !servicesDelegate.MethodInfo.IsStatic)
instance1 = ActivatorUtilities.GetServiceOrCreateInstance(hostingServiceProvider, startupType);
StartupLoader.ConfigureServicesDelegateBuilder instance2 = (StartupLoader.ConfigureServicesDelegateBuilder) Activator.CreateInstance(typeof (StartupLoader.ConfigureServicesDelegateBuilder<>).MakeGenericType(containerDelegate.MethodInfo != (MethodInfo) null ? containerDelegate.GetContainerType() : typeof (object)), (object) hostingServiceProvider, (object) servicesDelegate, (object) containerDelegate, instance1);
return new StartupMethods(instance1, configureDelegate.Build(instance1), instance2.Build());
} private static ConfigureBuilder FindConfigureDelegate(Type startupType, string environmentName)
{
return new ConfigureBuilder(StartupLoader.FindMethod(startupType, "Configure{0}", environmentName, typeof (void), true));
} private static ConfigureContainerBuilder FindConfigureContainerDelegate(Type startupType, string environmentName)
{
return new ConfigureContainerBuilder(StartupLoader.FindMethod(startupType, "Configure{0}Container", environmentName, typeof (void), false));
} private static ConfigureServicesBuilder FindConfigureServicesDelegate( Type startupType,string environmentName)
{
MethodInfo method = StartupLoader.FindMethod(startupType, "Configure{0}Services", environmentName, typeof (IServiceProvider), false);
if ((object) method == null)
method = StartupLoader.FindMethod(startupType, "Configure{0}Services", environmentName, typeof (void), false);
return new ConfigureServicesBuilder(method);
} private static MethodInfo FindMethod( Type startupType,string methodName,string environmentName,Type returnType = null, bool required = true)
{
string methodNameWithEnv = string.Format((IFormatProvider) CultureInfo.InvariantCulture, methodName, (object) environmentName);
string methodNameWithNoEnv = string.Format((IFormatProvider) CultureInfo.InvariantCulture, methodName, (object) "");
MethodInfo[] methods = startupType.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
List<MethodInfo> list = ((IEnumerable<MethodInfo>) methods).Where<MethodInfo>((Func<MethodInfo, bool>) (method => method.Name.Equals(methodNameWithEnv, StringComparison.OrdinalIgnoreCase))).ToList<MethodInfo>();
if (list.Count > )
throw new InvalidOperationException(string.Format("Having multiple overloads of method '{0}' is not supported.", (object) methodNameWithEnv));
if (list.Count == )
{
list = ((IEnumerable<MethodInfo>) methods).Where<MethodInfo>((Func<MethodInfo, bool>) (method => method.Name.Equals(methodNameWithNoEnv, StringComparison.OrdinalIgnoreCase))).ToList<MethodInfo>();
if (list.Count > )
throw new InvalidOperationException(string.Format("Having multiple overloads of method '{0}' is not supported.", (object) methodNameWithNoEnv));
}
MethodInfo methodInfo = list.FirstOrDefault<MethodInfo>();
if (methodInfo == (MethodInfo) null)
{
if (required)
throw new InvalidOperationException(string.Format("A public method named '{0}' or '{1}' could not be found in the '{2}' type.", (object) methodNameWithEnv, (object) methodNameWithNoEnv, (object) startupType.FullName));
return (MethodInfo) null;
}
if (!(returnType != (Type) null) || !(methodInfo.ReturnType != returnType))
return methodInfo;
if (required)
throw new InvalidOperationException(string.Format("The '{0}' method in the type '{1}' must have a return type of '{2}'.", (object) methodInfo.Name, (object) startupType.FullName, (object) returnType.Name));
return (MethodInfo) null;
} }
这里面主要是通过反射获取startupType里面的Configure和ConfigureServices作为参数赋值给StartupMethods的ConfigureDelegate和ConfigureServicesDelegate,这样一个完整的ConventionBasedStartup类型的startup就被注册为单例了。
优先级
比较有意思的是,我们可以看到,Startup类中的这两个方法除了可以命名为ConfigureServices和Configure之外,它们还可以携带运行环境名称,具体采用的格式分别为Configure{EnvironmentName}Services和Configure{EnvironmentName},后者具有更高的选择优先级。
注意到FindConfigureServicesDelegate这个方法的实现,一般来说,ConfigureServices/Configure{EnvironmentName}Services这个方法不具有返回值(返回类型为void),但是它也可以定义成一个返回类型为IServiceProvider的方法。如果这个方法返回一个ServiceProvider对象,后续过程中获取的所有服务将从这个ServiceProvider中提取。这个返回的ServiceProvider对我们后续的一个注册是非常有用的,具体在【ASP.NET Core - 利用Windsor Castle实现通用注册】这篇文章中提到,我们基于返回的IServiceProvider进行容器替换进而实现通用注册,对于没有返回值的情况,系统会根据当前注册的服务创建一个ServiceProvider。
真正的注册
注意到此为止,程序还是没有执行做一个真正的注册,因为我们只是往_configureServicesDelegates添加了委托而已,并没有执行,这个的执行是在WebHost真正调用Build方法时。
其实CreateDefaultBuilder方法中的其他几个UseXXX(UserUrl,UseKestrel等)扩展方法也是同样的道理,把对应需要注册的Action委托同样写入了configureServicesDelegates
总结
上面的这个过程,我们看到其实包含很多隐含的逻辑,这些逻辑的理解,可以提供多种选项让我们在后面的真正开发中能够根据自己项目的一个实际情况进行选择,到此为止,我们现在能看到的最重要的一个点是:_configureServicesDelegates的一个承载作用。
ASP.NET Core[源码分析篇] - Startup的更多相关文章
- ASP.NET Core[源码分析篇] - WebHost
_configureServicesDelegates的承接 在[ASP.NET Core[源码分析篇] - Startup]这篇文章中,我们得知了目前为止(UseStartup),所有的动作都是在_ ...
- ASP.NET Core[源码分析篇] - Authentication认证
原文:ASP.NET Core[源码分析篇] - Authentication认证 追本溯源,从使用开始 首先看一下我们通常是如何使用微软自带的认证,一般在Startup里面配置我们所需的依赖认证服务 ...
- ASP.NET Core[源码分析篇] - 认证
追本溯源,从使用开始 首先看一下我们的通常是如何使用微软自带的认证,一般在Startup里面配置我们所需的依赖认证服务,这里通过JWT的认证方式讲解 public void ConfigureServ ...
- 一个由正则表达式引发的血案 vs2017使用rdlc实现批量打印 vs2017使用rdlc [asp.net core 源码分析] 01 - Session SignalR sql for xml path用法 MemCahe C# 操作Excel图形——绘制、读取、隐藏、删除图形 IOC,DIP,DI,IoC容器
1. 血案由来 近期我在为Lazada卖家中心做一个自助注册的项目,其中的shop name校验规则较为复杂,要求:1. 英文字母大小写2. 数字3. 越南文4. 一些特殊字符,如“&”,“- ...
- [asp.net core 源码分析] 01 - Session
1.Session文档介绍 毋庸置疑学习.Net core最好的方法之一就是学习微软.Net core的官方文档:https://docs.microsoft.com/zh-cn/aspnet/cor ...
- ASP.NET Core源码学习(一)Hosting
ASP.NET Core源码的学习,我们从Hosting开始, Hosting的GitHub地址为:https://github.com/aspnet/Hosting.git 朋友们可以从以上链接克隆 ...
- ASP.NET MVC 源码分析(一)
ASP.NET MVC 源码分析(一) 直接上图: 我们先来看Core的设计: 从项目结构来看,asp.net.mvc.core有以下目录: ActionConstraints:action限制相关 ...
- DOTNET CORE源码分析之IOC容器结果获取内容补充
补充一下ServiceProvider的内容 可能上一篇文章DOTNET CORE源码分析之IServiceProvider.ServiceProvider.IServiceProviderEngin ...
- ASP.NET MVC源码分析
MVC4 源码分析(Visual studio 2012/2013) HttpModule中重要的UrlRoutingModule 9:this.OnApplicationPostResolveReq ...
随机推荐
- 开启Qt Lite Project
发布: http://blog.qt.io/blog/2017/01/23/qt-5-8-released/ 说法一:Qt Lite” is not a tool, but a concept tha ...
- Delphi 7学习开发控件(继承TGraphicControl只画一条线)
我们知道使用Delphi快速开发,很大的一方面就是其强大的VCL控件,另外丰富的第三方控件也使得Delphi程序员更加快速的开发出所需要的程序.在此不特别介绍一些概念,只记录自己学习开发控件的步骤.假 ...
- OpenSSL包括了8个功能
什么是OpenSSL 众多的密码算法.公钥基础设施标准以及SSL协议,或许这些有趣的功能会让你产生实现所有这些 算法和标准的想法.果真如此,在对你表示敬佩的同时,还是忍不住提醒你:这是一个令人望而生畏 ...
- Kafka Topic的详细信息 捎带主要的安装步骤
1. 安装步骤 Kafka伪分布式安装的思路跟Zookeeper的伪分布式安装思路完全一样,不过比Zookeeper稍微简单些(不需要创建myid文件), 主要是针对每个Kafka服务器配置一个单独的 ...
- git实战经验(很实用)
推荐学习git很好的网站https://www.breakyizhan.com/git/216.html 以下内容,虽然编排不行,但是请认真的读下去,都是自己平时请自操作的,放心使用. 这是个人的gi ...
- Linux 中文设置
命令 locale,查看Linux默认系统语言 [root@VM_0_15_centos /]# locale LANG=en_US.utf8 LC_CTYPE="en_US.utf8&q ...
- 先订一个小目标,把微信小程序搞懂
目标:系统性学习微信小程序. 第一阶段:熟悉微信小程序及前端开发(结合web前端学习) 第二阶段:了解设计及后端 第三阶段:学习后端.
- Linux运维工程师学习成长路线
不过大家的留言都很精彩,希望大家也可以去留言区逛一逛~~ 好在这不是最后一期送书,之前已经有了好多活动,小编一定继续为大家多送些福利. 希望大家可以一如既往的关注脚本之家,支持爱你们的小编,共同进步! ...
- Python开发【第八篇】: 网络编程
内容概要 楔子 软件开发架构 网络基础 套接字(socket) 粘包 socketserver模块 一. 楔子 现在有两个python文件a.py和b.py,分别运行,这两个程序之间需要传递一个数据, ...
- sql-实现select取行号、分组后在分组内排序、每个分组中的前n条数据
表结构设计: 实现select取行号 sql局部变量的2种方式 set @name='cm3333f'; select @id:=1; 区别:set 可以用=号赋值,而select 不行,必须使用:= ...