Creating NuGet package with reference to a non-NuGet reference - nuget

I am creating a Class Library that builds 2 dlls into a NuGet package. It has a few references to dlls that currently do not have a NuGet package to be referenced from.
How should I make my NuGet package dependent on those dlls that are currently unavailable via NuGet?
If I bundle them up as well, what happens if a project that already has a reference to these dlls, pulls down my NuGet package, what happens to that reference?
Should I just create a NuGet package for each dll reference and make my NuGet package dependent on these?

You can bundle the DLLs into your NuGet package with no ill effects. A project that already has those DLLs in some /libs (or whatever) folder will continue to reference them from there. Assemblies in your NuGet package will reference the bundled DLLs that are pulled into /packages.
In your nuspec file, use the <file> element to include the internal DLLs, as such:
<package>
<metadata>
...
</metadata>
<files>
<file src="PATH_TO_BIN\DependencyOne.dll" target="mylibs" />
<file src="PATH_TO_BIN\DependencyTwo.dll" target="mylibs" />
</files>
</packages>
This will result in the following file structure when the NuGet package is pulled:
PATH_TO_PROJECT/packages/YOUR_NUGET_PACKAGE/mylibs/DependencyOne.dll
PATH_TO_PROJECT/packages/YOUR_NUGET_PACKAGE/mylibs/DependencyTwo.dll
The target attribute can specify any arbitrary path relative to your package root.

Related

How to I make a nuget package referenced by the nuget package I'm creating get installed as a top-level packagereference?

I'm creating a nuget package that uses refit as a dependency and when consuming the produced package I need to ensure refit gets installed as a top level package in the consuming project instead of a transitive reference.
I'm using the new csproj file format to create the nuget package and currently referencing refit like this:
<ItemGroup>
...
<PackageReference Include="Refit" Version="4.0.1" />
...
</ItemGroup>
Is there an attribute I can specify to make sure this gets added as a top-level package reference when my nuget package gets consumed?

Nuget not including mscorlib.dll

Nuget ignores adding references to a project when the DLL is named mscorlib.dll. Any way force nuget to add a references to the project, or copy the DLL to the bin output directory?
Here's the snippet from the .nuspec file:
<files>
<file src="..\..\bin\mscorlib.dll" target="lib\net45" />
</files>
The necessity is for deploying an alternative implementation of mscorlib.

How can I include a file with a NuGet package, and then add that file to a project when the NuGet package is installed?

I have a project that includes 2 files:
ThisProject.config.example
ThisProject.xsd
This project is packaged as a Nuget package, which is then added to other projects. When this happens I would like these files copied into the projects. The user then copies ThisProject.config.example to ThisProject.config and edits that file.
I understand that within a .nuspec file there are two ways to include files. These are:
<files>
<file src="ThisProject.config.example" target="ThisProject.config.example" />
<file src="ThisProject.xsd" target="ThisProject.xsd"/>
</files>
Doing this, I see that when I install the package into the /packages folder that the files have been copied over. Which is good. However, then it would seem that I need to use a .ps script to add them to the project that the nuget package is installed to?
I haven't tried this yet, but according to THIS link, there are three scripts that can be incorporated into NuGet packages:
Init.ps1
Install.ps1
Uninstall.ps1
But No. 2 and 3 are obsolete from VS 2017 onwards? The link that is provided for explanation points to information on MSBuild. But there is no explicit information on flatfile types so far as I can see.
Then. Supposedly there is a <contentFiles> tag. But according to THIS post (NuGet blog) only works when a package is added to certain project types. And does not work with package.config files...
What is the expected way of adding a file to a project from a NuGet package??
I have found that if you put files within certain folder structures that they are implicitly added to the package and also added to the project root directtory. However I still have not found a way to add the files as items in the project itself. this is an example of the .nuspec file.
<?xml version="1.0"?>
<package >
<metadata>
...
<tags>some tags</tags>
<contentFiles>
<files include="**/*.*" buildAction="None" copyToOutput="true" flatten="true" />
</contentFiles>
</metadata>
<files>
<file src="contentFiles\any\any\config\name.example" target="content\name.example" />
<file src="contentFiles\any\any\config\name.xsd" target="content\name.xsd"/>
</files>
</package>
The MS docs mention it is useful to define both files and contentFiles. I think the requirement is that the nuget package has a folder content, in which files are transferred to the project directory

nuget pack ignores package.config dependencies

I cannot get nuget pack X.csproj to recognize package dependencies in a project. Amazingly, when packaging, the diagnostic message “Found packages.config. Using packages listed as dependencies” is printed, but in the end the <dependencies/> tag in the .nuspec file inside the package is empty.
The packages.config for the project does indeed contain references:
<packages>
<package id="SmartAction.Logger" version="1.0.2.0" targetFramework="net40" />
<package id="SmartAction.Pervasive" version="1.0.1.0" targetFramework="net40" />
</packages>
To narrow the problem down, I removed my own parallel .nuspec file, and mostly all switches from the nuget pack command:
> nuget pack libToneDetection.csproj -prop Configuration=Release
MSBuild auto-detection: using msbuild version '14.0' from 'C:\Program Files (x86)\MSBuild\14.0\bin'.
Attempting to build package from 'libToneDetection.csproj'.
Packing files from '[snip]\Core\ToneDetection\libToneDetection\bin\Release'.
Found packages.config. Using packages listed as dependencies
Successfully created package '[snip]\Core\ToneDetection\libToneDetection\SmartAction.Audio.ToneDetection.1.0.0.0.nupkg'.
NuGet Version: 3.3.0.212
The only difference I can spot with this project is that its name is different from package name (I am trying to maintain them in sync but this is older stuff I am repackaging).
I doubt I had ever seen this before. I am finding questions on SO from people trying to prevent references in packages.config from becoming dependencies of the package, but none from those trying, like me, to beat the reverse problem. Help!
Addendum. I copied the project out of the solution with other projects to a temporary directory and rebuilt the package from there. Now one of the two dependencies from packages.config was added to the package:
<dependencies>
<dependency id="SmartAction.Logger" version="1.0.2.0" />
</dependencies>
Thinking of the differences between the two, the SmartAction.Logger package depends on SmartAction.Pervasive. But the package I am compiling really uses both.
To me, either behavior looks incorrect. Am I hitting a nuget bug, or a cryptic complex feature?
Xref: Opened https://github.com/NuGet/Home/issues/1867

An item with the same key has already been added nuget error

I am currently creating a nuget package with a nuspec file but getting the following error:
An item with the same key has already been added.
My command I am using is:
nuget pack "MyProject.csproj" -o "..\Packages"
This is my nuspec file:
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<language>$language$</language>
</metadata>
<files>
<file src="bin\MyLibrary*.dll" target="lib\net45" />
</files>
</package>
The nuspec file I am using is also used when packaging other packages withing the same library. Could this be the reason why the above error is occurring? Any ideas?
It might be that you are adding files in nuspec that are also getting added when you call pack on the .csproj (files/dlls referenced by the csproj). If so you can remove the file references from the nuspec file and give it a try.
how does your nuspec file look like?
I created it with the following steps
1. create the Mylibrary project
2. let me add a dependency, I installed ninject package to the project
3. build
4. nuget spec mylibrary.csproj
5. nuspec file generated, I didn't add any file or dependency manually to the file
6. nuget pack mylibrary.csproj
7. nuget pack would automatically add ninject as a dependency and also add mylibrary.dll into the correct folder
8. http://npe.codeplex.com/ is a nice tool to open the nupkg file and see what got generated inside the package.
I had this error trying to use the package visualizer and it ended up that my packages.config had the same package name in it more than once with different versions.