How to pack an x86 / x64 specific C++/CLI DLL with Nuget? - nuget

I have a C++/CLI DLL that interfaces a third party native DLL. I want to pack this as Nuget.
I followed this official MS guide, this blog post and read through this other question.
So here is what I did:
First, I created the proper nuspec file:
<?xml version="1.0"?>
<package >
<metadata>
<id>CPlusPlusNativeIFace</id>
<version>1.0.4</version>
<title>CPlusPlusNativeIFace</title>
<authors>Jens Rabe</authors>
<owners>Who owns that</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A DLL that makes a native third-party DLL available to .net</description>
<releaseNotes>foo</releaseNotes>
<copyright>errm...</copyright>
<tags>foo bar</tags>
</metadata>
<files>
<file src="..\Release\**" target="runtimes/win-x86/lib/net461" />
<file src="..\x64\Release\**" target="runtimes/win-x64/lib/net461" />
<file src="DummyAny\**" target="ref\net461" />
</files>
</package>
Then I compiled the DLL for Release x86 and Release X64. Then I created the DummyAny folder, copied the contents of the x86 one in, and used the corflags utility like in Links 2 and 3 to strip the 32 bit flag.
When I now nuget pack and nuget add, and try to reference that package in another project, I always get:
Could not install package 'CPlusPlusNativeIFace 1.0.4'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6.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.
I double checked that the files are right:
I have the x86 stuff in runtimes/win-x86/lib/net461
I have the x64 stuff in runtimes/win-x64/lib/net461
I have a ref/net461 folder with the manipulated dll in
But I still can't load the package.
I also tried putting the CPU specific DLLs into runtimes/win-x86/native and runtimes/win-x64/native to no avail.
What else am I missing? Does that not work for C++/CLI projects built against .net Framework?

Turned out that the official MS documentation was not usable here.
This question was/is listed as "related" here and the accepted answer there made it work for me. I used the example project on Github as a reference.
So here is how I got it to work:
Opened the project properties, went to Configuration Properties / General, selected All Configurations and All Platforms, and as Output Directory, I specified: $(ProjectDir)bin\$(Platform)\$(Configuration)\
Rebuilt the project for x86 and x64 in Release mode
Adapted the Nuspec to be like this:
<?xml version="1.0"?>
<package >
<metadata>
<id>CPlusPlusNativeIFace</id>
<version>1.0.5</version>
<title>Third Party Proxy</title>
<authors>Jens Rabe</authors>
<owners>foo</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>bar</description>
<releaseNotes>Further experimenting with nuget</releaseNotes>
<copyright>baz moo</copyright>
<tags>quux bletch</tags>
</metadata>
<files>
<file src="bin\Win32\Release\**" target="build\x86" />
<file src="bin\x64\Release\**" target="build\x64" />
<file src="bin\Win32\Release\**" target="lib\net461" />
<file src="CPlusPlusNativeIFace.props" target="build\net461" />
</files>
</package>
Added a CPlusPlusNativeIFace.props which contains the following:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Reference Include="CPlusPlusNativeIFace" Condition="'$(Platform)' == 'x86'">
<HintPath>$(MSBuildThisFileDirectory)..\x86\CPlusPlusNativeIFace.dll</HintPath>
</Reference>
<Reference Include="CPlusPlusNativeIFace" Condition="'$(Platform)' == 'x64'">
<HintPath>$(MSBuildThisFileDirectory)..\x64\CPlusPlusNativeIFace.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
Now I can do nuget pack CPlusPlusNativeIFace.nuspec and I get a Nuget package that installs correctly.

Related

Excluding the library being built from nuget package in .NET Standard

Bear with me - this is an unusual scenario.
I have 4 projects in my solution. The top most project references the 3 other projects. None of the 3 other projects reference each other. So the architecture is like this:
Now, when I build project A I want it to produce a nuget package containing projects B, C and D but not project A. As this is in .NET standard I can configure the packages tab of project A to produce a nuget package automatically when it builds by checking the 'Generate NuGet package on build option.' Then, I can get it to include B, C and D by making the following changes to A's csproj file:
<ItemGroup>
<ProjectReference Include="..\B.csproj">
<PrivateAssets>all</PrivateAssets>
</ProjectReference>
<ProjectReference Include="..\C.csproj">
<PrivateAssets>all</PrivateAssets>
</ProjectReference>
<ProjectReference Include="..\D.csproj">
<PrivateAssets>all</PrivateAssets>
</ProjectReference>
</ItemGroup>
<PropertyGroup>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
<Version>1.0.0-beta</Version>
<PackageId>A</PackageId>
<Company></Company>
<Product>A</Product>
<Description></Description>
<Authors></Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
<ItemGroup>
<BuildOutputInPackage Include="#(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->WithMetadataValue('PrivateAssets', 'all'))" />
</ItemGroup>
</Target>
Ideally I would like to add a line to remove A.dll from the nuget package. Is this possible? A is a wrapper project which consuming code will never need to use. It is not possible for B, C and D to reference each other.
UPDATE
This is how I solved it (thanks #tom redfern)
I created a nuspec file manually:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>A</id>
<version>1.0.0-beta</version>
<authors>Foo</authors>
<owners>Bar</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A package</description>
<dependencies>
<group targetFramework=".NETStandard2.0">
</group>
</dependencies>
</metadata>
<files>
<file src="bin\Release\netstandard2.0\B.dll" target="lib\netstandard2.0\B.dll" />
<file src="bin\Release\netstandard2.0\C.dll" target="lib\netstandard2.0\C.dll" />
<file src="bin\Release\netstandard2.0\D.dll" target="lib\netstandard2.0\D.dll" />
</files>
</package>
Then in my .csproj file for A I put the following to automatically pack it after a build:
<Target Name="__PackNuGetPackage" AfterTargets="Build">
<Exec Command="$(NugetPackage)nuget.exe pack "A.nuspec"" />
</Target>
Using patented(1) elite(2) debugging skills, we can figure out if it's possible without manually creating and maintaining a nuspec file.
First, let's start with NuGet's docs on creating a package with the dotnet CLI. It says "msbuild -t:pack is functionality equivalent to dotnet pack". So, first hint, it's just running MSBuild targets.
So, run dotnet msbuild my.csproj -pp:pp.txt. This "pre-processes" (evaluates all MSBuild import statements and writes the result into a single file) the csproj (just a standard MSBuild file). We then search for the pack target, and scroll up until we find the filename of the file that was imported. We see it's NuGet.Build.Tasks.Pack.targets, and since NuGet is open source on GitHub, I can point you to the source.
Searching NuGet.Build.Tasks.Pack.targets for Condition, to see what extensibility options the NuGet team has provided, I see <IncludeBuildOutput Condition="'$(IncludeBuildOutput)'==''">true</IncludeBuildOutput>. So, settings <IncludeBuildOutput Condition="'$(IncludeBuildOutput)'==''">false</IncludeBuildOutput> in your csproj, might work.
(1) not patented
(2) standard, but since people don't modify MSBuild files anywhere near as often as C#, the skills and tools aren't as well known
You can achieve this by using a nuspec file. Use nuspec when you need absolute control over the nuget pack process. A simple nuspec file:
<package >
<metadata>
<id>MyPackage</id>
<version>1.0</version>
<authors>Something</authors>
<owners>Something</owners>
<description>Somthing</description>
<copyright></copyright>
<dependencies>
<!-- any nuget package dependencies -->
<dependency id="AnotherPackage" version="2019.2.4.1" />
</dependencies>
</metadata>
<files>
<!-- this is where you can have complete control over which assemblies get added to your package. You can add them individually pr using wildcards. -->
<file src="..\obj\**\*.dll" target="lib" />
</files>
</package>
When you have created your .nuspec file, add it into your solution, and then make your "Nuget Pack" build step read the nuspec file rather than the project file.

Why is nuget not adding my file?

I have this nuspec file:
<?xml version="1.0"?>
<package >
<metadata>
<id>My.Package</id>
<version>1.0.1.7</version>
<title>My Package</title>
<authors>My name</authors>
<owners>Mu author</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>My description</description>
<releaseNotes>Release note</releaseNotes>
<copyright>Copyright 2017</copyright>
<tags>tag1 tag2</tags>
<contentFiles>
<files include="myFile.config" copyToOutput="true" />
</contentFiles>
</metadata>
<files>
<file src="myFile.config" target=""/>
</files>
</package>
When I pack this nuspec file, my nupkg file is created and in the NuGet Package Explorer I see that myFile.config is included in my package. All well so far.
But, then, when I install this package, the dll is added to references, but myFile.config is not added to the solution.
Things I've tried without success:
- View the folder on Disk to see if myFile.config is created (not there)
- <file src="myFile.config" target="."/>
- <file src="myFile.config" target=".\"/>
I also want the file to set Copy to Output Directory: Copy always.
What am I missing here?
PS: I am working on a private nuget server.
The target needs to be content since files of the nugpkg's content subdirectory are copied to the consuming project.
Note that this only works for projects using the packages.config style of referencing NuGet packages. For PackageReference (available in VS 2017, default for .NET Core / ASP.NET Core / .NET Standard projects), there is a new contentFiles feature that includes files logically in the build process.
See NuGet's documentation on including content files for more information.

Options for placing and updating PowerShell files in Project folder using NuGet

I am building a NuGet package that only contains a set of PowerShell scripts. The desired behavior is for the scripts to be placed in the project and/or solution folder, removed when the package is uninstalled, and updated when the package is updated. These scripts just need to live in the folder, and be copied to output folder (they are deploy scripts).
I've done this before using content target in a nuspec, but this does not work in netstandard/.NET Core applications (i.e., anything that uses PackageReference). The NuGet documentation mentioned the contentFiles element under the metadata element, but that also does not work with PackageReference. The only thing that I have been able to get working at all is copying the PowerShell scripts in tools/init.ps1. I have something partially working, but it doesn't handle the uninstall or upgrade path. And DTE is never fun.
Is there a way to use content files in netstandard?
If not, does there exist a sample or example of how to properly manage the NuGet lifecycle (copy file, update file, delete file)?
EDIT: This is for a console application, but it also should work in an asp.net application.
EDIT2: This is the nuspec that I have tried to do things via the contentFiles element. When I install the package, I don't see anything in project or solution.
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Test</id>
<version>1.0.0</version>
<contentFiles>
<files include="Build.ps1" buildAction="Content" copyToOutput="true" />
</contentFiles>
</metadata>
<files>
<file src="Build.ps1" target="content" />
</files>
</package>
Thanks,
Erick
As you have noticed, NuGet packages referenced via PackageReference no longer modify the project when installing. This is also the case for .NET Framework projects using this new feature (it was made public for non-core projects in VS 2017 15.2).
The content/someScript.ps1 file is still necessary for compatibility with "classic" packages.config based project, but a new feature for PackageReference projects is contentFiles.
When packing manually using a nuspec file, copying a file on build can be done by adding a contentFiles section:
<package>
<metadata>
...
<contentFiles>
<files include="**/*.ps1" buildAction="Content" copyToOutput="true" />
</contentFiles>
</metadata>
<files>
<file src="Build.ps1" target="content" />
<file src="Build.ps1" target="contentFiles/any/any" />
</files>
</package>
See the documentation on contentFiles for more details my example on GitHub.

Is there a Lightspeed 5.x NuGet package that can be used with ASP.Net 5?

I did a search on NuGet and couldn't find a Lightspeed NuGet package.
I have pro license for Lightspeed 5 so I have all the binaries I need to use Lightspeed in a non-ASP.Net 5 code.
However, it is my understanding that ASP.Net 5 doesn't allow you to allow you to reference DLLs directly, you have to create a NuGet package first.
So, I created a NuGet package of my POCO objects, DB context and Lightspeed references and added it to a .Net Framework 4.5 console application; it added the appropriate references (NuGet spec I used is show below).
<?xml version="1.0"?>
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$id$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<copyright>Copyright 2015</copyright>
<references>
<reference file="$id$.dll" />
<reference file="Mindscape.LightSpeed.dll" />
<reference file="Mindscape.LightSpeed.Linq.dll" />
</references>
</metadata>
<files>
<file src="..\Mindscape.Lightspeed\Mindscape.LightSpeed.dll" target="lib\net45\Mindscape.LightSpeed.dll" />
<file src="..\Mindscape.Lightspeed\Mindscape.LightSpeed.Linq.dll" target="lib\net45\Mindscape.LightSpeed.Linq.dll" />
</files>
</package>
However, this didn't work as expect when I tried to add this package to my to my ASP.Net 5 website (package added but using statement causes a compiler error).
Does anyone know a work-around to get Lightspeed working with ASP.Net 5?
Ideally Mindscape would publish a NuGet package.
Turns out the have their own NuGet feed that is listed in the "Keys" section of your subscription

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>