NuGet package adds incorrect hint path - nuget

During an automated build, my nuget package needs to be non framework dependent, however I keep finding that the nuget package getting added is incorrectly adding a HintPath.
Within my nuspec I've defined the files that are part of the package:
<files>
<file src="lib\xyz.dll" target="lib\xyz.dll" />
<file src="lib\xyz.xml" target="lib\xyz.xml" />
</files>
However whenever I add the package to my project/solution, it incorrectly adds a hint path specifying:
<Reference Include="xyz, Version=11.0.0.0, Culture=neutral, PublicKeyToken=4a3c0a4c668b48b4">
<HintPath>..\packages\xyz.11.0.0.0\xyz.dll</HintPath>
<Private>True</Private>
</Reference>
This is causing the automated build server to not find the assembly and fail to build. I can manually fix the hint path, but would rather not.
I took a look at this post (Failed to add NuGet package) but I don't find it relevant. This post (NuGet package install uses specific assembly version in csproj files) seemed to be referring to the same problem but with no answer. Anybody have any thoughts?

You can work around this by using a custom MSBuild task.
Instead of adding the assembly to the lib directory create an MSBuild .targets file named after the package id and put your xyz assembly next to it.
\build
\Net45
\MyPackage.targets
\xyz.dll
\xyz.xml
Then in the MSBuild .targets file add the reference exactly how you want it to be. Something like:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Reference Include="xyz">
<HintPath>$(MSBuildThisFileDirectory)\xyz.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
The above shows how to specify a hint path relative to the MSBuild .targets file. You said that you do want to use a hint path so you could remove that if xyz.dll can be resolved by MSBuild somehow, such as it being in the GAC.

Related

Custom NuGet package not installing in wix project

I generate a NuGet that is is just a number of redist files that I want to use in one of my projects. If I install it in a C# or C++ projects, it works. But when I try to install it in a wixproj project and I get the following message:
Could not install package 'package-1.0.0'. You are trying to install this package into a project that targets 'Unsupported,Version=v0.0', 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.
I generate the package through a TeamCity task (using NuGet 5.6.0). When trying to generate the package with a NuGet CLI 5.8.1, I get the following warning:
*WARNING: NU5128: Some target frameworks declared in the dependencies group of the nuspec and the lib/ref folder do not have exact matches in the other location. Consult the list of actions below:
Add a dependency group for native0.0 to the nuspec*
Looked at https://learn.microsoft.com/en-us/nuget/reference/errors-and-warnings/nu5128, one of the solutions was trying a dependencies group targetFramework, (I used "native0.0") with no success. My nuspec is as follows:
<?xml version="1.0"?>
<package>
<metadata>
<id>package</id>
<version>1.0.0</version>
<authors>package</authors>
<owners>owner</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>my package</description>
<copyright>© 2021 company, Inc</copyright>
<tags>native</tags>
</metadata>
<files>
<file src="downloads\Folder\win32.vs2017\file1.lib" target="lib\native\lib\win32.vs2017\" />
<file src="downloads\Folder\win32.vs2017\file1-debug.lib" target="lib\native\lib\win32.vs2017\" />
<file src="downloads\Folder\Include\**" target="lib\native\include\" />
<file src="build\package.props" target="build\native" />
</files>
</package>
And my props file
<Project>
<PropertyGroup>
<MyVersion>1.0.0</MyVersion>
</PropertyGroup>
</Project>
I can install other NuGet packages into wixprojects, so how I configure mine to work? Thanks.
OK I found it, the issue lies at the line
<file src="build\package.props" target="build\native" />
changing target to "build\" allows the NuGet to be loaded to any project type, included WixProj. Note that the NU5128 warning still exists though, but not an issue for me.

How to properly define a NuGet package for an assembly where the dll file name is different from the assembly name?

I had to package a portable version of Newtonsoft.Json as an internal NuGet package under a different name (Newtonsoft.SL5.Json). The reason is that we are building into a shared bin directory and I do not want the Silverlight version to overwrite the full .NET version. (Yes, we still have Silverlight and yes the portable Json.Net version is not good enough for non Silverlight code we have)
The package file name is Newtonsoft.SL5.Json.8.0.1.19229.nupkg
Inside it there is Newtonsoft.SL5.Json.nuspec
Inside it there is lib\sl5\Newtonsoft.SL5.Json.dll
The nuspec file content is:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>Newtonsoft.SL5.Json</id>
<version>8.0.1.19229</version>
<authors>Newtonsoft</authors>
<owners>Newtonsoft</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Json.NET Portable .NET 4.0</description>
<copyright>Copyright © James Newton-King 2008</copyright>
</metadata>
</package>
Now something I am doing wrong, because as a result, when adding the dependency in Visual Studio, the reference it produces is:
<Reference Include="Newtonsoft.SL5.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.SL5.Json.8.0.1.19229\lib\sl5\Newtonsoft.SL5.Json.dll</HintPath>
<Private>True</Private>
</Reference>
And of course, there is no such assembly Newtonsoft.SL5.Json. It should be Newtonsoft.Json.
So, I am doing something wrong. How should I change the NuGet package so that it uses the correct assembly name, i.e. Newtonsoft.Json? Because even though I changed the file name, the assembly name is still the same.
while adding reference IDE takes .dll files that are included in the package (lib/{platform}/). If you rename those files added and packaged in nupkg file -> those should be added in element.

System.IO.FileNotFoundException for Microsoft.EntityFrameworkCore

I have a .NET Standard 2.0 project which uses EF Core via the Nuget package Microsoft.EntityFrameworkCore. Below is the relevant PropertyGroup from the *.csproj file:
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.4" />
</ItemGroup>
The solution builds, but at runtime, I get a System.IO.FileNotFoundException:
Could not load file or assembly 'Microsoft.EntityFrameworkCore, Version=2.1.4.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies.
That assembly is not in the build folder (or any subfolders such as the netstandard2.0 subfolder). It is in %userprofile%\.nuget\ ...
I have tried:
Cleaning / Rebuilding
Clearing the Nuget cache.
Adding
<RestoreProjectStyle>PackageReference</RestoreProjectStyle> to the
*.csproj file.
Removing and re-adding Micrososft.EntityFrameworkCore (and Microsoft.EntityFrameworkCore.Design, which is also used)
Is there some other way of forcing the package to be included in the build folder (like Local Copy)?
Editing the *.csproj files and adding the following to a PropertyGroup seems to have fixed the problem (or at least provided a reasonable workaround):
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

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

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.