原文:链接

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简单实现异步多文件上传并使用Postman测试提交图片

    虽然现在很多都是使用大平台的对象存储存放应用中的文件,但有时小项目还是可以使用以前的方式上传到和程序一起的服务器上,强调一下这里是小众需求,大众可以使用阿里云的OSS,腾讯的COS,七牛的巴拉巴拉xx ...

  2. MySQL Transaction--使用SHOW INNODB STATUS 查看未提交事务

    当MySQL服务器出现性能问题时,应该优先排查未提交事务,除可以查询相关系统表外,还可以观察SHOW INNODB STATUS的输出结果来确认未提交事务. 首先查看InnoDB事务的History ...

  3. 第一篇 -- XML基础

    一.XML简介 XML是一种标记语言,用于描述数据,它提供一种标准化的方式来来表示文本数据.XML文档以.xml为后缀.需要彻底注意的是XML是区分大小写的. 先从一个简单的XML例子来了解下xml基 ...

  4. ArcGIS 10.2 JavaScript API本地部署离线开发环境

    1 获取ArcGIS JavaScript API API的下载地址http://support.esrichina.com.cn/2011/0223/960.html,在下载页面会看到api和sdk ...

  5. 配置/更改vue项目中的资源路径

    打开 build/webpack.base.conf.js  ,在module.exports .resolve.alias下自定义: alias: { 'vue$': 'vue/dist/vue.e ...

  6. python基础语法3 元组,字典,集合

    元组: ========================元组基本方法===========================用途:存储多个不同类型的值定义方式:用过小括号存储数据,数据与数据之间通过逗号 ...

  7. Spring启动,constructor,@PostConstruct,afterPropertiesSet,onApplicationEvent执行顺序

    package com.xx; import javax.annotation.PostConstruct; import javax.annotation.Resource; import org. ...

  8. vuex2 mapActions 报错 `unknown action type: xxxx`

    export const setBreadCrumb = ({ dispatch }, data) => { dispatch('SET_BREADCRUMB', data) } 当调用的时候报 ...

  9. SPU、SKU、ARPU

    在涂涂商城开发之前,发现一篇关于电商中 SPU.SKU.ARPU 的介绍,转至博客,原文地址:http://www.ikent.me/blog/3017 什么是SPU.SKU.ARPU 首先,搞清楚商 ...

  10. vue使用插件的流程

    1.引入vue 2.引入插件 3.通过vue.use()调用 例子:使用router插件 import Vue from "vue"; import VueRouter from ...