Search code examples
c#mefnuget-package

How to determine behavior of binaries in NuGet package


There is this big solution I'm working on, where I turned a lot of the projects into NuGet packages. The packages were created via a .nuproj file in a separate solution in VS.

Everything works fine, except for the following: At bootstrap I load some catalogs for MEF to be able to import them, which worked perfectly when I worked with the original projects, but now the needed DLLs (which come from the a package) don't make it to the bin\Debug\Modules folder.

Is there a way to make NuGet copy its content to the Modules folder? (and not to the root path)

I tried using the different kinds of sub-folders inside the package with no success.


Solution

  • I found that the best solution for this matter is the following:

    Take the files that need to be loaded and put them on the content folder. This can be done simply:

    <ItemGroup>
        <Content Include=" {here go the needed files} " />
    </ItemGroup>
    

    The content folder just holds the files, but it does not copy them to the output folder on the client project. In order to copy them to the desired output, a .targets file can be used, just like the following:

    <?xml version="1.0" encoding="utf-8" ?>
    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <Target Name="CopyToOutput" AfterTargets="Build">
        <ItemGroup>
          <FilesToCopy Include="$(MSBuildThisFileDirectory)..\content\**\*.*"/>    
        </ItemGroup>
        <Copy 
          SourceFiles="@(FilesToCopy)" 
          DestinationFiles="@(FilesToCopy->'$(OutDir)/%(RecursiveDir)%(FileName)%(Extension)')"/>
      </Target>
    </Project>
    

    Keep in mind that the targets file name and the ID of the NuGet have to be equal for the targets file to be added to the project.