net core体系-web应用程序-3项目结构、配置文件详解
一、应用程序文件结构
如下图所示,相比于Asp.Net项目,在新建的Asp.Net Core项目中,没有了Global.asax以及Web.config这样的文件,但多了几个其他主要的文件,它们分别为:wwwroot、appsetting.json、Program.cs、Startup.cs
![](http://upload-images.jianshu.io/upload_images/9068018-131cc1de31f3b32f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/302/format/webp)
wwwroot:
首先,Razor Pages项目中多了一个wwwroot的文件夹,这个文件夹中,主要存放网站的静态资源,如css,网站图片资源文件,js文件,三方的js库, 网站的图标等。
appsetting.json
appsetting.json是应用程序配置文件,类似于ASP.NET MVC应用程序中的Web.config配置文件。
Program.cs
这是.NET Core的程序入口文件,包含Main函数。
Startup.cs
这是 .NET Core应用程序启动配置项目文件
二、程序启动
1.Program.cs
ASP.NET Core应用程序需要由Host(宿主)进行管理,宿主为其提供运行环境并负责启动。所以Main函数主要是用来初始化宿主环境,而宿主环境的初始化需要借助WebHostBuilder。初始化完毕后,调用Run()方法来启动应用程序。
1.WebHost.CreaateDefaultBuilder():创建WebHostBuilder。
2.UseStartup<Startup>():指定启动类,用于依赖注入和中间件注册。
3.Build():返回初始化完毕的IWebHost宿主。
4.Run():启动WebHost。
![](http://upload-images.jianshu.io/upload_images/9068018-8c772ec3b2553433.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/696/format/webp)
CreateDefaultBuilder():
![](http://upload-images.jianshu.io/upload_images/9068018-3b04d11b58ee8fc8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/839/format/webp)
从上图中我们可以看出CreateDefaultBuilder()方法主要干了六件事:
UseKestrel:使用Kestrel作为Web server。
UseContentRoot:指定Web host使用的content root(内容根目录),比如Views。默认为当前应用程序根目录。
ConfigureAppConfiguration:设置当前应用程序配置。主要是读取 appsettinggs.json 配置文件、开发环境中配置的UserSecrets、添加环境变量和命令行参数 。
ConfigureLogging:读取配置文件中的Logging节点,配置日志系统。
UseIISIntegration:使用IISIntegration 中间件。
UseDefaultServiceProvider:设置默认的依赖注入容器。
创建完毕WebHostBuilder后,通过调用UseStartup()来指定启动类,来为后续服务的注册及中间件的注册提供入口,IWebHostBuilder负责创建IWebHost,然后Run()方法启动IWebHost。。
2. Startup.cs
Asp.Net Core 启动类默认命名为Startup,它是在程序入口Main()函数中为了构造IWebHost时通过UseStartup<Startup>()指定的。
该类主要包括两个方法:
2.1 ConfigureServices :
该方法一般用于配置服务将服务注册到依赖注入容器中,在Configure方法之前被调用。通过调用IServiceCollection的扩展方法进行服务注册。扩展方法的命名建议按照Add[MethodName]进行约定,比如Mvc相关服务的注册services.AddMvc()。
除此之外,我们还可以在此方法中替换ASP.NET Core默认内置的依赖注入框架。比如我们使用Castle Windsor替换:
安装Castle.Windsor.MsDependencyInjectionNuGet包;
修改方法返回值类型为IServiceProvider;
替换依赖容器:
![](http://upload-images.jianshu.io/upload_images/9068018-1cbab4e4205431e3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/638/format/webp)
2.2. Configure
该方法用于定义应用程序如何响应每个HTTP请求,以及配置HTTP流水线中的中间件。
![](http://upload-images.jianshu.io/upload_images/9068018-486db6b435605f24.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/540/format/webp)
该方法接受IApplicationBuilder作为参数,同时可以接受一些可选参数,如IHostingEnvironment和ILoggerFactory。而且,在ConfigureServices方法中注册的其他服务,也可以直接在该方法中通过参数直接注入使用,比如:
![](http://upload-images.jianshu.io/upload_images/9068018-7c0f1f7a9f15e2a4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/534/format/webp)
因为该方法用于配置整个http请求管道,所以中间件的注册要注意顺序。同样,在配置中间件时,我们同样建议遵循Use[Middleware]的命名约定,比如启用Mvc中间件app.UseMvc。
ASP.NET Core在调用之前已经默认注入了以下几个可用服务:
IConfiguration:用于读取应用程序配置。
IServiceCollection:可以理解为ASP.NET Core内置的依赖注入容器。
IApplicationBuilder:用于构建应用程序的请求管道。
IHostingEnvironment :提供了当前应用程序的运行的宿主环境配置信息。
除此之外,我们还可以使用已经注入的IServiceProvider、ILoggerFactory、ILogger、IApplicationLifetime等可用服务。这些预置服务可以注入到Startup类的构造函数或Configure方中。就像这样:
![](http://upload-images.jianshu.io/upload_images/9068018-06614afd47c1cd4c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/486/format/webp)
ConfigureServices只支持一个参数IServiceCollection,因为所有的依赖都可以通过IServiceCollection获取,所以没有必要通过方法再注入其他服务,可以直接通过以下方式解析依赖的服务:
![](http://upload-images.jianshu.io/upload_images/9068018-979cb419113bfbc7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/405/format/webp)
2.3定义多个启动类
我们看下Configure方法我们发现,通过IHostingEnvironment服务,我们可以获取当前应用程序的运行环境。代码中为开发环境启用了异常详情页面、数据库错误页面和Brower Link(支持启动多个web浏览器,并可同步刷新,一般用于跨浏览器测试),以方便我们开发调试。
ASP.NET Core默认内置了三种环境变量:Development、 Staging、Production。我们可以为每一种环境定义一个启动类,命名为Startup[Environment]即可。在启动时通过指定启动类所在的程序集名称即可根据launchSettings中的配置的ASPNETCORE_ENVIRONMENT自动加载对应的启动类。
三、应用设置
1.启动设置之launchSettings.json
launchSettings.json文件有两个配置节点:“IIS Express”、“AspNetCoreStartDemo”,这两个节点,分别对应Visual Stuido的开始调试按钮的
![](http://upload-images.jianshu.io/upload_images/9068018-1fd482a84db6b10d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/618/format/webp)
![](http://upload-images.jianshu.io/upload_images/9068018-7d608a052a115b48.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/535/format/webp)
2.应用程序配置 appsettinggs.json
在应用启动时的CreateDefaultBuilder()方法中,通过ConfigureAppConfiguration设置当前应用程序配置。
![](http://upload-images.jianshu.io/upload_images/9068018-b8dc80eae2c27eb0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/739/format/webp)
在上面的代码中,可以根据不同的运行环境读取appsetting.json或appsetting.environment.json,需要注意的是,配置文件按照顺序被依次读取,后面的数据源会覆盖前面的数据源。
如下所示是一个简单的配置文件:
![](http://upload-images.jianshu.io/upload_images/9068018-7aeb469e6a8d67c2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/506/format/webp)
![](http://upload-images.jianshu.io/upload_images/9068018-9ba3bd2a429b3f26.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/571/format/webp)
在构建主机过程中使用appsettings.json中的配置。
3.读取配置的方式
3.1 以键-值对的形式读取配置
配置文件中的配置项,都是以键-值对的形式来体现的,所以最基础的读取方式就是直接读取键值对。
![](http://upload-images.jianshu.io/upload_images/9068018-cc74455c0d5c949e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/661/format/webp)
![](http://upload-images.jianshu.io/upload_images/9068018-69af2b3a4e08b6eb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/687/format/webp)
3.2 使用GetValue<T>
这是一个扩展方法,使用它需要安装Microsoft.Extensions.Configuration.Binder包。
![](http://upload-images.jianshu.io/upload_images/9068018-e17c87e87b7614d2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/656/format/webp)
3.3.使用Options
这种方式需要安装Microsoft.Extensions.Options.ConfigurationExtensions包。
调用AddOptions()添加使用Options需要的服务。
定义与配置对应的实体类
![](http://upload-images.jianshu.io/upload_images/9068018-b3df8743adca8209.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/438/format/webp)
![](http://upload-images.jianshu.io/upload_images/9068018-585b4880323653f0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/532/format/webp)
或
![](http://upload-images.jianshu.io/upload_images/9068018-7af47e6f62d3f3d2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/650/format/webp)
![](http://upload-images.jianshu.io/upload_images/9068018-5a657788a42e1ea0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/582/format/webp)
3.4.使用Bind
和Get<T>类似,建议使用Get<T>。
![](http://upload-images.jianshu.io/upload_images/9068018-16768a184518a70a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/334/format/webp)
或
![](http://upload-images.jianshu.io/upload_images/9068018-646ad3080175215d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/522/format/webp)
3.5.文件变化自动重新加载配置
IOptionsSnapshot 支持配置文件变化自动重新加载配置。使用IOptionsSnapshot也很简单,AddJsonFile有个重载,指定reloadOnChange:true即可。
![](http://upload-images.jianshu.io/upload_images/9068018-a7b4d394448284d7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/722/format/webp)
4.命令行参数配置
命令行参数配置需要安装Microsoft.Extensions.Configuration.CommandLine包。
调用AddCommandLine()扩展方法将命令行配置Provider添加到ConfigurationBuilder中
![](http://upload-images.jianshu.io/upload_images/9068018-190d7c187537443e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/503/format/webp)
传递参数有两种形式 :
![](http://upload-images.jianshu.io/upload_images/9068018-908231eb24ee1fe3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/181/format/webp)
或
![](http://upload-images.jianshu.io/upload_images/9068018-eda96227433aaa7b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/197/format/webp)
如果为--AppId提供一个缩写的参数-a,那么执行 dotnet run -a 12345 会报在switch mappings中没有 -a 定义的错误。
幸好AddCommandLine还有一个重载,可以传一个switch mapping。
![](http://upload-images.jianshu.io/upload_images/9068018-7124a36106a792c8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/579/format/webp)
5.环境变量配置
环境变量配置需要安装 Microsoft.Extensions.Configuration.EnvironmentVariables 包。
调用AddEnvironmentVariables()扩展方法将环境变量配置Provider添加到ConfigurationBuilder中。
![](http://upload-images.jianshu.io/upload_images/9068018-92dad2a2df1ace55.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/548/format/webp)
获取所有的环境变量
![](http://upload-images.jianshu.io/upload_images/9068018-4eaa770ea5f62585.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/486/format/webp)
根据名称获取环境变量
![](http://upload-images.jianshu.io/upload_images/9068018-138d8cc809159a9b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/502/format/webp)
文章出处:https://www.jianshu.com/p/01721c257b4e