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

查看:45
本文介绍了将纯内容包导入到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="content\JsonSchemas" />
    </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工具安装,它似乎不是由您引起的直接打包.您确定通过NuGet软件包管理器或

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软件包之前,已完全删除旧的NuGet软件包,尤其是如果您未在NuSpec中更改软件包的版本.一个常见的问题是改用旧的缓存程序包.要

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-全部清除
  • nuget.exe 中,使用 locals-全部清除
  • 在Visual Studio> = 2017中,转到工具> NuGet程序包管理器> Package Manager设置,然后单击清除所有NuGet缓存
  • 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?

带有 PackageReference 的NuGet 4.0+使用 contentFiles ,请参阅此

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

使用元素将内容文件包含在包中,并在target属性中指定内容文件夹.但是,当使用 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 目录.您必须确保它们位于 contentFiles \ any \ any \ 下,否则它们将不会被提取到具有任何目标框架的项目中.

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 contentFiles\any\any\, otherwise they will not be extracted to projects with any target framework.

<file src="JsonSchemas\*.*" target="contentFiles\any\any\JsonSchemas" />

下面给出了程序包中的路径,因此第一个路径段代表代码语言,第二个路径段代表目标框架的绰号.在这种情况下,您必须使用 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="any\any\JsonSchemas\*.json" buildAction="Content" copyToOutput="true" flatten="false" />
        </contentFiles>
    </metadata>
    <files>
        <file src="JsonSchemas\*.*" target="content\JsonSchemas" />
        <file src="JsonSchemas\*.*" target="contentFiles\any\any\JsonSchemas" />
    </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文件中设置.为了向后兼容,最好不显式设置Dependency类型,而是在未指定类型的情况下依靠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天全站免登陆