Asp.netCore 的Startup 不继承接口
2019-12-03 17:14 qgbo 阅读(308) 评论(0) 编辑 收藏 举报有一个问题: Asp.netCore 的Startup 要实现 Config 和ConfigServie 方法, 为什么不接口约束呢。
进入源码:
// // 摘要: // /// Specify the startup type to be used by the web host. /// // // 参数: // hostBuilder: // The Microsoft.AspNetCore.Hosting.IWebHostBuilder to configure. // // startupType: // The System.Type to be used. // // 返回结果: // The Microsoft.AspNetCore.Hosting.IWebHostBuilder. public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder, Type startupType) { string name = startupType.GetTypeInfo().Assembly.GetName().Name; return hostBuilder.UseSetting(WebHostDefaults.ApplicationKey, name).ConfigureServices(delegate (IServiceCollection services) { if (IntrospectionExtensions.GetTypeInfo(typeof(IStartup)).IsAssignableFrom(startupType.GetTypeInfo())) { services.AddSingleton(typeof(IStartup), startupType); } else { services.AddSingleton(typeof(IStartup), delegate (IServiceProvider sp) { IHostingEnvironment requiredService = sp.GetRequiredService<IHostingEnvironment>(); return new ConventionBasedStartup(StartupLoader.LoadMethods(sp, startupType, requiredService.EnvironmentName)); }); } }); }
这里会判断这个StartUp 是否有IStartUp 约束。
这在后面会创建IStartUp 的。这里设置了依赖注入。
没有约束,会根据环境给这个类增加东西的。
气功波(18037675651)