Startup文件


Startup文件
新增一个 asp.net core 的空项目,这里只有 Program.cs 和 Startup.cs 两个文件。
Program.cs
public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .Build(); }
Startup.cs
public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 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) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); } }
在 Program.cs 文件中,是构建了一个 WebHost 并使用 Startup 文件来启动web的。
此文件有:
构造函数(默认没有写出来)
ConfigureServices 方法
Configure 方法
三个方法执行顺序:
构造函数、ConfigureServices方法、Configure方法
构造函数
在构造函数中,我们可以加载一些接口类,来实现配置。
比如:添加 IConfiguration,添加 ILoggerFactory
public IConfiguration Configuration { get; set; } public Startup(IConfiguration config, ILoggerFactory logger) { this.Configuration = config; var log = logger.CreateLogger("default"); logger.AddConsole(); log.LogInformation("start info"); }
IConfiguration 和 ILoogerFactory 都是 Microsoft.Extensions 下的 命名空间的。
在构造函数执行时,会为其赋值默认的具体类。这是 .net core 的 DI 依赖注入 实现的。
这里的 IConfiguration 读取的是 appsettings.json 文件 。
而假如你自己写了一个接口,并实现了此接口,并写在其构造函数中,是不会默认找到此接口的实现的。
ConfigureServices 方法
在 ConfigureServices 方法中,就可以把自己的接口和实现写在这里。
比如:
public interface IUser { string Say(); } public class User : IUser { public string Say() { return "hello"; } }
在 ConfigureServices 方法中加入 IUser
//加入接口与实现 services.AddTransient<IUser, User>(); //其他地方使用方法 services.AddTransient<IUser, User>();
Configure 方法
在这里可以拦截每个 HTTP 请求,因此,一些全局请求设置可以加在这里。
我们在这里输出一下上面的配置和IUser
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.Run(async (context) => { var user = context.RequestServices.GetRequiredService<IUser>(); var str = user.Say(); await context.Response.WriteAsync(str); var name = this.Configuration["name"]; await context.Response.WriteAsync(name); var connStr = this.Configuration["Data:DefaultConnection:ConnectionString"]; await context.Response.WriteAsync(connStr); await context.Response.WriteAsync("Hello World!"); }); }
IHostingEnvironment 和 IApplicationLifetime 在 Configure 中经常使用
IApplicationLifetime 定义了一些和程序启动和结束相关的东西。
lifeTime.ApplicationStarted.Register(()=> { Console.WriteLine("started"); }); lifeTime.ApplicationStopped.Register(() => { Console.Write("web stopped"); }); app.Run(async (context) => { await context.Response.WriteAsync($"{env.WebRootPath}\r\n"); await context.Response.WriteAsync($"{env.ApplicationName}\r\n"); await context.Response.WriteAsync($"{env.EnvironmentName}\r\n"); });
版权说明
作者:SQBER
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
{0}
{5}
{1}
{2}回复
{4}
*昵称:
*邮箱:
个人站点:
*想说的话: