将纯内容包导入到 Visual Studio 16.6 中的 DotNet Core 3.1 项目 [英] Importing content-only package to DotNet Core 3.1 project in Visual Studio 16.6

查看:27
本文介绍了将纯内容包导入到 Visual Studio 16.6 中的 DotNet Core 3.1 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义并创建了一个纯内容包​​来在不同项目之间共享 JSON 模式.我使用nuget.exe打包,成功添加到.Net Framework 4.6库项目中.

I have defined and created a content-only package to share JSON schemas between different projects. I packaged it using nuget.exe, and could successfully add it to .Net Framework 4.6 library project.

但是当我尝试将其添加到 DotNet Core 3.1 库项目(NUnit 测试)时,发生了以下错误:

But when I tried to add it to the DotNet Core 3.1 library project (NUnit tests) the following error occurred:

NU1212 Invalid project-package combination for <package name>. DotnetToolReference project style can only contain references of the DotnetTool type

Nuget 支持文档(包类型内容文件) 未列出对纯内容包的任何限制(超出假设它们是兼容的").问题是如何创建与 DotNet Core 3.1 库兼容的纯内容 Nuget 包?

Nuget support documentation (package type, content files) does not list any restrictions (beyond "assuming they are compatible") on the content-only packages. Question is how do I create DotNet Core 3.1 library compatible content-only Nuget package?

我尝试按照 中的建议禁用除本地数据源之外的所有数据源这个问题,但这没有任何区别.

I tried disabling all data sources except the local one as suggested in this question, but that didn't make any difference.

这是一个.nuspec文件内容

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
        <id>Package.JsonSchemas</id>
        <version>0.0.1</version>
        <packageTypes>
            <packageType name="Dependency" />
        </packageTypes>
        <authors>me</authors>
        <owners>me</owners>
        <releaseNotes>Fill in later</releaseNotes>
        <description>Set of JSON schemas.</description>
        <tags>json, json-schema, tdv</tags>
        <contentFiles>
            <files include="JsonSchemas*.json" buildAction="Content" copyToOutput="true" flatten="false" />
        </contentFiles>
    </metadata>

    <files>
        <file src="JsonSchemas*.*" target="contentJsonSchemas" />
    </files>
</package>

架构示例:

{
  "$schema": "https://json-schema.org/draft/2019-09/schema",

  "$defs": {
    "ArrayItem": {
      "type": "object"
    }
  },

  "title": "dataset object",
  "type": "object",

  "properties": {
    "Data": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/ArrayItem"
      },
      "default": []
    }
  },
  "required": [ "Data" ]
}

推荐答案

NU1212 错误确实指向 dotnet tool install,它似乎不是由您的直接打包.您确定通过 NuGet 包管理器或 控制台?它不能在 .NET Core 3.1 库或 NUnit 项目类型中重现.

The NU1212 error does point to dotnet tool install, it does not seem to be caused by your package directly. Are you sure that your are adding your package correctly via the NuGet package manager or console? It is not reproducible in a .NET Core 3.1 library or NUnit project type.

正如@Perry Qian-MSFT 所建议的,在添加新包之前,您应该始终确保完全删除旧的 NuGet 包,特别是如果您没有更改 NuSpec 中的包版本.使用旧的缓存包是一个常见问题.清除所有 NuGet 包缓存 使用以下命令之一.

As @Perry Qian-MSFT suggested, you should always make sure that the old NuGet package is removed completely before you add a new one, especially if you did not change the package version in the NuSpec. It is a common issue that the old, cached package is used instead. To clear all NuGet package caches use one of the following commands.

  • dotnet.exe 中使用 locals --clear all
  • nuget.exe 中使用 locals -clear all
  • 在 Visual Studio >= 2017 中,转到 工具 >NuGet 包管理器 >Package Manager Settings 并点击Clear All NuGet Cache(s)
  • In dotnet.exe use locals --clear all
  • In nuget.exe use locals -clear all
  • In Visual Studio >= 2017 go to Tools > NuGet Package Manager > Package Manager Settings and click Clear All NuGet Cache(s)

问题是如何创建与 DotNet Core 3.1 库兼容的纯内容 Nuget 包?

Question is how do I create DotNet Core 3.1 library compatible content-only Nuget package?

NuGet 4.0+ with PackageReference 使用 contentFiles,请参阅此 参考.

NuGet 4.0+ with PackageReference uses contentFiles, see this reference.

内容文件包含在使用元素的包中,在目标属性中指定内容文件夹.但是,当使用 PackageReference 将包安装到项目中时,此类文件将被忽略,而是使用元素.

Content files are included in a package using the element, specifying the content folder in the target attribute. However, such files are ignored when the package is installed in a project using PackageReference, which instead uses the element.

为了兼容性,您可以继续将文件复制到 content 目录,但也必须将它们复制到 contentFiles 目录.您必须确保它们位于 contentFilesanyany 下,否则它们将不会被提取到具有任何目标框架的项目中.

You can keep copying the files to the content directory for compatibility, but you have to copy them to the contentFiles directory, too. You have to make sure that they are located under contentFilesanyany, otherwise they will not be extracted to projects with any target framework.

<file src="JsonSchemas*.*" target="contentFilesanyanyJsonSchemas" />

包中的路径如下所示,所以第一个路径段代表代码语言,第二个是目标框架名称.在 boh 情况下,您必须使用 any.

The path within the package is given below, so the first path segment represents the code language, the second the target framework moniker. You have to use any in boh cases.

/contentFiles/{codeLanguage}/{TxM}

以下是适用于 contentFiles 的示例 NuSpec,它也适用于 .NET Core 3.1.

Below is your sample NuSpec adapted to contentFiles that will also work in .NET Core 3.1.

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
        <id>Package.JsonSchemas</id>
        <version>0.0.1</version>
        <authors>me</authors>
        <owners>me</owners>
        <releaseNotes>Fill in later</releaseNotes>
        <description>Set of JSON schemas.</description>
        <tags>json, json-schema, tdv</tags>
        <contentFiles>
            <files include="anyanyJsonSchemas*.json" buildAction="Content" copyToOutput="true" flatten="false" />
        </contentFiles>
    </metadata>
    <files>
        <file src="JsonSchemas*.*" target="contentJsonSchemas" />
        <file src="JsonSchemas*.*" target="contentFilesanyanyJsonSchemas" />
    </files>
</package>

来自相同的来源你链接了,为了向后兼容,建议不要显式指定依赖类型,所以我省略了.

From the same source that you linked, it is recommended not to specify the dependency type explicitly for backwards compatibility, so I left it out.

包类型在 .nuspec 文件中设置.为了向后兼容,最好不显式设置依赖类型,而是在未指定类型时依赖 NuGet 假设此类型.

Package types are set in the .nuspec file. It's best for backwards compatibility to not explicitly set the Dependency type and to instead rely on NuGet assuming this type when no type is specified.

这篇关于将纯内容包导入到 Visual Studio 16.6 中的 DotNet Core 3.1 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆