select project.json 中的依赖类型是什么以及如何

What is and how to select the type of dependency in project.json

我找不到有关 "build" 和 "platform"(可能还有其他一些)依赖类型之间的区别以及默认类型是什么的信息。

例如,我可以使用

添加依赖项
"Microsoft.Extensions.JsonParser.Sources": "1.0.0"

"Microsoft.Extensions.JsonParser.Sources": {
  "type": "build",
  "version": "1.0.0"
},

"Microsoft.Extensions.JsonParser.Sources": {
  "type": "platform",
  "version": "1.0.0"
},

那么如何选择呢? Official documentation 还没有包含这方面的信息。

文档中确实有说明(至少对于平台而言),您只需要查看正确的位置:)

您可以在 .NET Core App Types 页面找到它。基本上,"platform" 意味着从已安装的运行时使用它,并且当您 publish/deploy 应用程序时不会打包。

基本上,您对便携式应用程序使用 "platform",对独立应用程序不使用类型。

我建议避免手动编辑这个文件,这样你就不用太担心它了。另外,还有project.jsonplans to remove it entirely - such that you will no longer even use a project.json file to define dependencies. According to the JSON SchemaStore definitiontype键是这样定义的:

"type": {
    "type": "string",
    "default": "default",
    "enum": [ "default", "build", "platform" ]
  • build = 只需要构建项目的依赖,build-time dependency

  • platform = 预计驻留在您的项目配置为 target.

    的平台上的依赖项

The type "platform" property on that dependency means that at publish time, the tooling will skip publishing the assemblies for that dependency to the published output.

如果您检查 dotnet 存储库中 ProjectReader.cs 的源代码,您将看到它如何将此 JSON 序列化为一个对象,并且在此object我们可以找到意思(上面有详细介绍)。

补充阅读