0

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!

Sara Liu - MSFT
  • 6,007
  • 1
  • 21
  • 27
SilverFish
  • 1,014
  • 6
  • 28
  • 65
  • 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 [MCVE])... – Alexei Levenkov Mar 31 '21 at 21:43
  • I updated post with code from nupec file. please let me know what other info would help – SilverFish Mar 31 '21 at 22:35
  • some posts refer to a lib folder. for eg. https://stackoverflow.com/questions/8764236/nuget-add-external-reference. I don't see lib folder in my project directory. where should it be? lib\net40 - does it mean .Net framework 4.0 – SilverFish Mar 31 '21 at 22:59
  • 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 Mar 31 '21 at 23:12
  • 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 Mar 31 '21 at 23:21
  • I always had my own Files section for NuGets that manually picked my own DLL files from bin folder... ( src="bin..." target="lib/...") so that why I wanted to see your nuspec (adding that definitely helped the question to be have info in general). Since you trying to rely on some automatic way to add your own files from project I have no idea about that. The one thing you should not need to add entry in files section for dependent nugets. Consider just specifying files yourself in NuSpec... – Alexei Levenkov Mar 31 '21 at 23:45
  • some examples I see node, while some places and some using both. When to use which? – SilverFish Apr 01 '21 at 00:01
  • @SilverFish, any update about this issue? – Sara Liu - MSFT Apr 05 '21 at 03:23

2 Answers2

0

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.

Sara Liu - MSFT
  • 6,007
  • 1
  • 21
  • 27
  • What goes under dependency and what goes under files? I see you mention ProjectTitle.dll goes under files. I have been putting same files (except project file) under both dependency and files). In Solution Explorer I have some dlls and some exes. In project.config I have just dlls eg.( ). Please advise. Also projectTitle.dll never gets copied to target="lib\net462" (that is incorrect what I have there) – SilverFish Apr 05 '21 at 13:46
  • files is to pack your extra project files into nupkg file while dependency is to add any other nuget dependencies or assembly dlls for your main project which installs the nuget package. More info you can refer to [this document](https://learn.microsoft.com/en-us/nuget/reference/nuspec). Files can pack various project contents into nodes with different functions under nupkg. See [this](https://learn.microsoft.com/en-us/nuget/reference/nuspec#including-assembly-files). – Sara Liu - MSFT Apr 07 '21 at 06:30
  • What did you mean by `project.config`? All of these should be under `nuspec` file. You should generate the nuspec file under the project folder which `xxx.cspro`j exists. Add these node under nuspec file, then add `......` and then use `cd xxx\project folder`, `nuget pack` to generate the new nupkg. You can use 7zip to unpack the nupkg to check if there is `ProjectTitle.dll` under it. – Sara Liu - MSFT Apr 07 '21 at 06:40
  • Also, `` should be added into `packages.config` file. When you finish it,you should run `update-package -reinstall` under `Tools`-->`Nuget Package Manager`-->`Package Manager Console` to install it. There is also [an issue about pack dlls](https://stackoverflow.com/questions/61538825/package-third-party-dll-in-context-of-multi-target-framework-project-net-frame/61587477#61587477). And you should check [nuget pack with nuspec document carefully](https://docs.microsoft.com/en-us/nuget/create-packages/creating-a-package). – Sara Liu - MSFT Apr 07 '21 at 06:54
  • Correction: I meant package.config and not project.config – SilverFish Apr 07 '21 at 21:24
  • [packages.config](https://docs.microsoft.com/en-us/nuget/reference/packages-config) is the file which record the installed nuget packages under your project folder. It is the name of nuget package rather than dll. You should note that. It is the same effect as nuget package manager-->input a nuget package name-->install.`Encryption` nuget package your own private package? I cannot find it under `nuget.org`. You should add its source which exists Encryption into [nuget package source](https://docs.microsoft.com/en-us/nuget/consume-packages/install-use-packages-visual-studio#package-sources). – Sara Liu - MSFT Apr 09 '21 at 03:30
  • 1
    Did you use my method to get the right package? Use `nuget pack xxx.csproj` rather than `nuget pack xxx.nuspec`. Please tell us your any feedback and we will help you further. Maybe you should make your issue more clear:) – Sara Liu - MSFT Apr 09 '21 at 03:32
  • nuget pack YOURProjectName.csproj -Properties Configuration=Release (command worked for me) – SilverFish Apr 30 '21 at 15:46
  • I had to use references instead of dependencies. I think because my dll is in .NET Framework and not .NET Standard. I know .NET standard is recommended, but the other Nuget packages being referenced in this dll (nuget package) were build using .NET Framework, so I could not use .NET standard. I will share my file structure in case anyone ever needs – SilverFish Apr 30 '21 at 15:55
0

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
SilverFish
  • 1,014
  • 6
  • 28
  • 65