Importing content-only package to DotNet Core 3.1 project in Visual Studio 16.6

守給你的承諾、 提交于 2021-01-29 18:02:38

问题


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.

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

Here is a .nuspec file content

<?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 example:

{
  "$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" ]
}

回答1:


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.

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.

  • 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)

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

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

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.

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

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}

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.

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.



来源:https://stackoverflow.com/questions/63296311/importing-content-only-package-to-dotnet-core-3-1-project-in-visual-studio-16-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!