Kae Travis

Use Visual Studio to Publish an Unreferenced Assembly to the Bin Folder

This blog explains how to use Visual Studio to publish an unreferenced assembly to the Bin Folder.

There are lots of examples on Google that provide explanations on how to copy unreferenced assemblies (DLL files) to the bin folder at build time. For example, I could right-click my project and navigate to ‘Build Events’ and then paste this into the ‘Post-build event command line’:

xcopy /y "$(ProjectDir)Spelling\Hunspellx64.dll" "$(TargetDir)"
xcopy /y "$(ProjectDir)Spelling\Hunspellx86.dll" "$(TargetDir)"

And of course when i debugged my web application the DLLs were copied to the Bin folder after a build, and it worked great! However, when i published my web application to the production web server, it did NOT publish these DLLs to the bin folder on the web server! Meaning that in production, my web application threw an error!

After searching and searching, I stumbled upon this solution that worked at both build time AND publish time.

Paste this just before the </Project> tag at the end of your <project>.csproj file. I am copying two DLL’s in this example:

Spelling\Hunspellx64.dll
Spelling\Hunspellx86.dll

 <PropertyGroup>
<UnreferencedDlls>Spelling\Hunspellx64.dll;Spelling\Hunspellx86.dll</UnreferencedDlls>
</PropertyGroup>
<PropertyGroup>
<CopyAllFilesToSingleFolderForPackageDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForPackageDependsOn>
</PropertyGroup>
<Target Name="AfterBuild">
<Message Text="Copying unreferenced DLLs to bin" Importance="High" />
<CreateItem Include="$(UnreferencedDlls)">
<Output TaskParameter="Include" ItemName="_UnReferencedDLLs" />
</CreateItem>
<Copy SourceFiles="@(_UnReferencedDLLs)" DestinationFolder="bin\%(RecursiveDir)" SkipUnchangedFiles="true" />
</Target>
<Target Name="CustomCollectFiles">
<Message Text="Publishing unreferenced DLLs" Importance="High" />
<ItemGroup>
<_CustomFiles Include="$(UnreferencedDlls)" />
<FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
<DestinationRelativePath>bin\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>

The <Message> tags will also print out a message to the debug output so you can see the actions being executed.

Use Visual Studio to Publish an Unreferenced Assembly to the Bin Folder
Use Visual Studio to Publish an Unreferenced Assembly to the Bin Folder

Leave a Reply