原文:链接

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. 搜索和浏览离线 Wikipedia 维基百科(中/英)数据工具

    为什么使用离线维基百科?一是因为最近英文维基百科被封,无法访问:二是不受网络限制,使用方便,缺点是不能及时更新,可能会有不影响阅读的乱码. 目前,主要有两种工具用来搜索和浏览离线维基百科数据:Kiwi ...

  2. 升级.net core 3.x 后mvc项目调试状态编辑view代码不能实时预览

    https://stackoverflow.com/a/58126955 简单来说 需要在nuget上安装Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilati ...

  3. SpringBoot2.x搭建Eureka

    1 说明 全部配置基于1.8.0_111 当前SpringBoot使用2.0.5 2 创建项目 在SpringBoot项目生成器中,输入Group和Artifact,如下配置: 3 pom.xml配置 ...

  4. Centos6.5配置防火墙

    1.查看防火墙状态 [root@instance-xfl1djr7 ~]# /etc/init.d/iptables status 2.启动/关闭防火墙 开启防火墙 [root@instance-xf ...

  5. Linux文本处理sed、软件包管理、磁盘存储、文件系统和挂载

    Linux文本处理工具sed.软件包管理.磁盘存储及文件系统 文本处理工具sed巧妙用法 1.通过sed获取文件路径的基名和目录名 思路:采用正则表达式将文本字符串分组,取对应的分组后向引用即可. 获 ...

  6. Nginx的特性功能-反向代理、负载均衡、缓存、动静分离、平滑升级

    反向代理 nginx配置文件 events  {   }  事件驱动 httpd  {   }   关于httpd相关的配置 server {  }  定义虚拟主机 location {   }    ...

  7. IDisposable 接口

    提供一种用于释放非托管资源的机制. 地址:https://docs.microsoft.com/zh-cn/dotnet/api/system.idisposable?view=netframewor ...

  8. DRF序列化和反序列化(二:ModelSerializer)

    一: rest_framework 中 serializers.Serializer的不方便之处(以下简称Serializer) a:需要定义每一个字段,并且这个字段是和models字段及其类似. b ...

  9. 项目Beta冲刺--1/7

    项目Beta冲刺--1/7 作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Beta冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综合评估及 ...

  10. Flash完美跨域访问的方法

    先,你要确定以下几点,否则可能无法实现: 1.你要跨到哪个域,你必须能管理那域上文件,因为这里要放一个通行文件. 2.你的Flash如果只有SWF,那不一定能实现,因为有时,Flash的AS中,要加入 ...