_configureServicesDelegates的承接
在【ASP.NET Core[源码分析篇] - Startup】这篇文章中,我们得知了目前为止(UseStartup),所有的动作都是在_configureServicesDelegates里面添加了注册的委托,那么系统是什么时候执行这些委托完成注册的呢?
真正的注册通过之前的一系列眼花缭乱的操作,我们得到了所有需要注册的委托_configureServicesDelegates,我们看一下WebHostBuilder.Build如何实现真正的注册。
WebHostBuilder.Build()public IWebHost Build() { if (this._webHostBuilt) throw new InvalidOperationException(Resources.WebHostBuilder_SingleInstance); this._webHostBuilt = true; AggregateException hostingStartupErrors; IServiceCollection serviceCollection1 = this.BuildCommonServices(out hostingStartupErrors); IServiceCollection serviceCollection2 = serviceCollection1.Clone(); IServiceProvider providerFromFactory = GetProviderFromFactory(serviceCollection1); ..... WebHost webHost = new WebHost(serviceCollection2, providerFromFactory, this._options, this._config, hostingStartupErrors); try { webHost.Initialize(); return (IWebHost) webHost; } catch { webHost.Dispose(); throw; } IServiceProvider GetProviderFromFactory(IServiceCollection collection) { ServiceProvider serviceProvider = collection.BuildServiceProvider(); IServiceProviderFactory<IServiceCollection> service = ((IServiceProvider) serviceProvider).GetService<IServiceProviderFactory<IServiceCollection>>(); if (service == null) return (IServiceProvider) serviceProvider; using (serviceProvider) return service.CreateServiceProvider(service.CreateBuilder(collection)); } }