原文地址:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup

下文:

--Startup类

--Configure方法

--ConfigureServices方法

--可启用的服务(Services Available in Startup)

--其他资源

Startup类配置请求管道,处理所有应用程序接收的请求

The Startup class configures the request pipeline that handles all requests made to the application.

Startup

asp.net core需要启动类,通常以“Startup”命名,在程序WebHostBuilderExtensions中的UseStartup<TStartup>中指定Startup类名。

ASP.NET Core apps require a Startup class. By convention, the Startup class is named "Startup". You specify the startup class name in the Main programs WebHostBuilderExtensions UseStartup<TStartup> method.

你能分别定义Startup类在不同的环境(Environment),并在运行时选择适当的一个启动,如果在WebHost的配置或操作中指定了startupAssembly程序集,托管将加载startup程序集并查找Startup 或 Startup(Environment)类型,参考 StartupLoader 的 FindStartupType 和 Working with multiple environments,建议使用UseStartup<TStartup> .

You can define separate Startup classes for different environments, and the appropriate one will be selected at runtime. If you specify startupAssembly in the WebHost configuration or options, hosting will load that startup assembly and search for a Startup or Startup[Environment] type. See FindStartupType in StartupLoader and Working with multiple environments. UseStartup<TStartup> is the recommended approach.

Startup类构造函数能接受通过依赖注入的依赖关系,你能用IHostingEnvironment设置配置,用ILoggerFactory设置logging提供程序。

The Startup class constructor can accept dependencies that are provided through dependency injection. You can use IHostingEnvironment to set up configuration sources and ILoggerFactory to set up logging providers.

Startup类必须包含 方法 而 方法可选,两个方法都在程序启动时调用,这个类也可包括这些方法的特定环境版本。

The Startup class must include a Configure method and can optionally include a ConfigureServices method, both of which are called when the application starts. The class can also include environment-specific versions of these methods.

了解关于在程序启动时的异常处理

Learn about handling exceptions during application startup.

Configure方法

configure方法用于指定ASP.NET程序如何响应HTTP请求,通过将中间件组件添加到由依赖注入提供的IApplicationBuilder实例中来配置请求管道。

The Configure method is used to specify how the ASP.NET application will respond to HTTP requests. The request pipeline is configured by adding middleware components to an IApplicationBuilder instance that is provided by dependency injection.

下面是来自默认网站模板的示例,对管道增加一些扩展方法用于支持  BrowserLink, error pages, static files, ASP.NET MVC, and Identity.

In the following example from the default web site template, several extension methods are used to configure the pipeline with support for BrowserLink, error pages, static files, ASP.NET MVC, and Identity

public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactory)
{loggerFactory.AddConsole(Configuration.GetSection("Logging"));    loggerFactory.AddDebug();    if (env.IsDevelopment()){    app.UseDeveloperExceptionPage();app.UseDatabaseErrorPage();app.UseBrowserLink();}else{        app.UseExceptionHandler("/Home/Error");}    app.UseStaticFiles();    app.UseIdentity();    app.UseMvc(routes =>{routes.MapRoute(name: "default",template: "{controller=Home}/{action=Index}/{id?}");});
}

每个扩展方法添加一个中间件组件到请求管道中,例如,UseMvc扩展方法添加routing中间件到请求管道中并配置将MVC做为默认处理

Each Use extension method adds a middleware component to the request pipeline. For instance, the UseMvc extension method adds the routing middleware to the request pipeline and configures MVCas the default handler.

关于IApplicationBuilder的详细信息见 中间件。

For more information about how to use IApplicationBuilder, see Middleware.

也可以在方法签名中指定一些其他服务,如 IHostingEnvironment 和 ILoggerFactory,在这种情况下如果他们可用将被注入这些服务。

Additional services, like IHostingEnvironment and ILoggerFactory may also be specified in the method signature, in which case these services will be injected if they are available.

ConfigureServices方法

ConfigureServices方法是可选的,但若调用将在 Configure 之前被调用(一些功能会在链接到请求管道之前添加上),配置操作在方法中设置。

The ConfigureServices method is optional; but if used, it's called before the Configure method by the runtime (some features are added before they're wired up to the request pipeline). Configuration options are set in this method.

对于需要大量设置的功能,用  IServiceCollection的 Add[Service]扩展方法,下面是默认网站模板示例,将 Entity Framework, Identity, and MVC配置到程序中。

For features that require substantial setup there are Add[Service] extension methods on IServiceCollection. This example from the default web site template configures the app to use services for Entity Framework, Identity, and MVC.

public void ConfigureServices(IServiceCollection services)
{    // Add framework services.services.AddDbContext<ApplicationDbContext>(options =>options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));services.AddIdentity<ApplicationUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();services.AddMvc();// Add application services.services.AddTransient<IEmailSender, AuthMessageSender>();services.AddTransient<ISmsSender, AuthMessageSender>();
}

向服务容器中添加服务,使他们可以通过依赖注入在你的程序中使用。

Adding services to the services container makes them available within your application via dependency injection.

Startup可用的服务

ASP.NET Core 依赖注入 是在 程序的startup中 提供程序服务,你能请求这些服务通过 Startup类的构造函数 或 它的Configure和 ConfigureServices 发放中 将适当的接口作为一个参数传入。

ASP.NET Core dependency injection provides application services during an application's startup. You can request these services by including the appropriate interface as a parameter on your Startup class's constructor or one of its Configure or ConfigureServices methods.

按调用顺序看 Startup类中的每个方法,服务作为一个参数被请求

Looking at each method in the Startup class in the order in which they are called, the following services may be requested as parameters:

  • In the constructor: IHostingEnvironmentILoggerFactory

  • In the ConfigureServices method: IServiceCollection

  • In the Configure method: IApplicationBuilderIHostingEnvironmentILoggerFactoryIApplicationLifetime

其他资源

  1. Working with Multiple Environments

  2. Middleware

  3. Logging

  4. Configuration

转载于:https://blog.51cto.com/volvo9yue/1902703

ASP.NET Core 中的 startup类相关推荐

  1. ASP.NET Core中为指定类添加WebApi服务功能

    POCO Controller是 ASP.NET Core 中的一个特性,虽然在2015年刚发布的时候就有这个特性了,可是大多数开发者都只是按原有的方式去写,而没有用到这个特性.其实,如果利用这个特性 ...

  2. 在ASP.Net Core 中使用枚举类而不是枚举

    前言:我相信大家在编写代码时经常会遇到各种状态值,而且为了避免硬编码和代码中出现魔法数,通常我们都会定义一个枚举,来表示各种状态值,直到我看到Java中这样使用枚举,我再想C# 中可不可以这样写,今天 ...

  3. asp.net core 系列之Startup

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

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

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

  5. ASP.NET Core中如何显示[PII is hidden]的隐藏信息

    有时候我们在ASP.NET Core项目运行时,发生在后台程序中的错误会将关键信息隐藏为[PII is hidden]这种占位符,如下所示: 而知道这些关键信息,有时候对我们调试程序是非常重要的.所以 ...

  6. asp.net core中IHttpContextAccessor和HttpContextAccessor的妙用

    分享一篇文章,关于asp.net core中httpcontext的拓展. 现在,试图围绕HttpContext.Current构建你的代码真的不是一个好主意,但是我想如果你正在迁移一个企业类型的应用 ...

  7. 在asp.net core中使用托管服务实现后台任务

    在业务场景中经常需要后台服务不停的或定时处理一些任务,这些任务是不需要及时响应请求的. 在 asp.net中会使用windows服务来处理. 在 asp.net core中,可以使用托管服务来实现,托 ...

  8. 探索ASP.NET Core中的IStartupFilter

    原文:Exploring IStartupFilter in ASP.NET Core 作者:Andrew Lock 译者:Lamond Lu 在本篇博客中,我将介绍一下IStartupFilter, ...

  9. 如何简单的在 ASP.NET Core 中集成 JWT 认证?

    前情提要:ASP.NET Core 使用 JWT 搭建分布式无状态身份验证系统 文章超长预警(1万字以上),不想看全部实现过程的同学可以直接跳转到末尾查看成果或者一键安装相关的 nuget 包 自上一 ...

最新文章

  1. ArrayList与LinkedList区别
  2. ryu的防火墙功能 ryu.app.rest_firewall,配合mininet和open vswitch(OVS)
  3. 李超:WebRTC传输与服务质量
  4. [SPS2010] 使用心得 7 - ebook for Installation
  5. c语言向文件中写入字符串_C语言中定义字符串的两种方式及其比较
  6. soap接口怎么不返回tuple python_Python 中的接口
  7. java程序a-z b-y,请完成下列Java程序:对大写的26个英文字母加密,从键盘输入一个大写字母串,输出这个串加密后的结 - 赏学吧...
  8. 推荐系统组队学习——矩阵分解和FM
  9. Zerotier Moon服务器配置
  10. 制作jquery插件1-基础
  11. 全触摸模式,让你尽享ipad 开发出的精品
  12. 常用网页设计html特殊符号转义字符编码查询对照表
  13. 十大气势背景音乐(适合战队,广告招商会场用)
  14. 浏览器上不去网络。需要进入ie点开Internet选项,网络中,局域网(LAN)设置,可以勾选上自动检测设置
  15. Java数组的复制、扩容、删除
  16. mybatis之choose标签
  17. 这么写参数校验(validator)就不会被劝退了~
  18. 数字化汗字中仲字如化数字化_如何将旧的电影照片数字化
  19. java跳出循环的几种方式
  20. 关于Inziu Iosevka和Sarasa Gothic字体

热门文章

  1. XR应用场景骤变,一场波及5亿人的新探索开始了
  2. 中国如何赢得新一轮超算竞赛?关键在向数据密集型超算转变
  3. 曾在字节实习的程序员小姐姐,教你一步提取动漫线稿!比用PS更清晰
  4. DeeCamp2021启动,李开复张亚勤吴恩达等大咖喊你报名啦
  5. 中国首次实现量子优越性!比谷歌突破更厉害,比最强超级计算机快一百万亿倍 | Science...
  6. Waymo自动驾驶报告:平均21万公里一次事故,严重事故都是人类司机的锅
  7. 比英伟达便宜4000元、功耗更低、游戏性能相同,AMD发布RX 6900 XT旗舰显卡
  8. mybatis 思维导图,让 mybatis 不再难懂(二)
  9. 使用LVS+TUN搭建集群实现负载均衡
  10. 服务器市场步步为营:Intel发布新款至强Xeon E5-4600v4四路处理器