1

I have a WCF project with a service library and console app for self-hosting. The service app requires managed and unmanaged libraries, which I include using NuGet based on this SO answer, Where to place dlls for unmanaged libraries?

This works fine when testing the library independently--all libraries are copied correctly to the service library output folder. However, when the console app is built, the unmanaged dll does not get copied into the console app's output folder and the app fails at runtime.

Question: How can I get the unmanaged dll to be lifted up/copied into any app that hosts the service library? I know I can do it by hand but would like an automated solution since I will likely add IIS hosting at least and don't want to take risk of forgetting to handcopy every time I build.

Here is my .nuspec file:

<?xml version="1.0"?>
<package>
  <metadata>
    <id>XyzLibrary</id>     
    <version>1.0.0</version>    
    <authors>Authors</authors>  
    <description>Description</description>  
    <releaseNotes>Notes</releaseNotes>  
  </metadata> 
    <files>
      <file src="XyzLibraryNet.dll" target="lib\net461\"/>
      <file src="XyzLibrary.dll" target="unmanaged"/>
      <file src="build\XyzLibrary.targets" target="build" />
    </files>    
</package>

And here is the .targets file:

<?xml version="1.0" encoding="utf-8"?> 
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
 <Target Name="CopyMyPackageFiles" AfterTargets="AfterBuild"> 
    <ItemGroup> 
        <MyPackageFiles Include="$(MSBuildProjectDirectory)..\unmanaged\*.*"/> 
    </ItemGroup> 
    <Copy SourceFiles="@(MyPackageFiles)" DestinationFolder="$(OutputPath)">
    </Copy> 
 </Target> 
</Project>
JJJulien
  • 269
  • 2
  • 13

1 Answers1

0

It works for the service library because the .targets file is being imported in to the project file and the Copy command is being executed. If you need them in the console app you can:

  1. Add a reference to the nuget package in the console app
  2. Add a post build step that copies the files from the service library to the console app
  3. Add a custom MSBuild step in the Console App project file to perform the copy.

Of the three options, the easiest to implement would be to add a post build step. This can be done by opening the project properties, selecting the build events tab and adding something like:

copy ..\ServiceLibrary\bin\$(Configuration)\*.dll $(OutputPath)
mageos
  • 1,216
  • 7
  • 15
  • I was afraid there was no easy answer. I at first used #1 myself before positing but it just feels wrong to include a package that is only needed indirectly for another project; plus its not clear to anyone who comes after why that package is there. I will try #3, since maybe it as a little more explicit. – JJJulien May 12 '18 at 03:37