简体   繁体   中英

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

I'm following along this official doc about creating a nuget package with nuget.exe . I'm trying to create a nuspec file out of a Visual Studio project.

According to the docs, when you run

> nuget spec

From the directory where your .csproj file resides, it creates a tokenized .nuspec file. Then, when you run nuget pack <project-name>.csproj the values from the project file are used to replace the tokens in the nuspec file.

So in my example, I have a project called Logger1 . After running nuget spec , I have the following .nuspec

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

Now, this doc says that:

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

My Assembly version is:

# AssemblyInfo.cs

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

Now I run nuget pack Logger1.csproj and I expect the version of the package to be drawn from the AssemblyVersion of the project, as promised by the docs.

After creating the package, I examine the.nuspec file in the package created:

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

As you can see, for some reason, it doesn't take the correct value of the AssemblyVersion which is 1.6.0 , but rather always uses the value 1.0.0 .

Can someone tell me what am I doing wrong?

NuGet Version: 5.8.1.7021


Just to confirm, after building the dll, I can see that the assembly version is 1.6.0:

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

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

This issue looks quite like a potential issue.

I did more tests and, after analyzing, I saw that, during the packing process, always, there was an error message like XXXXX is required . I confirmed several times, and referred to the official documents , which mentioned that the tokens will be replaced with the value which set in my AssemblyInfo.cs file.

For example, if I set [assembly: AssemblyCompany("Company")] , as promised, the $author$ token should be replaced with "Company" but it wasn't replaced, instead it reported Authors is required error, which looks like nuget.exe didn't check my AssemblyInfo.cs file at all.

I then found this thread: NuGet again throwing exceptions "authors is required"… ignoring csproj/nuspec replacement tokens . And I saw one workaround/solution > name the downloaded nuget.exe file to nuget.exe file and right-click the file > property > check the Unblock option beside This file came from another computer and might be blocked to help protect this computer. message. And it seems it's the solution and let nuget.exe work normally to find and check AssemblyInfo.cs file then replace the tokens.

In short(whole solution)

Download nuget.exe from official website and make sure that its name is nuget.exe. Then right-click the nuget.exe file and choose Properties. Switch to General tab, check the Unblock option. Go to your project folder, clear the project cache first(delete bin , obj folders, close your solution in VS and delete the hidden .vs folder, remove the .nuspec file and .nupkg file), and then use nuget.exe spec in command prompt to generate a new .nuspec file, edit it to what you want. After that run nuget.exe pack yourproject.csproj command line to pack your NuGet package.

在此处输入图像描述

In short(reason)

In my opinion, the downloaded nuget.exe file is blocked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM