nuget pack 不从 Visual Studio 项目中读取程序集版本

nuget pack doesn't read assembly version from Visual Studio project

我正在跟随 this official doc 使用 nuget.exe 创建 nuget 包。我正在尝试从 Visual Studio 项目中创建一个 nuspec 文件。

根据文档,当您 运行

> nuget spec

从您的 .csproj 文件所在的目录,它会创建一个标记化的 .nuspec 文件。然后,当您 运行 nuget pack <project-name>.csproj 项目文件中的值用于替换 nuspec 文件中的标记。

所以在我的示例中,我有一个名为 Logger1 的项目。 运行宁nuget spec后,我有以下.nuspec

<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>Yoav Klein</authors>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <license type="expression">MIT</license>
...

现在,this doc 说:

To use these tokens, run nuget pack with the project file rather than just the .nuspec. For example, when using the following command, the $id$ and $version$ tokens in a .nuspec file are replaced with the project's AssemblyName and AssemblyVersion values:

nuget pack MyProject.csproj

我的程序集版本是:

# AssemblyInfo.cs

// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.6.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

现在我 运行 nuget pack Logger1.csproj 并且我希望从项目的 AssemblyVersion 中提取包的版本,正如文档所承诺的那样。

创建包后,我检查创建的包中的 .nuspec 文件:

<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
  <metadata>
    <id>Logger1</id>
    <version>1.0.0</version>

如您所见,出于某种原因,它没有采用 AssemblyVersion 的正确值 1.6.0,而是始终使用值 1.0.0

谁能告诉我我做错了什么?

NuGet 版本:5.8.1.7021


确认一下,构建dll后,可以看到汇编版本是1.6.0:

PS> [Reflection.AssemblyName]::GetAssemblyName("$(pwd)\bin\Debug\Logger1.dll")

Version        Name
-------        ----
1.6.0.0        Logger1

这个问题看起来很像一个潜在问题。

我做了更多的测试,分析后发现,在打包过程中,总是出现XXXXX is required这样的错误信息。我确认了几次,并参考了official documents,其中提到令牌将被替换为我在AssemblyInfo.cs文件中设置的值。

例如,如果我按照承诺设置[assembly: AssemblyCompany("Company")],$author$ token 应该替换为“Company”,但它并没有被替换,而是报告了Authors is required 错误,这看起来 nuget.exe 根本没有检查我的 AssemblyInfo.cs 文件。

然后我找到了这个帖子:NuGet again throwing exceptions "authors is required"… ignoring csproj/nuspec replacement tokens。我看到一个 workaround/solution > 将下载的 nuget.exe 文件命名为 nuget.exe 文件和 right-click 文件 > 属性 > 检查旁边的 Unblock 选项This file came from another computer and might be blocked to help protect this computer. 消息。这似乎是解决方案,让 nuget.exe 正常工作以查找和检查 AssemblyInfo.cs 文件,然后替换标记。

简而言之(整个解决方案)

从官方网站下载nuget.exe并确保其名称为nuget.exe。然后 right-click nuget.exe 文件并选择属性。切换到常规选项卡,勾选 Unblock 选项。转到您的项目文件夹,首先清除项目缓存(删除 binobj 文件夹,在 VS 中关闭您的解决方案并删除隐藏的 .vs 文件夹,删除 .nuspec 文件和 .nupkg 文件),然后在命令提示符下使用 nuget.exe spec 生成一个新的 .nuspec 文件,将其编辑为您想要的。之后 运行 nuget.exe pack yourproject.csproj 命令行来打包你的 NuGet 包。

简而言之(原因)

在我看来,下载的 nuget.exe 文件被阻止了。