首页 > Web开发 > 详细

Asp.netCore 的Startup 不继承接口

时间:2019-12-03 17:50:46      阅读:77      评论:0      收藏: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 的。这里设置了依赖注入。

没有约束,会根据环境给这个类增加东西的。

 

Asp.netCore 的Startup 不继承接口

原文:https://www.cnblogs.com/qgbo/p/11978109.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!