原文:链接

Startup.cs的作用:

配置各服务和HTTP请求管道。

Startup类:

ASP.NET Core中使用按惯例Startup命名的类Startup.cs

ConfigureServicesConfigure在应用程序启动时由ASP.NET Core runtime调用

public class Startup
{
// Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
...
} // Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
...
}
}

Startup在构建app host时,将为应用程序指定该类。app host这里通常为webHost在Program类中的 CreateWebHostBuilder上调用时构建的。即调用WebHostBuilderExtensions.UseStartup <TSTARTUP>方法构建:

public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
} public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}

HostingEnvironment 可作为Startup类构造函数参数服务,ConfigureServices用来添加其他服务,在ConfigureServices中添加后,相应的服务和应用程序就可以在Configure方法中使用。

在类中依赖注入的常见用法Startup是注入:

public class Startup
{
private readonly IHostingEnvironment _env;
private readonly IConfiguration _config;
private readonly ILoggerFactory _loggerFactory; public Startup(IHostingEnvironment env, IConfiguration config,
ILoggerFactory loggerFactory)
{
_env = env;
_config = config;
_loggerFactory = loggerFactory;
} public void ConfigureServices(IServiceCollection services)
{
var logger = _loggerFactory.CreateLogger<Startup>(); if (_env.IsDevelopment())
{
// Development service configuration logger.LogInformation("Development environment");
}
else
{
// Non-development service configuration logger.LogInformation($"Environment: {_env.EnvironmentName}");
} // Configuration is available during startup.
// Examples:
// _config["key"]
// _config["subsection:suboption1"]
}
}

ConfigureServices方法:

在startup.cs中ConfigureServices方法:

  • 可选的。
  • 在startup Configure方法之前由Host调用。
  • 配置选项被设置惯例。

典型的模式是调用所有Add{Service}方法,然后调用所有services.Configure{Service}方法。请参阅配置身份服务

对于需要大量设置的功能,IServiceCollectionAdd{Service}上有扩展方法。典型的ASP.NET Core应用程序会配置Entity Framework,Identity和MVC注册服务:

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<ApplicationDbContext>(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); // Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}

Configure方法

configure方法用于指定应用程序如何响应HTTP请求。通过将中间件组件添加到IApplicationBuilder实例来配置请求管道。IApplicationBuilder可用于该Configure方法,但它未在服务容器中注册。托管创建IApplicationBuilder并直接传递给Configure

ASP.NET核心模板配置与支持的管道:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
} app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy(); app.UseMvc();
}

每个Use扩展方法都将一个或多个中间件组件添加到请求管道。例如,UseMvc扩展方法将Routing Middleware添加到请求管道并将MVC配置为默认处理程序。

请求管道中的每个中间件组件负责调用管道中的下一个组件或者在适当的时候使链路短路。如果中间件链中没有发生短路,则每个中间件都有第二次机会在请求发送到客户端之前处理该请求。

其他服务(例如IHostingEnvironmentILoggerFactory)也可以在Configure方法签名中指定。指定后,如果可用,则会注入其他服务。

有关如何使用IApplicationBuilder和中间件处理顺序的更多信息,请参阅ASP.NET核心中间件

ASP.NET Core中的Startup的更多相关文章

  1. ASP.NET Core中的Startup类

    ASP.NET Core程序要求有一个启动类.按照惯例,启动类的名字是 "Startup" .Startup类负责配置请求管道,处理应用程序的所有请求.你可以指定在Main方法中使 ...

  2. ASP.NET Core 中文文档 第三章 原理(1)应用程序启动

    原文:Application Startup 作者:Steve Smith 翻译:刘怡(AlexLEWIS) 校对:谢炀(kiler398).许登洋(Seay) ASP.NET Core 为你的应用程 ...

  3. asp.net core 系列之Startup

    这篇文章简单记录 ASP.NET Core中 ,startup类的一些使用. 一.前言 在 Startup类中,一般有两个方法: ConfigureServices 方法: 用来配置应用的 servi ...

  4. ASP.NET Core 中的应用程序启动 Startup

      ASP.NET Core 应用使用Startup类来作为启动类.   Startup类中包含了ConfigureServices方法,Configure方法,IConfiguration,IHos ...

  5. Asp.net Core中使用Session

    前言 2017年就这么悄无声息的开始了,2017年对我来说又是特别重要的一年. 元旦放假在家写了个Asp.net Core验证码登录, 做demo的过程中遇到两个小问题,第一是在Asp.net Cor ...

  6. 在ASP.NET Core中使用百度在线编辑器UEditor

    在ASP.NET Core中使用百度在线编辑器UEditor 0x00 起因 最近需要一个在线编辑器,之前听人说过百度的UEditor不错,去官网下了一个.不过服务端只有ASP.NET版的,如果是为了 ...

  7. ASP.NET Core 中文文档 第二章 指南(4.6)Controller 方法与视图

    原文:Controller methods and views 作者:Rick Anderson 翻译:谢炀(Kiler) 校对:孟帅洋(书缘) .张仁建(第二年.夏) .许登洋(Seay) .姚阿勇 ...

  8. ASP.NET Core 中文文档 第三章 原理(6)全球化与本地化

    原文:Globalization and localization 作者:Rick Anderson.Damien Bowden.Bart Calixto.Nadeem Afana 翻译:谢炀(Kil ...

  9. ASP.NET Core 中文文档 第三章 原理(13)管理应用程序状态

    原文:Managing Application State 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:高嵩 在 ASP.NET Core 中,有多种途径可以对应用程序的状态进行 ...

随机推荐

  1. php 根据URL下载远程图片、压缩包、pdf等文件到本地

    1.此方法可以下载图片.压缩包.pdf(亲测),应该所有类型的文件都可以下载到本地,可以试一下 //远程路径,名称,文件后缀 function downImgRar($url,$rename,$ext ...

  2. Golang: 常用的文件读写操作

    Go 语言提供了很多文件操作的支持,在不同场景下,有对应的处理方式,今天就来系统地梳理一下,几种常用的文件读写的形式. 一.读取文件内容 1.按字节读取文件 这种方式是以字节为单位来读取,相对底层一些 ...

  3. docker启动报错 docker: Error response from daemon: OCI runtime create failed: container_linux.go:348

    问题描述 doker启动时,报错:docker: Error response from daemon: OCI runtime create failed: container_linux.go:3 ...

  4. 性能测试之Jmeter插件安装

    使用Jmeter的实际过程中,需要使用到很多插件,比如json的插件,还有就是做websocket接口测试的时候需要下载websocket的插件,虽然官方提供了插件下载的地址,但是知道为什么每次访问的 ...

  5. httprunner学习20-跳过用例skip/skipIf/skipUnless

    前言 在实际工作中,我们有时候会需要对测试用例加判断,比如某个接口功能暂时去掉了,我们希望对这个用例skip不去执行. 当其它的接口依赖于登陆接口返回的token时候,如果登陆都失败了,后面的接口,我 ...

  6. httprunner学习9-完整的用例结构(yaml&json)

    前言 前面几篇零散的学了一些httprunner的知识点,那么一个完整的 YAML/JSON 用例文件包含哪些关键字呢? 测试用例结构 在 HttpRunner 中,测试用例组织主要基于三个概念: 测 ...

  7. [图解]Windows下使用Zend Studio 10和XAMPP 1.8搭建开发环境,ZendFramework 2 HelloWorld

    1.下载并安装 ZendStudio,搜一个破解版 XAMPP,官网下载:https://www.apachefriends.org/index.html 2.打开ZendStudio新建一个php项 ...

  8. datagrid 溢出文本显示省略号 转载

    http://www.jeasyuicn.com/?sort=3 .datagrid-cell, .datagrid-cell-group, .datagrid-header-rownumber, . ...

  9. Mysql建表通用写法

    Mysql建表通用写法 CREATE TABLE IF NOT EXISTS stu( id ) PRIMARY KEY AUTO_INCREMENT,//主键 自增 stuname ) NOT NU ...

  10. ReadIniTest_GetPrivateProfileString

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...