asp.net5 的应用启动

参考来源:html

http://docs.asp.net/en/latest/conceptual-overview/aspnet.htmlnode

http://docs.asp.net/en/latest/fundamentals/startup.htmlweb

asp.net5应用是运行在dnx之上的,每个程序都是一个dnx项目,经过 Microsoft.AspNet.Hosting 这个包来加载的。编程

与Node.js作个类比:json

node.exe    index.js  至关于node这个程序打开代码文件,加载运行。c#

                                  在进程里增长一个Node.js: Server-side JavaScript的进程,占空间7.6MB。windows

dnx web    至关于dnx程序搜索当前目录,找到project.json,并找到里面相应的命令,而后加载相应的代码并运行。缓存

                  我机器是64位的win10.,在进程里增长一个dnx(32位)的进程,在我机器上大概28MB的内存。  mvc

public class Startup{
     public void ConfigureServices(IServiceCollection services)
     {
     //定义用户服务
     }

     public void Configure(IApplicationBuilder app)
     {
     //定义中间件
     }}

Services:

底层是经过DI 而建立的组件,用于通用部分。服务有三种:单件、局部做用域、即时服务。app

即时服务,每次使用都建立。局部做用域服务是每次请求时,建立一个做业域。单件服务是全局只有一个的服务。

Middleware

中间件是asp.net5处理时每一个请求管道。它处理HttpContext  上的异步逻辑,而且能够按顺序调用以后的中间件,也能够随时中断请求。是在Configure方法里,一般用UseXXX的一个IApplicationBuilder 扩展方法来调用。

asp.net5有一组预约义好的中间件,好比静态文件访问,路由,诊断,认证。 你还能够自定义一个

Servers服务端。

经过Hosting并不直接监听请求,而是一个HTTP Servergo 监听,并将其转为一个HttpContext。

服务支持IIS或者self hosting 。在window上, WebListener server是基于HTTP.sys,而且脱离IIS的一个服务。

在非windows中,可使用跨平台的 Kestrel web server

Web root

在project.json中,能够定义根目录。好比处理静态文件时的根目录。

Configuration

asp.net5支持一种简单的 键值对的配置信息。 它是内置的功能,支持多种文件类型、环境变量。

在startup.cs中,定义一个静态变量。初始化时,让它加载各类配置。而后程序其它地方直接经过

Startup.Configure就能使用信息。


应用启动

Startup类是程序的入口,这里加载配置,服务。启动时,会在程序集中在全部空间搜索叫Startup的类。你也能够指定一个类名,经过Hosting:Application 的配置的键值,启动类是否是public并没有关系。  若是有多个同名的类,程序会搜索与项目根命名空间相同的优先,找不到,则根据命名空间的字母表顺序来决定。

Startup类的构造函数也支持依赖注入,好比IApplicationEnvironment的参数。

Startup类一般它必须定义一个Configure函数,以及可选的ConfigureServices函数。

Configure函数是用来指定应用如何处理HTTP请求的。一般复杂的请求管道的配置是封闭在中间件里,而后中间件再作成IApplicationBuilder的扩展方法来被Use. 函数必须有IApplicationBuilder 类型 的参数,后两个是可选的参数IHostingEnvironment and ILoggerFactory 

这些参数的对象都是经过依赖注入产生的。依赖注入是Microsoft.Extensions.DependencyInjection包实现的功能。它向外提供IServiceCollection的一个实例,挂载着全部已经添加的ServiceDescriptors。

我的认为asp.net5此处的Service命名有些问题 !IServiceCollection本是依赖注入的服务集合,它能够随时添加一个服务描述符(ServiceDescriptors),也随时能够反向生成一个实例对象来。asp.net5自身的服务Service前面描述过,表明一个功能模块或功能组件,好比mvc,静态文件服务等。

但我也不肯定这两个Service到底指定的是否是同一个意思,或是故意为之。

public void ConfigureServices(IServiceCollection services){
      Console.WriteLine(services.Count); //显示13
      services.AddMvc();
     Console.WriteLine(services.Count);//显示112,代表向依赖注入的服务集合里缓存了99个服务描述
}

asp.net5的依赖注入的相关内容看了一些,但一直没法深刻。由于每一个文档都是在讲:如何在Startup里自动注入参数,如何在Startup里添加一个服务,而后到Controller里,构造注入。

关键的疑问点:Startup构造函数,Configure函数,Controller的构造函数注入时,为何写个参数就能注入了,这不很奇怪吗?是经过重载实现的吗?可能须要看一下源码才能够。

ConfigureServices 函数

这个函数在Startup类里,是可选的。它能够经过注入,传入一IServiceCollection 实例做为参数.

ConfigureServices函数比Configure更早的调用,services.AddMvc();能够引入许多MVC必须的一些服务。

这样在app.UseMvc()时,这些服务才能在请求来到 时被使用。

建议在使用时,把一些服务作成IServiceCollection 的扩展,好比下面这样:

services.AddEntityFramework()
        .AddSqlServer();

这些添加进来的服务就进入到依赖注入的容器里了,随时能够在你的应用中访问到。经过依赖注入,能够在函数里注入参数,代替“硬编码”,是很是好的编程实践!

ConfigureServices函数仍是你添加配置---Configuration的地方

最后一段是讲框架支持注入的服务,重点是Startup构造函数以及两个Configre的函数里的参数。它们分别有什么用,有什么属性能够获取!

 The framework services and objects include:

  • IApplicationBuilder

  • Used to build the application request pipeline. Available only to the Configuremethod in Startup. Learn more about Request Features.

  • IApplicationEnvironment

  • Provides access to the application properties, such as ApplicationName,ApplicationVersion, and ApplicationBasePath. Available to the Startup constructor andConfigure method.

  • IHostingEnvironment

  • Provides the current EnvironmentNameWebRootPath, and web root file provider. Available to the Startup constructor and Configure method. Learn more about 🔧 Hosting.

  • ILoggerFactory

  • Provides a mechanism for creating loggers. Available to the Startup constructor andConfigure method. Learn more about Logging.

  • IServiceCollection

  • The current set of services configured in the container. Available only to theConfigureServices method, and used by that method to configure the services available to an application.

下面是三个函数调用里,服务所对应的参数依次是

Startup Constructor - IApplicationEnvironment - IHostingEnvironment - ILoggerFactory

ConfigureServices - IServiceCollection

Configure - IApplicationBuilder - IApplicationEnvironment - IHostingEnvironment -ILoggerFactory

相关文章
相关标签/搜索