您的位置:首页 > 编程语言 > ASP

ASP.NET Core project.json imports 是什么意思?

2016-12-01 16:45 465 查看
示例代码:

"frameworks": {
"netcoreapp1.0.0": { "imports" : "portable-net45+win81" }
}

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".

注:摘录自 Frameworks and imports sections in project.json: what are they?

上面这段解释什么意思?字面意思:
imports
是使用程序包的一种方式,虽然不是为这种框架设计的,基本上你告诉它“使用这些目标框架,即使它们似乎不被支持,但我知道我在做什么”,翻译的比较生硬,你可以这样理解:
imports
的作用是各个平台之间的兼容处理,并且针对的是不支持的程序包。

概念理解还是有些问题,再来看下面的解释。

摘录:What it says right here is that I’m targeting the netcoreapp1.0 framework (which is the default framework for a .NET Core application). With the “imports” bit I’m also stating that any dependency I want to include that targets dnxcore50 should be treated as compatible with netcoreapp1.0. Basically I’m instructing the tooling to ignore the fact that dnxcore50 isn’t the same thing as netcoreapp1.0 because I know it is the same thing (or more precisely Microsoft knows that they are the same).

翻译:需要说明的是,如果目标框架平台是 netcoreapp1.0,使用
imports dnxcore50
,就可以使 netcoreapp1.0 和 dnxcore50 平台兼容,并且使工具忽略 netcoreapp1.0 和 dnxcore50 不一样的事实,因为我知道它是相同的事情(或更确切的 Microsoft 知道他们是相同的)。

解释:举一个示例,比如 WebApi.Client 程序包,分别实现了 netcoreapp1.0 和 net451 平台版本(代码完全一样,只是基于的平台不一样),然后有一个应用程序是 netcoreapp1.0 平台的,然后它去引用 net451 平台的 WebApi.Client 程序包,在 VS2015 中会报不支持平台错误,虽然两种平台版本的代码完全一样,但还是不能跨平台饮用,
imports
就是解决这个问题的,并且使 VS2015 忽略这个错误。

摘录:A word of warning is in order here though. Theoretically I could “import” any framework that’s currently out there I can install pretty much any package into my project. However, that doesn’t mean that my application will actually run and work. If a package I installed into my project is using an API that’s not available on the platform that my application will be running on it will simply fail at runtime.

Therefore you should be careful when using “imports” and make sure that you test your application properly on the actual framework it will be running on. Think of it as when you’re using “imports” you’ve turned off the tooling and you are on your own and its your job to make sure that your application works correctly.

翻译:警告在这里,理论上,我可以
imports
任何框架平台,然后安装所有平台的程序包。然而,这并不意味着我的应用程序可以实际运行和工作。如果我安装到我项目的程序包,在这个平台上 API 不可用,我的应用程序在运行时将是失败的。

因此,在使用
imports
时应该小心,要确保在平台框架上,应用程序可以实际正常运行。想象下,当你使用了
imports
,你就关闭了 VS2015 的提示,你就要确保它是正常工作的。

解释:举个例子,比如 WebApi.Client 程序包,分别实现了 netcoreapp1.0 和 net451 平台版本(代码完全不一样,内部实现使用了不同平台的 API),然后有一个应用程序是 netcoreapp1.0 平台的,然后它去引用 net451 平台的 WebApi.Client 程序包,需要使用
imports net451
,在 VS2015 编译完全没什么问题,但在 netcoreapp1.0 平台实际运行的时候,就会出错了,因为不同平台的 WebApi.Client 程序包,使用了不同平台的 API。

注:摘录自 .NET Platform Standard and the magic of “imports”

翻译的比较差,但大概意思是懂了,总的来说,使用
imports
,只是解决了在 VS2015 中平台不支持的错误提示,但并不一定说明实际是正常运行的,所以,要在实际运行环境中,测试引用其他平台的程序包,执行是否有问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: