【问题标题】:Contentfile in nuget gets compiled causing build errorsnuget 中的内容文件被编译导致构建错误
【发布时间】:2020-01-31 12:17:57
【问题描述】:

我有一个包含内容文件的 nuget。它是一个json file,并在打包nuget 时在nuspec 文件中表示。

在常规 wpf 应用程序中安装 nuget 时,我遇到了几个构建错误。看来Visual Studio 正在尝试编译.json file。想知道是否有人知道可能导致这种情况的原因。正如您将在 nuspec sn-p 中看到的,我在 nuspec 中有 BuildAction=None

ContentFile.json(1,1,1,2):错误 CS1022:类型或命名空间定义,或预期文件结尾

ContentFile.json(4,16,4,26):错误 CS1001:需要标识符

ContentFile.json(4,37,4,45):错误 CS1001:需要标识符

【问题讨论】:

  • 为此,您应该在<file> 节点下添加<file src="**\Resources\ContentFile.json" (the reative path of the file in the current project folder) target="ContentFiles/any/any" />。此外,<contentfiles> 节点下的files include="xxxxx" 是相对于nuget 包内contentFiles 文件夹的路径。所以在这一点上,应该是<files include="any/any/ContentFiles.json" buildAction="None" copyToOutput="true" flatten="false" />
  • 那么在你打包这个项目然后安装到另一个项目之后,别忘了clean the nuget cache
  • 嗨,这个问题有更新的吗?请按照我的指导解决您的问题,如果对您有帮助,请考虑accepting it。如果您的问题仍然存在,请随时告诉我们。这只是一个提醒。

标签: json nuget nuget-package visual-studio-2019


【解决方案1】:

在常规 wpf 应用程序中安装 nuget 时,我得到了几个构建 错误。似乎 Visual Studio 正在尝试编译 .json 文件。想知道是否有人知道可能导致这种情况的原因。正如你将看到的 nuspec sn-p 我在 nuspec 中有 BuildAction=None

我认为重点是你错误地认为<files include="path">是你当前nuget项目中源文件ContentFile.json的路径,实际上是相对于contentFiles文件夹的路径在nuget包内(xxxx.nupkg)。(可以通过解压xxx.nupkg文件查看contentFiles文件夹)。

此外,您可以查看this official document以获取更多详细信息。

作为正确的指导,您可以尝试此步骤:

解决方案

xxx.nuspec 文件中更改为这些

....
<metadata>
....
<contentFiles>
<files include="any/any/ContentFile.json"(the relativepath of the file under the ContentFiles folder in the xxx.nupkg) buildAction="None" copyToOutput="true" flatten="false" />
</contentFiles>
</metadata>

<files>
<file src="xxxxx/Resource/ContentFile.json(the relativePath of the file under the nuget project)" target="contentFiles/any/any" />
</files>
....

在将这个包安装到另一个项目之前,不要忘记clean the nuget cache

此外,您可以查看this thread了解更多信息。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 2016-01-22
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    相关资源
    最近更新 更多