0
votes

I'm trying to create Nuget package from a Visual Studio 2017 class Library first time. It is a .NET Framework 4.6.2 project. The class library is referencing some other nuget packages, dlls, exes which are in References section under Solution Explorer.

Here are the steps I took after looking at some youtube videos and Microsoft documentation:

Right click project and select Properties. Build option, set Configuration to Release. Saved and closed project properties. Opened csproj file and changed Configuration to Release

<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>

Now build the project in Release mode. I can see dlls under

MyProject\bin\Release and also under MyProject\bin\Debug

Then I create the spec file using

nuget spec Opened it and made appropriate changes and then

nuget pack MyProject.nuspec

I am getting number of warnings like both for Debug and Release directory:

WARNING: NU5100: The assembly 'bin\Debug\Encryption.dll' is not inside the 'lib' folder and hence it won't be added as a reference when the package is installed into a project. Move it into the 'lib' folder if it needs to be referenced.

although the Class Library (which I am creating Nuget), has a packages.config and has references:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Encryption" version="1.1.0" targetFramework="net462" />
    ...
    ...
    ...
<package id="TeraData" version="16.20.8" targetFramework="net462" />
</packages>

Since I am getting warnings, I tried entering dependency information in the nuspec file. Here is what my nuspec file looks like

   <?xml version="1.0" encoding="utf-8"?>
    <package >
      <metadata>
        <id>ProjectTitle</id>
        <version>1.0.0</version>
        <title>ProjectTitle</title>
        <authors>auther name</authors>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>desc of package</description>
        <releaseNotes>release notes text</releaseNotes>
        <copyright>Copyright info</copyright>
        <tags>some tages</tags>
    
        <dependencies>
          <dependency id="Encryption" version="1.1.0" />
    ...
          <dependency id="TeraData" version="16.20.8" />
        </dependencies>
      </metadata>
    </package>

But still get same warnings. If you can please provide a sample how dependency info in nuspec should look like, that would really help! Please advise if I'm missing anything!

2
Since error is about nuspec file it is impossible to figure out what exactly you get wrong there... Generally we trust compiler more than users... unless there is clear evidence in the post (like minimal reproducible example)...Alexei Levenkov
I updated post with code from nupec file. please let me know what other info would helpSilverFish
some posts refer to a lib folder. for eg. stackoverflow.com/questions/8764236/…. I don't see lib folder in my project directory. where should it be? lib\net40 - does it mean .Net framework 4.0SilverFish
Is "bin\Debug\Encryption.dll" your DLL or part of "Encryption" NuGet? I don't think you actually need to do anything in that case as dependency should correctly install that NuGet... Have you tried to add your newly created NuGet to a blank project to see what actually happens? I'd be very concerned to include external DLL from my Bin folder (as it likely cause mismatches down the road for users of the nuget).Alexei Levenkov
Yes, I did try referencing it from another project and here is what I get: Could not install package MyNugetPackage 1.0.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6.2', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.SilverFish

2 Answers

0
votes

I think it's just a problem with the command of your nuget pack method.

We usually do not use nuget pack xxx.nusepc command to pack a nuget package because it cannnot pack the realated dll,pdb files including the main nuget project's dll automatically into the nupkg.

You have to write the whole nuspec node with it. You have to write <files> node in nuspec file to include your main project's dll so that it will remove the warning of missing dependencies. You should not add <references> node additionally.

like:

 <?xml version="1.0" encoding="utf-8"?>
    <package >
      <metadata>
        <id>ProjectTitle</id>
        <version>1.0.0</version>
        <title>ProjectTitle</title>
        <authors>auther name</authors>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>desc of package</description>
        <releaseNotes>release notes text</releaseNotes>
        <copyright>Copyright info</copyright>
        <tags>some tages</tags>
    
        <dependencies>
          <dependency id="Encryption" version="1.1.0" />
    ...
          <dependency id="TeraData" version="16.20.8" />
        </dependencies>
      </metadata>
<files>
<file src="bin\Release\ProjectTitle.dll" target="lib\net462" />

.....
</files>
    </package>

Then, use nuget pack xxx.nuspec -Properties Configuration=Release command to pack it. You should pack the the main project' dll in this way. And if your project refences other assembly dlls or extra exe files.

You should add them:

  <file src="bin\Release\extra_assembly.dll" target="lib\net462" />
  <file src="bin\Release\extra_exe.exe" target="lib\net462" />

=========================================

However, this function is not very convenient. And we usually do not need them, we usually use this:

nuget pack xxx.csproj

Usually, we use nuget pack xxx.csproj -Properties Configuration=Release to pack without any other node. Before this, you should cd xxx\<project folder>.

use this nuspec file:

 <?xml version="1.0" encoding="utf-8"?>
        <package >
          <metadata>
            <id>ProjectTitle</id>
            <version>1.0.0</version>
            <title>ProjectTitle</title>
            <authors>auther name</authors>
            <requireLicenseAcceptance>false</requireLicenseAcceptance>
            <description>desc of package</description>
            <releaseNotes>release notes text</releaseNotes>
            <copyright>Copyright info</copyright>
            <tags>some tages</tags>
        
            <dependencies>
              <dependency id="Encryption" version="1.1.0" />
        ...
              <dependency id="TeraData" version="16.20.8" />
            </dependencies>
          </metadata>
  <!--If you have any other referenced assembly dll files or pdb files, exe files, you should add them here.-->
    <files>
    .....
    </files>
        </package>

You should not add your main nuget project's dll with <file> node and it will add into your nupkg automatically with that command.

When you create the new release version of your nuget package, first uninstall the old one under your project, then delete all cache files under C:\Users\xxx\.nuget\packages. After that, reinstall the new release one in your new project.

0
votes

Here is the nuspec file structure using .NET framework which finally worked for me:

   <?xml version="1.0" encoding="utf-8"?>
    <package >
      <metadata>
        <id>ClasslibProj </id>
        <version>1.0.0.0</version>
        <title> ClasslibProj</title>
        <authors>author(s) name</authors>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>desc</description>
        <releaseNotes>release notes</releaseNotes>
        <copyright>Copyright @ Company name 2021</copyright>
        <tags>tags to search </tags>
        <references>
          <group targetFramework=".NETFramework4.6.2">
            <reference file="SomeOtherNugetpackage1.dll"/>
           <reference file="anyexecutable.exe"/>
    …
    
            <reference file="ClasslibProj.dll"/> //dll you are working with
          </group>
        </references>
      </metadata>
      <files>
        <file src="bin\Release\SomeNugetOtherpackage1.dll" target="lib\net20"/>
       <file src="bin\Release\anyexecutable.exe" target="lib"/>
    ..
        <file src="bin\Release\ClasslibProj.dll" target="lib\net462"/>
      </files>
    </package>

Build project in Release mode. use command:

nuget pack ClasslibProj.csproj 

As mentioned by Sara Liu, avoid using ClasslibProj.nuspec
or you may use detailed command:

nuget pack ClasslibProj.csproj -Properties Configuration=Release