0

I have a Web API project that depends on a NuGet package. This package contains an XML file that is referenced by my application at runtime.

When I build the solution and run it in debug mode from Visual studio the file gets copied to \bin, and the application runs without problem.

When I publish the application the file doesn't get copied to the final output, and I can see that it's never been copied to the \obj folder.

I've though of adding the file reference directly to the package in the \packages folder, but this will break whenever the package version is updated.

How can I specify that the file should be copied when deploying?

3
  • You can add the file to your project with Resource for the property Build action and Always copy for Copy in output directory Commented May 15, 2017 at 6:53
  • The point was that the file would disappear once the package is updated since the NuGet packages in the \packages folder are named according to their version. Commented May 18, 2017 at 6:09
  • You can use wildcards for the version Commented May 18, 2017 at 6:36

2 Answers 2

3

I figured it out based on this blogpost.

I added the following to the end of the .csproj file:

<PropertyGroup>
    <CopyAllFilesToSingleFolderForPackageDependsOn>
            CustomCollectFiles;
            $(CopyAllFilesToSingleFolderForPackageDependsOn);
    </CopyAllFilesToSingleFolderForPackageDependsOn>
</PropertyGroup>


<Target Name="CustomCollectFiles">
    <Message Text="=== CustomCollectFiles ===" Importance="high" />
    <ItemGroup>
        <_CustomFiles Include="..\Packages\**\*PackageName.*.xml*" />
        <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
            <DestinationRelativePath>bin\%(Filename)%(Extension)</DestinationRelativePath>
        </FilesForPackagingFromProject>
    </ItemGroup>
</Target>
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for sharing your solution here, you could mark it as the answer, so it could help other community members who get the same issues.
I will, but I'm not allowed to accept my own answer until after a grace period. There's still 18 hours remaining as of this writing.
Due to reasons, MS changed "CopyAllFilesToSingleFolderForPackageDependsOn" to "CopyAllFilesToSingleFolderForMSDeployDependsOn" ... see: dotnetcatch.com/2016/07/20/… @PetterBrodin: Maybe you wish to update your answer. And thanks for helping me solving a similar problem.
@Aaginor, do you know which versions this affects?
My only source is the linked blog and that this helped me to fix the code not working. I use VS2019. I'd guess the change was introduced somewhere in 2016
0

Petter Brodins answer almost work for me. And since I am new to Stackoverflow commenting i can not add a comment. In order for it to work I changed the Include to "..\Packages*\lib*.xml". But this was a life saver since i wrote a lot of documentation and couldn't get it to my swagger implementation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.