Add custom DLL files in nuget package in new csproj - nuget

.nuspec file has section <files>, what are the alternatives for include localization resources for new csproj file? How to add custom DLL files?

The first part of the trick is to get your third party DLL added to the nupkg. This does it:
<ItemGroup>
<Reference Include="ThirdParty">
<HintPath>..\DLLs\ThirdParty.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Content Include="$(OutputPath)\ThirdParty.dll">
<Pack>true</Pack>
<PackagePath>lib\$(TargetFramework)</PackagePath>
</Content>
</ItemGroup>
If you install this package into an old-style csproj then a reference to ThirdParty.dll will be added.
However, if you install this package into a new-style csproj then a reference to ThirdParty.dll will not be added as a referece, irritatingly. Work in progress...

Related

Include my Nuget README in my Nuget Package

I maintain a few tiny Nuget Packages.
I have a Nuget README file in the git repository, and the .nupkg file is auto-built by VS, based on the "Package" config, stored in the .csproj file.
Whenever I need to release a new versio of the package, I upload the .nupkg file to nuget, in the web UI, and then it asks me for any documentation, at which point I have to manually upload the README file.
Is there any way to put that README file in the .nupkg so that I don't have to manually upload it every time?
NuGet have now added support to make this JustWork.
Announcement on the Issue thread: https://github.com/NuGet/Home/issues/6873#issuecomment-833829727
Announcement Blog post: https://devblogs.microsoft.com/nuget/add-a-readme-to-your-nuget-package/
All you need to do is reference the README.md file in the .csproj, using the PackageReadmeFile tag. If the README.md is in the git root (i.e. where GitHub auto-detects it) then it will look like this:
<PackageReadmeFile>README.md</PackageReadmeFile>
Currently there is not a way to do this, but it is WIP.
The new spec under review: https://github.com/NuGet/Home/wiki/Embedding-and-displaying-NuGet-READMEs
This issue for this feature: https://github.com/NuGet/Home/issues/6873
i do this via the csprj. file.
I created a .netstandard project and added a readme.txt.
add following to the x.csproj
<ItemGroup>
<None Include="readme.txt" pack="true" PackagePath="." />
</ItemGroup>
When installing the nuget the first time the file will be displayed in VS.
There is somewhere in the internet a artikel describing the solution, but i cannot find it now.
Here my complete csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net471;net35</TargetFrameworks>
.....
<FileVersion>1.0.1.11</FileVersion>
<AssemblyVersion>1.0.1.11</AssemblyVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NLog" Version="4.7.6" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="readme.txt" pack="true" PackagePath="." />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project>

Net Standard Library - Add files to nuget content folder on 'dotnet pack'

Is there an automated way to include a config transform file in the nuget package that is created when using the dotnet pack command?
I was able to do this by adding the xdt transform files in the class library project and then editing the csproj file to have this ItemGroup.
<ItemGroup>
<Content Include="app.config.uninstall.xdt" />
<Content Include="app.config.install.xdt" />
</ItemGroup>
When doing a dotnet pack this automatically adds them to the content folder of the nuget package.

Add install.ps1 on Class Library (.NET Standard) Project

I have migrate my .NET Framework project to a .NET Standard project.
In the .NET Framework project i have a .nuspec file with additional file config and create the nuget package with "NuGet.exe pack"
<files>
<file src="Install.ps1" target="tools\Install.ps1" />
</files
In the .NET Standard project i have not longer a nuspec file and switch to "msbuild -t:Pack" to create the nuget package. I have try to set the install.ps1 to (BuildAction = Content) but then i see a warning in the log "Issue: PowerShell file out side tools folder." And in the nupkg file the directory is "content\tools\Install.ps1" i need "tools\Install.ps1".
To get file into a different path in the package you can use the <PackagePath> element in the <Content> element like this:
<ItemGroup>
<Content Include="install.ps1">
<PackagePath>tools\</PackagePath>
</Content>
</ItemGroup>
(providing the install.ps1 is in the root of your project, otherwise you'll have to adjust the Include attribute value)
For more information check out the docs about the pack MsBuild Target here:
https://github.com/NuGet/Home/wiki/Adding-nuget-pack-as-a-msbuild-target
I have try to set the install.ps1 to (BuildAction = Content) but then i see a warning in the log "Issue: PowerShell file out side tools folder." And in the nupkg file the directory is "content\tools\Install.ps1" i need "tools\Install.ps1"
When you use msbuild -t:Pack to create the nuget package, msbuild/VS expects the files to be in content folder. But if you still want to the install.ps1 file in the tools directory, you can still use the .nuspec file and nuget.exe to pack the package.
The detail steps to pack package:
Create the .nuspec as below settings:
<?xml version="1.0"?>
<package >
<metadata>
<id>TestInstall.ps1</id>
<version>1.0.0</version>
<authors>Test</authors>
<owners>Test</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package description</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>Copyright 2017</copyright>
<tags>Tag1 Tag2</tags>
</metadata>
<files>
<file src="Install.ps1" target="tools" />
<file src="bin\Debug\netstandard1.4\TestInstall.ps1.dll" target="lib\netstandard1.4" />
</files>
</package>
Then use the command line: nuget.exe pack xxx.nuspec to pack the package, you will get the package with Install.ps1 in the tools directory:

NuGet package adds incorrect hint path

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.

How to use a native NuGet package from a managed project?

I have a managed project that uses a C-style native DLL through P/Invoke.
What is the correct way to package the native DLL so it can be added as a NuGet package to the managed project, and have the DLL be copied automatically to the output folder?
I have currently created a package using CoApp for the native DLL but i can't use it from the managed project; I get the following error when trying to add the package:
Could not install package 'foo.redist 1.0.0'. You are trying to
install this package into a project that targets
'.NETFramework,Version=v4.5.1', 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.
Currently i only have these "pivots" in the autopkg file:
[Win32,dynamic,release] {
bin: release\foo.dll;
}
[Win32,dynamic,debug] {
bin: debug\foo.dll;
}
... do i need to add something else?
I'm in a similar situation. I opted not to use CoApp for this project, but to create a fresh nuspec/.targets file combination instead.
Inside the nuspec file I use a <files> element to list my native dlls.
In the .targets file you have access to the msbuild Condition attribute, which allows basic Configuration pivoting. In our case we always deploy 64 bit binaries, so the Platform pivot is not needed, but you could also add it if needed.
I get warnings when running nuget pack since the binaries are not inside lib, but it works fine otherwise.
Steps:
run nuget spec in the folder that contains your vcxproj
create a .build folder, in that folder create an empty mydll.targets file (match the nuspec filename)
manually populate the files similarly to the examples below;
Example mydll.nuspec:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
...your metadata here
</metadata>
<files>
<file src="x64\Release\my.dll" target="x64\Release\my.dll" />
<file src="x64\Debug\my.dll" target="x64\Debug\my.dll" />
</files>
</package>
Example mydll.targets:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)\..\x64\Release\my.dll" Condition="'$(Configuration)'=='Release'">
<Link>my.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(MSBuildThisFileDirectory)\..\x64\Debug\my.dll" Condition="'$(Configuration)'=='Debug'">
<Link>my.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>