ASP.NET编程实战:理解 .NET Platform Standard
安安 2017-12-07 来源 :网络 阅读 1100 评论 0

摘要:本篇ASP.NET编程实战教程将为大家讲解ASP.NET编程的知识点,看完这篇文章会让你对ASP.NET编程的知识点有更加清晰的理解和运用。

本篇ASP.NET编程实战教程将为大家讲解ASP.NET编程的知识点,看完这篇文章会让你对ASP.NET编程的知识点有更加清晰的理解和运用。


.NET Platform Standard 是什么?直译过来就是 .NET 平台规范或标准,它的目的就是使 .NET 各个平台之间更加统一和规范,在之前的 .NET Core RC2 发布文章中提到了 .NET Standard Library,它其实就是 .NET Platform Standard 的体现之一,.NET Standard Library 现在有一个对应程序包NETStandard.Library,它的作用是兼容各个 .NET Platform,这个后面有进行说明,现在只是一个临时方案,以后微软慢慢会把相关的程序包(比如基础类库等),按照 .NET Standard Library 的标准进行开发和发布。

.NET Platform Standard 列表:

Target Platform Name Alias

.NET Platform Standard netstandard 1.0 1.1 1.2 1.3 1.4 1.5 1.6

.NET Core netcoreapp → → → → → → 1.0

.NET Framework net → → → → → → 4.6.3

→ → → → → 4.6.2

→ → → → 4.6.1

→ → → 4.6

→ → 4.5.2

→ → 4.5.1

→ 4.5

Universal Windows Platform uap → → → → 10.0

Windows win → → 8.1

→ 8.0

Windows Phone wpa → → 8.1

Windows Phone Silverlight wp 8.1

8.0

Mono/Xamarin Platforms → → → → → → *

Mono → → *

上面这些都是概念,我们在 ASP.NET Core 1.0 RC2 项目开发中,如何应用和体现呢?其实就是我们在project.json中配置的frameworks节点,我们先看一段配置(来自Microsoft.EntityFrameworkCore/project.json):

"frameworks": {

  "net451": {

    "frameworkAssemblies": {

      "System.ComponentModel.DataAnnotations": "",

      "System.Runtime": {

        "type": "build"

      }

    }

  },

  "netstandard1.3": {

    "imports": [

      "portable-net452+win81"

    ],

    "dependencies": {

      "System.Collections.Concurrent": "4.0.12-*",

      "System.ComponentModel.Annotations": "4.1.0-*",

      "System.Linq.Queryable": "4.0.1-*",

      "System.ObjectModel": "4.0.12-*",

      "System.Reflection.Extensions": "4.0.1-*",

      "System.Reflection.TypeExtensions": "4.1.0-*"

    }

  },

  "netcore50": {

    "dependencies": {

      "Microsoft.NETCore.Platforms": {

        "type": "build",

        "version": "1.0.1-*"

      },

      "System.Collections.Concurrent": "4.0.10",

      "System.ComponentModel.Annotations": "4.0.10",

      "System.Linq.Queryable": "4.0.0",

      "System.ObjectModel": "4.0.10",

      "System.Reflection.Extensions": "4.0.0",

      "System.Reflection.TypeExtensions": "4.0.0",

      "System.Runtime": {

        "type": "build",

        "version": "4.0.20"

      },

      "System.Dynamic.Runtime": {

        "type": "build",

        "version": "4.0.10"

      },

      "System.Runtime.WindowsRuntime": {

        "type": "build",

        "version": "4.0.10"

      },

      "System.Runtime.Extensions": {

        "type": "build",

        "version": "4.0.10"

      }

    }

  }}

可以看到frameworks配置了net451、netstandard1.3和netcore50,这些是什么意思?从上面的 .NET Platform Standard 列表中,我们可以得到一些信息,但还是有些不太明白,我们看一下相关解释(来自 Project.json definition dnx451 vs .dotnet (4.51)):

dnxcore50: DNX SDK running on CoreCLR/CoreFx (deprecated, use netcoreapp1.0 instead).

dnx451: DNX SDK running on .Net 4.5.1 (Desktop CLR / Full BCL and FCL) (deprecated, use net451 instead).

net46: .Net Framework 4.6 SDK running on Desktop CLR / Full BCL and FCL.

uap10.0: UWP Windows 10 SDK running on .Net Native/CoreFx.

netcoreapp1.0: .NET Core 1.0 SDK running on CoreCLR/CoreFx.

netstandard1.5: (RC2, dotnet before) any pure IL code which declares its dependencies (System.Runtime (based) libraries instead of a PCL contracts). Framework dependencies are available for .Net 4.5.x onwards, .NET Core or UWP (System.Runtime based library set in different versions). As with RC2 dotnet is deprecated, use netstandard instead.

先看dnxcore50的解释,DNX SDK 是什么?它其实是一种命令或工具,指向你的程序集使用的哪种目标环境,CoreCLR/CoreFx 就是说,程序集跑在 CoreCLR/CoreFx 上,dnxcore50现在已经被弃用了,被 netcoreapp1.0所替代,netstandard1.5是一种新的平台规范,使用它必须引用NETStandard.Library程序包,否则System所有相关命名空间都找不到。

简单来说,frameworks所配置的就是你程序集的运行环境或平台,如果配置了多个,就表示程序集可以跑在多个平台上,比如,上面Microsoft.EntityFrameworkCore配置了net451、netstandard1.3和netcore50,也就是说Microsoft.EntityFrameworkCore可以被这三种平台的程序集引用,比如你的程序集frameworks中只配置了net451,照样可以引用Microsoft.EntityFrameworkCore程序集,只不过只能在 Windows 上运行,不能跨平台而已,一个程序集不同平台的代码写法:

#if DNX451

    //Code here for dnx451#elif DNXCORE50

    //code here for dnxcore50#endif

imports的解释是(来自 Frameworks and imports sections in project.json: what are they?):imports is a way to use packages that were not designed for that framework. Basically you tell it "Use those targets even though they don't seem to be supported. I know what I'm doing". 简单来说,就是兼容本程序集配置平台所不支持的平台,有点绕,我们做一个测试就清楚了:

ASP.NET编程实战:理解 .NET Platform Standard

如上图的配置,为什么frameworks配置了netcoreapp1.0会出现错误?因为我们引用的Microsoft.EntityFrameworkCore程序包并不完全支持netcoreapp1.0平台,所以我们需要在netcoreapp1.0下增加"imports": ["net451"]配置,其作用就是使之兼容,当然现在只是兼容平台的配置,以后完善之后,这个配置会去掉的。

ASP.NET编程实战:理解 .NET Platform Standard

imports 的一段配置:

"netcoreapp1.0": {

  "imports": [

    "net461",

    "portable-net45+win81"

  ]}

首先,imports 可以配置多个节点,portable-net45+win81是什么意思?portable 的意思是便携式的,在之前的博文中有提及,意思就是自身发布不携带依赖的程序包,而是使用系统中安装配置的,net45就是上面说的frameworks配置,win81是系统平台的意思,但不只是特指 Windows 8.1 系统。

Platform NuGet identifier

.NET Framework 2.0 - 4.6 net20 - net46

.NET Core netcoreapp

.NET Micro Framework netmf

Windows 8 win8, netcore45

Windows 8.1 win8, netcore451

Windows Phone Silverlight (8, 8.1) wp8, wp81

Windows Phone 8.1 wpa8.1

Universal Windows Platform 10 uap10, netcore50

Silverlight 4, 5 sl4, sl5

MonoAndroid monoandroid

MonoTouch monotouch

MonoMac monomac

Xamarin iOS xamarinios

Xamarin PlayStation 3 xamarinpsthree

Xamarin PlayStation 4 xamarinpsfour

Xamarin PlayStation Vita xamarinpsvita

Xamarin Watch OS xamarinwatchos

Xamarin TV OS xamarintvos

Xamarin Xbox 360 xamarinxboxthreesixty

Xamarin Xbox One xamarinxboxone

最后,再做一个测试,这个我们一般在 ASP.NET 5 Core 1.0 RC1 升级到 RC2 中会遇到的,两个程序集:

CNBlogs.Ad.Infrastructure.Interfaces

CNBlogs.Ad.Infrastructure: 依赖于CNBlogs.Ad.Infrastructure.Interfaces

CNBlogs.Ad.Infrastructure.Interfaces的project.json配置:

{

  "version": "1.0.0-*",

  "description": "CNBlogs.Ad.Infrastructure.Interfaces Class Library",

  "authors": [ "xishuai" ],

  "frameworks": {

    "netcoreapp1.0": {

      "imports": [

        "net451"

      ]

    },

    "net451": { }

  },

  "dependencies": {

    "Microsoft.EntityFrameworkCore": "1.0.0-rc2-final",

    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-final"

  }}

CNBlogs.Ad.Infrastructure的project.json配置:

{

  "version": "1.0.0-*",

  "description": "CNBlogs.Ad.Infrastructure Class Library",

  "authors": [ "xishuai" ],

  "frameworks": {

    "netcoreapp1.0": {

      "imports": [

        "net451"

      ]

    },

    "net451": { }

  },

  "dependencies": {

    "CNBlogs.Ad.Infrastructure.Interfaces": "1.0.0-*",

    "Microsoft.EntityFrameworkCore": "1.0.0-rc2-final",

    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-final"

  }}

几种测试情况:

CNBlogs.Ad.Infrastructure.Interfaces中的netcoreapp1.0去除"imports": ["net451"]配置:出现错误,上面有过分析,因为Microsoft.EntityFrameworkCore并不完全支持netcoreapp1.0。

CNBlogs.Ad.Infrastructure.Interfaces去除netcoreapp1.0配置:出现错误,因为CNBlogs.Ad.Infrastructure配置了netcoreapp1.0,而引用的CNBlogs.Ad.Infrastructure.Interfaces却支持net451。

CNBlogs.Ad.Infrastructure.Interfaces去除net451配置:出现错误,同上,因为CNBlogs.Ad.Infrastructure配置了net451,而引用的CNBlogs.Ad.Infrastructure.Interfaces却支持netcoreapp1.0。

CNBlogs.Ad.Infrastructure去除netcoreapp1.0配置:成功,因为依赖的CNBlogs.Ad.Infrastructure支持net451。

CNBlogs.Ad.Infrastructure去除net451配置:出现成功,同上,因为依赖的CNBlogs.Ad.Infrastructure支持netcoreapp1.0。

综合上面的测试,简单来说,就是程序包的运行平台或环境取决于底层的引用,底层的引用指的是你自己项目中的程序包,而不是基础类库和微软开发的程序包,因为它们都支持多平台,比如上面的Microsoft.EntityFrameworkCore程序包。

另外,如果你的程序包frameworks配置的是net451,它其实和 .NET Core 没多大关系了,因为它使用的是 .NET Framework 和 Desktop CLR,而不是 CoreCLR 和 CoreFx,即使你项目中使用的是 .NET Core RC2 的程序包。

参考资料:

Project.json definition dnx451 vs .dotnet (4.51)

Target Frameworks

Project.json Frameworks

What frameworks are available in ASP.NET Core (ASP.NET 5) applications?

.NET Platform Standard

Frameworks and imports sections in project.json: what are they?

All about httpRuntime targetFramework

Project.json Usage

以上,关于ASP.NET编程的全部内容讲解完毕啦,欢迎大家继续关注!更多关于ASP.NET编程的干货请关注职坐标ASP.NET频道!


本文由 @安安 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程