NetCore学习 启动代码顺序

1: 执行Program.cs  中的Main方法

 

 public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }

 

2:执行Startup.cs 中 Startup() 方法

  public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

            if (env.IsEnvironment("Development"))
            {
                // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
                builder.AddApplicationInsightsSettings(developerMode: true);
            }

            builder.AddEnvironmentVariables();
            Configuration = builder.Build();

            //初始化映射关系
            KwHisDataMapper.InitializeDto();

            log4netRepository = LogManager.CreateRepository("NETCoreRepository");
            XmlConfigurator.Configure(log4netRepository, new FileInfo("log4net.config"));

        }

 

 env.ContentRootPath: 当前根目录

 AddJsonFile:加载配置文件 appsettings.json

 $"appsettings.{env.EnvironmentName}.json"     大括号里面的为替换内容

 

3:执行执行Startup.cs 中 ConfigureServices() 方法

   public void ConfigureServices(IServiceCollection services)
        {
            //依赖注入
            AddDependencies(services);

            //添加数据上下文
            services.AddDbContext<KwHisDataDbContext>(options => options.UseMySql(Configuration.GetConnectionString("KwHisDataBase")));        
            services.ConfigureDapperMySqlConnection(Configuration.GetSection("ConnectionStrings"));

            // Add framework services.
            //  services.AddApplicationInsightsTelemetry(Configuration);

            services.AddMemoryCache();

            services.AddMvc();

            //string strRedisConfig= Configuration.GetConnectionString("RedisConnection");

            services.AddSingleton(typeof(ICacheService), new RedisCacheService(new RedisCacheOptions {

                Configuration = Configuration.GetConnectionString("RedisConnection"),
                InstanceName="HisData"

            },0));

            

            services.AddSwaggerGen();
            services.ConfigureSwaggerGen(options =>
            {
                options.SingleApiVersion(new Swashbuckle.Swagger.Model.Info
                {
                    Version = "v1",
                    Title = "Test",
                    Description = "RESTful API for Kw.HisDataAnalysis",
                    TermsOfService = "None",
                    Contact = new Contact { Name = "Team", Email = "xx@qq.com", Url = "www.xxx.com" }
                });
                options.IncludeXmlComments(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath,
                    "Test.xml")); // 注意:此处替换成所生成的XML documentation的文件名。
                options.DescribeAllEnumsAsStrings();
            });
          
        }

 

posted @ 2017-03-06 17:51  湘江油菜  阅读(705)  评论(0编辑  收藏  举报