Force external files to be copied to bin folder on publish
Asked Answered
C

3

11

I have a web application project that references a third party assembly, which in turn uses some native dlls that it needs to find at runtime. Because of that, I wanted to add these dlls to the bin folder of the project, so that they can definitely be found.

However, if I add the files to the project in /bin/, select "copy to output", the files are compiled and published into /bin/bin. Adding them to the root folder works, but can hardly be the right solution.

Adding them to the build target "AfterBuild" has no effect when publishing (e.g. "build deployment package")

It also has to work when building the solution via TFS, where the MSBuild target _CopyWebApplicationLegacy is invoked.

Condensate answered 24/9, 2012 at 10:1 Comment(0)
C
10

The solution was a combination of the things I had tried already:

  • Include the "Bin" folder in the project
  • Add the needed files (I added them as a link due to our development structure)
  • Set the following properties: "Build Action = Content" and "Copy to Output = Do Not Copy"

The files are now copied to the bin folder when publishing, even when automating the builds on TFS.

The component that needed this was GdPicture, which uses a couple of native DLLs during runtime.

Condensate answered 24/9, 2012 at 12:48 Comment(4)
Cleanest, simplest solution.Depositary
Including the Bin folder under source control would cause unnecessary check-insAllerie
The bin folder should never be part of the solution. It's just an output folder. If you use test cases, TFS etc. the output folder will be redirected. Setting the option to "do not copy" will lead to missing assemblies. Sorry to say: "Very bad idea"Excessive
You can include the bin folder without including all the files under it - which is what I think the answer suggests. By linking the files from their original locations, you also don't have to check those in. If you have a .gitignore rule to ignore files under bin, you're also going to be fine. For those who don't know how to "link" to a file, you right-click and choose Add existing item... then select the file and use the dropdown next to the Add button to "Add as link"Toil
C
4

It's common to have a lib folder in either your solution or workspace to put your third party dlls within. You would then add a reference to this location from your project and ensure this location is under source control.

You could also look at using NuGet to package this for you, which uses a solution level packages folder automatically.

Clinton answered 24/9, 2012 at 10:10 Comment(7)
how does this help me with the external assembly having to find these files?Condensate
@Alex if you have these files referenced in your project it will find them. Can you specify which libraries these are?Clinton
They are native dlls(c++ I presume), and therefore cannot be added as a normal reference. The referenced assembly, GdPicture, accesses them at runtime.Condensate
@Alex 1. try having the lib folder included in your project and have files set to CopyLocal in properties, that should do it. 2. What happens when you try to add them as a referenced dll? 3. Would adding them to the GAC on the server be an option?Clinton
CopyLocal can not be set for files, only "Copy to output directory". This, however, will result in the creation of /bin/lib during build. I can not reference the dlls as they are not .NET assemblies. GAC would work, but I want to avoid it since I don't need it for the other files either.Condensate
I don't know why I did not try this combination before, but including the bin folder and adding the dll files with "Build Action = Content" and "Copy to Output = Do Not Copy" did it! Thanks for leading me to this solution. Would it be right to mark your answer as correct or should I write a new one?Condensate
@Alex glad you got your answer. It probably be right for you to write your own answer with this detail and accept it yourself (I would include fact it is GDPicture). If I helped it would probably be etiquette to +1 my answer.Clinton
C
0

Based on this article http://sedodream.com/2010/05/01/WebDeploymentToolMSDeployBuildPackageIncludingExtraFilesOrExcludingSpecificFiles.aspx. It's possible to define the msbuild target into main (lastest in order of solution's build) project file that will capture all files of the bin folder. Like this

<Target Name="CustomCollectFiles">
    <ItemGroup>
      <_CustomFiles Include="$(OutDir)\**\*" />
      <FilesForPackagingFromProject  Include="%(_CustomFiles.Identity)">
        <DestinationRelativePath>$(OutDir)\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
      </FilesForPackagingFromProject>
    </ItemGroup>
  </Target>
  <PropertyGroup>
    <CopyAllFilesToSingleFolderForPackageDependsOn>
      CustomCollectFiles;
      $(CopyAllFilesToSingleFolderForPackageDependsOn);
    </CopyAllFilesToSingleFolderForPackageDependsOn>
  </PropertyGroup>

And place it before closing root project element.

Clingy answered 13/7, 2017 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.