'How to make a nuspec (.NETFramework) to install a legacy dll so that it is copied along with the assembly in the build folder

I try to create a nuspec file to distribute de .NET component usiing a legacy DLL. Here's an extract of the nuspec file.

My problem is I can't figure out how to specify the legacy dll so that it is copied to the "build" folder when compiling a project using my .NET component.

With nuget when deployiong an assembly that one is copied from the packages... folder to the build (debug or release) folder. I want the same for my legacy dll so that I can run the program using my assembly using the legacy dll. I could copy the file within the build but it doesn't make sense, nuget should be there for that.After having searched a loy I inserted a "references" item in the nuspec but when specifying the "file" entry for my legacy dll (AAA.dll) I don't know what to put in target. I've tried ref\any, ref\net47, lib, lib\any, lib\net47 (that one simply prevents the package to install becaus eit tries to reference AAA.dll in the project),...

<package>
  <metadata>
    <dependencies>
      <group targetFramework=".NETFramework4.7">
      </group>
    </dependencies>
    <references>
      <group targetFramework="net47">
        <reference file="AAA.dll" />
      </group>
    </references>
  </metadata>
  <files>
    <file src=".\Release\ASSEMBLY.dll" target="lib\net47"></file>
    <file src=".\Release\AAA.dll" target="lib"></file>
  </files>
</package>

What I want is: when compiling AAA.dll should be put in the build folder as ASSEMBLY.dll is, so that the program using the assembly dll (using my legacy dll) does not crash.

I could not find a working solution on Internet and I turn to you for that.

UPDATE I finally managed to do it but had to do it wit the following steps (even though not all may be necessary, that's the way I did it).

  1. Move my package to PackageReference format
  2. created a .targets file with the exact same name as my package (i.e. my.package.I.created.targets). That targets file is located in my project folder, next to the csproj file and its content is:
    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <ItemGroup>
        <NativeLibs Include="$(MSBuildThisFileDirectory)**\mydll.dll" />
        <None Include="@(NativeLibs)">
          <Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
      </ItemGroup>
    </Project>
  1. Inside the csproj file I added the following lines:
    <ItemGroup>
      <None Include="$(SolutionDir)$(Configuration)/mydll.dll" Pack="true" PackagePath="build\">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </None>
      <None Include="$(SolutionDir)/my.package.I.created.targets" Pack="true" PackagePath="build\">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </None>
    </ItemGroup>

The result is:

  • the legacy dll is inserted into the nupkg
  • the legacy dll is copied into the target folder along with the assembly when that one is referenced


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source