NetCore2.0Web应用之Startup
生活随笔
收集整理的這篇文章主要介紹了
NetCore2.0Web应用之Startup
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>> ??
作為main函數的程序啟動文件UseStartup 默認就是調用我們的整個應用程序的啟動文件 class Program{static void Main(string[] args){var host = new WebHostBuilder().UseKestrel() // 指定WebServer為Kestrel.UseStartup<StartUpB>() // 配置WebHost.Build();host.Run(); // 啟動WebHost}} UseStartup?首先這是IWebHostBuilder接口的擴展類,這里有兩個分支
1、如果StartUp從IStartup繼承,則直接以單例的方式加入插件服務框架中。
2、如果不是從IStartup繼承,則包裝為IStartup后,再以單例的方式加入插件服務框架中。
public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder, Type startupType){var startupAssemblyName = startupType.GetTypeInfo().Assembly.GetName().Name;return hostBuilder.UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName).ConfigureServices(services =>{if (typeof(IStartup).GetTypeInfo().IsAssignableFrom(startupType.GetTypeInfo())){services.AddSingleton(typeof(IStartup), startupType);}else{services.AddSingleton(typeof(IStartup), sp =>{var hostingEnvironment = sp.GetRequiredService<IHostingEnvironment>();ConventionBasedStartup類正是繼承了IStartup。 LoadMethods 內部調用FindConfigureDelegate 就是為了找到 Configure{0}此方法 public void Configure(IApplicationBuilder app){ }return new ConventionBasedStartup(StartupLoader.LoadMethods(sp, startupType, hostingEnvironment.EnvironmentName));});}});} public class ConventionBasedStartup : IStartup{private readonly StartupMethods _methods;public ConventionBasedStartup(StartupMethods methods){_methods = methods;}public void Configure(IApplicationBuilder app){try{_methods.ConfigureDelegate(app);}catch (Exception ex){if (ex is TargetInvocationException){ExceptionDispatchInfo.Capture(ex.InnerException).Throw();}throw;}}public IServiceProvider ConfigureServices(IServiceCollection services){try{return _methods.ConfigureServicesDelegate(services);}catch (Exception ex){if (ex is TargetInvocationException){ExceptionDispatchInfo.Capture(ex.InnerException).Throw();}throw;}}}?
public static StartupMethods LoadMethods(IServiceProvider hostingServiceProvider, Type startupType, string environmentName){var configureMethod = FindConfigureDelegate(startupType, environmentName);var servicesMethod = FindConfigureServicesDelegate(startupType, environmentName);object instance = null;if (!configureMethod.MethodInfo.IsStatic || (servicesMethod != null && !servicesMethod.MethodInfo.IsStatic)){instance = ActivatorUtilities.GetServiceOrCreateInstance(hostingServiceProvider, startupType);}Func<IServiceCollection, IServiceProvider> configureServices = services =>{ return services.BuildServiceProvider();};return new StartupMethods(instance, configureMethod.Build(instance), configureServices);}?
private static ConfigureBuilder FindConfigureDelegate(Type startupType, string environmentName){var configureMethod = FindMethod(startupType, "Configure{0}", environmentName, typeof(void), required: true);return new ConfigureBuilder(configureMethod);} 這個是源碼實現的了一個IStartup 但是在默認的項目中并沒有使用這個 正常情況下我們繼承StartupBase 此抽象類 實現 Configure(IApplicationBuilder app) 方法就可以了 public abstract class StartupBase : IStartup{public abstract void Configure(IApplicationBuilder app);IServiceProvider IStartup.ConfigureServices(IServiceCollection services){ConfigureServices(services);return CreateServiceProvider(services);}public virtual void ConfigureServices(IServiceCollection services){}public virtual IServiceProvider CreateServiceProvider(IServiceCollection services){return services.BuildServiceProvider();}}?
總結最終情況就是:我們的應用程序要啟動文件必須滿足一下方式就可以了
1、自己定義個類,必須包含Configure方法
2、繼承自IStartup,實現所有方法
3、繼承自StartupBase抽象類,只需要實現Configure方法
轉載于:https://my.oschina.net/stuyun/blog/3014524
總結
以上是生活随笔為你收集整理的NetCore2.0Web应用之Startup的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Git vs SVN
- 下一篇: Hadoop----hdfs的基本操作