若是作過Web以外開發的人,應該記得這個是標準的Console或者Winform的入口。爲何會這樣呢?
.NET Web Development and Tools Bloghtml
ASP.NET Core is a console app In RC1 an ASP.NET application was a class library that contained a Startup.cs class. When the DNX toolchain run your application ASP.NET hosting libraries would find and execute the Startup.cs, booting your web application. Whilst the spirit of this way of running an ASP.NET Core application still exists in RC2, it is somewhat different.
As of RC2 an ASP.NET Core application is a .NET Core Console application that calls into ASP.NET specific libraries. What this means for ASP.NET Core apps is that the code that used to live in the ASP.NET Hosting libraries and automatically run your startup.cs now lives inside a Program.cs. This alignment means that a single .NET toolchain can be used for both .NET Core Console applications and ASP.NET Core applications. It also means that customers have more obvious control over the code that hosts and runs their ASP.NET Core app:git
翻譯過來的意思大概是:
ASP.NET Core 在RC1的時候,是一個Console 應用程序,一個ASP.NET程序是一個包含有Startup.cs的類的類庫。
當DNX(.NET運行環境)運行你的ASP.NET程序,它將會嘗試去找到而且運行你的Stratup類,啓動你的網站程序。這種啓動方法任然在RC2中保留了下來,可是多少有些不一樣。
由於RC2是所謂的ASP.NET Core的應用程序,它是一種.NET Core 控制檯(Console)程序,他被一些ASP.NET特殊的類所調用。這就意味着 ASPNET Core 原來存活在ASP.NET宿主庫裏面,自動執行Startup.cs的機制有所變化。
RC2的ASPNET Core變成了存活在一個控制檯(Console)程序的程序,全部由Program.cs做爲調用的入口了。
這樣的話,整個.NET的工具鏈就變得內部執行機制統一了,便可以執行普通的 .NET Core Console 程序,也能夠執行ASP.NET Core程序。
對於開發者和用戶來講,也帶來了一些控制和管理上的靈活性。github
public class Program { public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<Startup>() .Build(); host.Run(); } }
這裏反覆強調了一個宿主的概念。原來的MVC5時代,宿主應該是IIS服務器。web
如今MVC6時代,宿主變成了一個普通的控制檯程序了。固然,Web程序畢竟是須要一個WebHost運行環境的。UseKestrel,UseIISIntegration就是這樣的環境。若是從Beta版開始關注的話,應該對於Kestrel不陌生,這個東西是一個迷你的HTTP服務器,可讓ASP程序HOST在上面運行。數據庫
注意:KestrelHttpServer,若是Baidu的話,通常出現這樣的結果。請使用完整的關鍵字進行查詢。json
Kestrel 是 Scala 的一個很是小的隊列系統,基於 starling。服務器
啓動的前三句,分別涉及到Kestrel服務器,IIS集成,ContentRoot(內容的根目錄,這裏多說一句,如今的工程,解決方案的概念開始弱化了,取而代之的是文件夾的概念。可能和Nodejs的興起有關。代碼的組織形式更加自由了)
第四句則是和StartUp有關。ide
這裏的StartUp雖然使用了泛型,可是若是看一下方法定義:TStartup只是約束爲class工具
// // Summary: // Specify the startup type to be used by the web host. // // Parameters: // hostBuilder: // The Microsoft.AspNetCore.Hosting.IWebHostBuilder to configure. // // Type parameters: // TStartup: // The type containing the startup methods for the application. // // Returns: // The Microsoft.AspNetCore.Hosting.IWebHostBuilder. public static IWebHostBuilder UseStartup<TStartup>(this IWebHostBuilder hostBuilder) where TStartup : class;
咱們來看一下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.IsDevelopment()) { // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709 builder.AddUserSecrets(); } builder.AddEnvironmentVariables(); Configuration = builder.Build(); }
SetBasePath,仍是要設定一下全局的路徑。還記得MVC5的時代,Server.Map方法,將虛擬路徑和實際路徑進行轉換,我猜想這裏也是爲了這樣的方法作準備。
AddJsonFile,這個就是將JSON文件格式的配置文件放入總的配置管理器中。
具體的配置問題,請參看下面這篇文章
(1-2)配置的升級 - ASP.NET從MVC5升級到MVC6
AddUserSecrets,這個是RC新的配置。
如下引用自 解讀ASP.NET 5 & MVC6系列(5):Configuration配置信息管理 更多UserSecrets,請閱讀原文。
敏感信息配置(RC版新增功能)
在RC版發佈之後,微軟又新增了一種敏感信息配置實現,程序集爲Microsoft.Framework.ConfigurationModel.UserSecrets,經過該程序集的管理,咱們能夠將敏感的配置信息放在計算機的特殊目錄下的secrets.json文件,其目錄定義規則以下:
Windows: %APPDATA%\microsoft\UserSecrets\\secrets.json
Linux: ~/.microsoft/usersecrets/\secrets.json
Mac: ~/.microsoft/usersecrets/\secrets.json
AddEnvironmentVariables:能夠將操做系統的環境變量添加到配置系統中,同時你也能夠對這些環境變量進行自定義
除了StartUp方法,下面兩個方法也是被系統(Runtime)自動調用的。
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
和整個系統相關的,有兩件事情,一個是系統提供什麼服務,固然這裏是你能夠自定義的,還有一件事情是,系統要怎麼作LOG日誌。
雖然這樣說並不能很好的解釋上面兩個方法,可是大概就是這麼回事。固然配置裏面日誌只是一個項目,MVC路由也能夠做爲配置項寫在Configure裏面的。
打開WebHostBuilder的源代碼:這裏就關心兩件事情Service和Logger:服務和日誌
(小知識,階層型的配置文件,如今用冒號表示階層關係了)
namespace Microsoft.AspNetCore.Hosting { /// <summary> /// A builder for <see cref="IWebHost"/> /// </summary> public class WebHostBuilder : IWebHostBuilder { private readonly IHostingEnvironment _hostingEnvironment; private readonly List<Action<IServiceCollection>> _configureServicesDelegates; private readonly List<Action<ILoggerFactory>> _configureLoggingDelegates; private IConfiguration _config = new ConfigurationBuilder().AddInMemoryCollection().Build(); private ILoggerFactory _loggerFactory; private WebHostOptions _options; /// <summary> /// Initializes a new instance of the <see cref="WebHostBuilder"/> class. /// </summary> public WebHostBuilder() { _hostingEnvironment = new HostingEnvironment(); _configureServicesDelegates = new List<Action<IServiceCollection>>(); _configureLoggingDelegates = new List<Action<ILoggerFactory>>(); // This may end up storing null, but that's indistinguishable from not adding it. UseSetting(WebHostDefaults.EnvironmentKey, Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") // Legacy keys, never remove these. ?? Environment.GetEnvironmentVariable("Hosting:Environment") ?? Environment.GetEnvironmentVariable("ASPNET_ENV")); if (Environment.GetEnvironmentVariable("Hosting:Environment") != null) { Console.WriteLine("The environment variable 'Hosting:Environment' is obsolete and has been replaced with 'ASPNETCORE_ENVIRONMENT'"); } if (Environment.GetEnvironmentVariable("ASPNET_ENV") != null) { Console.WriteLine("The environment variable 'ASPNET_ENV' is obsolete and has been replaced with 'ASPNETCORE_ENVIRONMENT'"); } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddIdentity<ApplicationUser, IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); services.AddMvc(); // Add application services. services.AddTransient<IEmailSender, AuthMessageSender>(); services.AddTransient<ISmsSender, AuthMessageSender>(); }
這裏顧名思義:
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); app.UseBrowserLink(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseIdentity(); // Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715 app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
還有一個問題是系統是如何得到EnvironmentName的。
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; using Microsoft.AspNet.FileSystems; using Microsoft.Framework.Runtime; namespace Microsoft.AspNet.Hosting { public class HostingEnvironment : IHostingEnvironment { private const string DefaultEnvironmentName = "Development"; public HostingEnvironment(IApplicationEnvironment appEnvironment, IEnumerable<IConfigureHostingEnvironment> configures) { EnvironmentName = DefaultEnvironmentName; WebRoot = HostingUtilities.GetWebRoot(appEnvironment.ApplicationBasePath); WebRootFileSystem = new PhysicalFileSystem(WebRoot); foreach (var configure in configures) { configure.Configure(this); } } public string EnvironmentName { get; set; } public string WebRoot { get; private set; } public IFileSystem WebRootFileSystem { get; set; } } }
經過源代碼咱們知道有一個默認的「Development」
咱們如何設定這個EnvironmentName?
stackoverflow上面的回答:
how-to-set-ihostingenvironment-environmentname-in-vnext-application
這裏使用了直接設定的方法(如下代碼段並不是針對 .NETCore RC2)
public Startup(IHostingEnvironment env) { // Setup configuration sources. Configuration = new Configuration() .AddJsonFile("config.json").AddEnvironmentVariables(); Configuration.Set("ASPNET_ENV","Your own value"); }
估計也能夠寫在Config的文件中。
<configuration> <system.webServer> <handlers> <add name="httpplatformhandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" /> </handlers> <httpPlatform processPath="..\approot\web.cmd" arguments="--ASPNET_ENV Development" stdoutLogEnabled="false" stdoutLogFile="..\logs\stdout.log" startupTimeLimit="3600"></httpPlatform> </system.webServer> </configuration>
固然若是你去看最新版的代碼,這裏的環境變量關鍵字變成了 「ASPNETCORE_ENVIRONMENT」
(Hosting:Environment 和 ASPNET_ENV 都是舊的關鍵字了)
/// <summary> /// Initializes a new instance of the <see cref="WebHostBuilder"/> class. /// </summary> public WebHostBuilder() { _hostingEnvironment = new HostingEnvironment(); _configureServicesDelegates = new List<Action<IServiceCollection>>(); _configureLoggingDelegates = new List<Action<ILoggerFactory>>(); // This may end up storing null, but that's indistinguishable from not adding it. UseSetting(WebHostDefaults.EnvironmentKey, Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") // Legacy keys, never remove these. ?? Environment.GetEnvironmentVariable("Hosting:Environment") ?? Environment.GetEnvironmentVariable("ASPNET_ENV")); if (Environment.GetEnvironmentVariable("Hosting:Environment") != null) { Console.WriteLine("The environment variable 'Hosting:Environment' is obsolete and has been replaced with 'ASPNETCORE_ENVIRONMENT'"); } if (Environment.GetEnvironmentVariable("ASPNET_ENV") != null) { Console.WriteLine("The environment variable 'ASPNET_ENV' is obsolete and has been replaced with 'ASPNETCORE_ENVIRONMENT'"); } }