ASP.NET Core 启动的配置
Web主机配置
使用默认配置创建Web主机
静态类WebHost,公开了一系列简便方法,可使用默认配置参数创建Web主机。用的较多的是CreateDefaultBuilder方法,默认配置创建WebHostBuilder实例。
默认配置包括:
(1)使用内置Kestrel服务器组件,能够使Web应用在进程中独立运行。
(2)使用IIS交互。IIS将作为反向代理端,将HTTP请求转发到Web应用程序。
(3)将应用程序的当前目录作为Web内容根目录。
(4)加载appsettings.json文件来对应程序进行配置。
(5)加载环境变量和命令行参数。
(6)记录日志,并在控制台窗口和调试窗口输出日志信息。
调用CreateDefaultBuilder方法后,还需要调用UseStartup指定一个项目类,这个类主要负责配置应用程序要用到的服务组件以及中间件。
| public static void Main(string[] args) | |
| { | |
| //IWebHostBuilder builder = WebHost.CreateDefaultBuilder(args).UseStartup<Startup>(); | |
| IWebHostBuilder builder = WebHost.CreateDefaultBuilder<Startup>(args); // 也可以这样写 | |
| // 创建 Web 宿主 | |
| IWebHost host = builder.Build(); | |
| // 启动 Web 主机 | |
| host.Run(); | |
| } |
配置Web服务器的URL
配置都在WebHostBuilder对象上完成,一旦调用Build方法生成服务主机后就不能更改配置了,尤其是用于监听客户端请求的URL。
一般指定URL的方法有三种:
(1)调用UseUrls方法。这是个扩展方法,内部调用了IWebHostBuilder的UseSetting方法。UseUrls可使用可变字符串对象作为参数,方便指定多个URL。
(2)调用UseSetting方法,配置的Key参数为WebHostDefaults.ServerUrlsKey字段,配置的值是一个单独的字符串,如果有多个URL,需要用英文分号分隔。
(3)通过配置文件,如默认的appsettings.json,可自定义文件名。
一般格式为如 http://localhost:6000 ,如果需要监听本机某个端口上所有地址,可用*或+代替主机名,如 http://*:8005
| public static void Main(string[] args) | |
| { | |
| var builder = new WebHostBuilder() | |
| .UseKestrel() | |
| .UseContentRoot(Directory.GetCurrentDirectory()) | |
| .UseIISIntegration() | |
| .UseStartup<Startup>(); | |
| // 方法一 | |
| //.UseUrls("http://localhost:6500", "http://localhost:7000", "http://*:9730"); | |
| // 方法二 | |
| //.UseSetting(WebHostDefaults.ServerUrlsKey, "http://localhost:8990"); | |
| // 第三种方法 | |
| ConfigurationBuilder config = new ConfigurationBuilder(); | |
| config.SetBasePath(builder.GetSetting(WebHostDefaults.ContentRootKey)) | |
| .AddJsonFile("host.json"); | |
| builder.UseConfiguration(config.Build()); | |
| builder.Build().Run(); | |
| } |
配置Web项目调试方案
ASP.NET Core Web项目模板生成两个调试方案:
(1)以IIS Express为反向代理运行应用程序。
(2)用项目名称命名,独立运行项目(通过dotnet命令)。
可以通过项目属性窗口中的“调试”选项卡来操作。调试页面配置内容在目录\Properties\launchSetting.json文件中。
| { | |
| "iisSettings": { | |
| "windowsAuthentication": false, | |
| "anonymousAuthentication": true, | |
| "iis": { | |
| "applicationUrl": "http://localhost/Demo", | |
| "sslPort": 0 | |
| }, | |
| "iisExpress": { | |
| "applicationUrl": "http://localhost:53196", | |
| "sslPort": 0 | |
| } | |
| }, | |
| "profiles": { | |
| "RunApp": { | |
| "commandName": "Project", | |
| "launchBrowser": true, | |
| "environmentVariables": { | |
| "ASPNETCORE_Environment": "Development" | |
| }, | |
| "applicationUrl": "http://localhost:6000" | |
| } | |
| } | |
| } |
profiles字段下所包含内容就是调试方案列表。模板生成两个一个是IIS Express,一个与项目名相同。commandName用于描述程序在调试时的启动方式,IIS Express,IIS,Project,Executable。
Startup
基于方法约定的Startup类
在默认项目中,会创建一个Startup类,并通过WebHostBuilder的扩展方法UseStartup来进行配置。Startup类也可以自定义类名,但必须包含两个约定方法。
(1)ConfigureServices方法:这个是可选的,当需要向容器中添加服务是才定义。只有一个参数,类型为IServiceCollection。在ConfigureServices方法内部可以向IServicecollection集合添加要用到的服务。
(2)Configure方法:此方法必需有,它支持参数的依赖注入,要求必需包含IApplicationBuiler类型参数,而且放在参数第一位。
| public void ConfigureServices(IServiceCollection services) | |
| { | |
| // ... | |
| } | |
| public void Configure(IApplicationBuilder app) | |
| { | |
| app.Run(async context => | |
| { | |
| // 设置文本编码 | |
| context.Response.ContentType = "text/html;charset=UTF-8"; | |
| // 返回消息给客户端 | |
| await context.Response.WriteAsync("这是一个 Web 应用"); | |
| }); | |
| } |
使用IStartup接口定义Startup类
Startup类也可以通过实现IStartup接口来定义。
| public interface IStartup | |
| { | |
| void Configure(IApplicationBuilder app); | |
| IServiceProvider ConfigureServices(IServiceCollection services); | |
| } |
由于这种实现并非约定,不能在Configure方法上接收依赖注入对象,但可以通过构造函数参数来注入。
| public class Startup : IStartup | |
| { | |
| IHostingEnvironment _hostEnv; | |
| public Startup(IHostingEnvironment env) | |
| { | |
| _hostEnv = env; | |
| } | |
| public void Configure(IApplicationBuilder app) | |
| { | |
| app.Run(async (context) => | |
| { | |
| context.Response.ContentType = "text/html;charset=UTF-8"; | |
| await context.Response.WriteAsync($"我的 Web 应用程序,它运行在 {_hostEnv.EnvironmentName} 环境中"); | |
| }); | |
| } | |
| public IServiceProvider ConfigureServices(IServiceCollection services) | |
| { | |
| return services.BuildServiceProvider(); | |
| } | |
| } |
无Startup类启动
定义Startup类是为了配置方便,其实也可以不使用,直接调用IWebHostBuilder的扩展方法配置。
| public static void Main(string[] args) | |
| { | |
| IWebHostBuilder builder = new WebHostBuilder() | |
| .UseKestrel() | |
| .UseIISIntegration() | |
| .UseContentRoot(Directory.GetCurrentDirectory()) | |
| .UseUrls("http://localhost:8605") | |
| .ConfigureServices(services => | |
| { | |
| // 按需添加服务 | |
| }) | |
| .Configure(app => | |
| { | |
| app.Run(async context => | |
| { | |
| context.Response.ContentType = "text/plain;charset=UTF-8"; | |
| await context.Response.WriteAsync("你好,Web 应用程序!"); | |
| }); | |
| }); | |
| builder.Build().Run(); | |
| } |
启动环境
非预定义环境
预定义启动环境有三个:
(1)Development:在开发阶段使用。
(2)Staging:上线之前,预览版本。
(3)Production:正式上线投入使用。
可根据实际自定义启动环境名称。在程序中随时可通过访问IHostingEnvironment.EnvironmentName属性来监测当前环境,也可调用IsEnvironment扩展方法来判断。
| public static void Main(string[] args) | |
| { | |
| var builder = new WebHostBuilder() | |
| .UseEnvironment("Preview") // UseEnvironment扩展方法用于设置启动环境,此处自定义了名称。 | |
| .UseKestrel() | |
| .UseUrls("http://localhost:6000") | |
| .UseContentRoot(Directory.GetCurrentDirectory()) | |
| .UseStartup<Startup>(); | |
| var host = builder.Build(); | |
| host.Run(); | |
| } |
| public class Startup | |
| { | |
| public void ConfigureServices(IServiceCollection services) | |
| { | |
| } | |
| public void Configure(IApplicationBuilder app, IHostingEnvironment env) | |
| { | |
| app.Run(async (context) => | |
| { | |
| context.Response.ContentType = "text/html;charset=UTF-8"; | |
| string responseMessage = null; | |
| if(env.IsEnvironment("Preview")) | |
| { | |
| responseMessage = "应用目前仍处于预览阶段"; | |
| } | |
| else | |
| { | |
| responseMessage = "应用已正式上线"; | |
| } | |
| await context.Response.WriteAsync(responseMessage); | |
| }); | |
| } | |
| } |
Startup类匹配启动环境
一种是通过Startup类名来与环境匹配。如类名可以命名为StartupDevelopment。
另一种是类名不匹配而使用约定方法与环境匹配。如方法名为ConfigureDevelopment,ConfigureDevelopmentServices。
还应该编写默认Startup类,这样如果找不到匹配环境可以使用默认类。
| public class Startup | |
| { | |
| public void ConfigureServices(IServiceCollection services) | |
| { | |
| } | |
| public void Configure(IApplicationBuilder app, IHostingEnvironment env) | |
| { | |
| if (env.IsDevelopment()) | |
| { | |
| app.UseDeveloperExceptionPage(); | |
| } | |
| app.Run(async (context) => | |
| { | |
| await context.Response.WriteAsync("Hello World!"); | |
| }); | |
| } | |
| public void ConfigureDevelopment(IApplicationBuilder app) | |
| { | |
| app.Run(async context => | |
| { | |
| context.Response.ContentType = "text/html;charset=UTF-8"; | |
| await context.Response.WriteAsync("在开发环境中运行"); | |
| }); | |
| } | |
| } |
本文来自博客园,作者:一纸年华,转载请注明原文链接:https://www.cnblogs.com/nullcodeworld/p/15400843.html
浙公网安备 33010602011771号