ASP.NET 5 简介

来源https://docs.asp.net/en/latest/conceptual-overview/aspnet.html
ASP.NET 5 是ASP.NET的重新设计。

什么是ASP.NET 5##

ASP.NET 5 是开源、跨平台用来构建现代基于云平台的web应用的.NET框架。是模块化的、低overhead、灵活的框架。
为什么构建ASP.NET 5

首先从ASP.NET 1.0到现在已经15年了,虽然一直在演变,增加新功能,也有很多开发者使用,但是无法跨平台、不开源,所以你懂得,跟不上时代了。

所以ASP.NET 5 做出了一些架构上的改变,使得核心的web框架更加精简和模块化。他不再依赖于System.Web.dll,而是依赖于一系列颗粒度更小的dll,而且这些dll都是可以通过NuGet来进行安装管理的,所有可以减少应用的surface area来提高安全性,同时也减少了服务负担,提高了性能,实行pay for play.

ASP.NET 5是为了满足现代化的web开发,统一了Web UI 和Web api以及开发工作流。通过引进基于环境的配置,是的ASP.NET 5支持cloud-ready,同时也提供了内建的DI(依赖注入)支持。

为了吸引开发者,所以要开源,要跨平台,鼓励社区contributions。在支持Visual Studio快速开发,同时也提供了一个完整的命令行接口。

总之,ASP.NET 5为了吸引开发者、跟上时代,做出了开源、跨平台的改变,基础改进如下
1. 轻量级、模块化的http请求pipeline
2. 可以使用IIS作为host,也可以self-host in your own process
3. Built on .NET Core, which supports true side-by-side app versioning
4. 完全通过NuGet来发布
5. 对创建和使用NuGet包提供完整的支持
6. 对Web UI 和 Web APIs提供了一致的web开发栈
7. Cloud-ready environment-based configuration
8. 内建支持依赖注入
9. 新的工具简化现代web开发
10. 跨平台
11. 开源、专注社区

应用解剖##

ASP.NET 5是通过新的.NET Execution Environment(DNX)来构建和运行的。每一个ASP.NET 5都是一个DNX项目,通过Microsoft.AspNet.Hosting包,ASP.NET 5和DNX集成在一起的。

ASP.NET 5应用是由一个Startup类来定义的:

public class Startup
{
     public void ConfigureServices(IServiceCollection services)
     {
     }
	public void Configure(IApplicationBuilder app)
 {
  }
}

ConfigureServices方法定义了应用所使用的服务,Configure方法定义了构成请求管道的中间件。

服务##

服务是在ASP.NET 5应用中公共使用的组件,通过依赖注入来获取。ASP.NET 5 包含了内建的IOC容器,默认支持构造器注入,当然也可以用自己的IOC容器替换。See Dependency Injection for more details.

在ASP.NET 5 中服务有三种生命周期,singleton,scoped和transient. Transient是每次请求都会创建一次服务,Scoped是是在当前Scoped中如果不存在服务,就会创建,Singleton仅仅创建一次。对于Web应用,容器的scope对每个请求都是会创建一次,so you can think of scoped services as per request.

中间件##

在ASP.NET 5中,可以通过中间件来组成你自己的请求管道。ASP.NET 5中间件在HttpContext上执行异步逻辑操作,可选地调用管道中的下一个中间件或者直接终止请求。一般情况下,你可以通过调用在Configure方法中的IApplicationBuilder的对应的扩展方法来“使用”中间件。

ASP.NET 5附带一些预编译的中间件
1. Working with Static Files
2. Routing
3. Diagnostics
4. Authentication
当然你也可以写自己的中间件

服务器##

ASP.NET应用hosting model并不是直接监听请求,而是but instead relies on an HTTP server implementation to surface the request to the application as a set of feature interfaces that can be composed into an HttpContext.

Web Root

Web Root是ASP.NET 应用的根位置,在这里http请求的到处理(如处理静态文件)。在ASP.NET 5中Web Root 是通过project.json的webroot属性来配置的。

配置##

ASP.NET 5使用了新的配置模型来处理简单的name-value对,而不是基于System.Configuration或者web.config。This new configuration model pulls from an ordered set of configuration providers.内建的配置provider支持各种文件格式,如xml,json,ini,同时支持环境变量,实现基于环境的配置。你也可以写自己的配置provider。在ASP.NET5中,环境,如开发和生产,都是first-class概念,也是可以通过环境变量来设置的。(Environments, like Development and Production, are a first-class notion in ASP.NET 5 and can also be set up using environment variables):

var builder = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
    // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
    builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();

参见Configuration来了解更多关于新的配置系统, Working with Multiple Environments for details on how to work with environments in ASP.NET 5

客户端开发(Client-side development)##

ASP.NET 5 无缝集成了各种各样的客户端框架,如angularJS,KnockoutJs和BootStrap,详情见
Client-Side Development for more details.

posted @ 2016-01-11 18:27  sosoThink  阅读(426)  评论(0编辑  收藏  举报