I recently had a requirement to completely package up a .NET application so that it was extremely portable beyond it’s initial installation. By bundling all the dependencies into the main executable package you can move the application around with minimal fuss once installed. The goal was to keep the application as independent as possible.
The application is actually fairly simple with only a handful of required library files to package. Once all the dependencies had been packaged the entire application executable is still under 600k. This process detailed below also enables the package to contain non .NET assemblies, like C++ components and other files. What’s really good is by default the dependencies are never extracted onto the file system which is ideal.
The NuGet Package Solution
make sure that you have your start up project selected in the package manager console and execute this command:
You don’t even have to configure anything. Just build …
The only thing that might need doing in order to make sure your package only contains exactly what’s needed you can add a new set of targets to your *.csproj file.
When you install the package it will automatically add a new import:
<Import Project="packages\Fody.1.26.1\build\Fody.targets" Condition="Exists('packages\Fody.1.26.1\build\Fody.targets')" />
Then to make sure you get the package your really want you can add:
<Target AfterTargets="AfterBuild;NonWinFodyTarget" Name="CleanReferenceCopyLocalPaths"> <Delete Files="@(ReferenceCopyLocalPaths->'$(OutDir)%(DestinationSubDirectory)%(Filename)%(Extension)')" /> </Target>
Done. Very nice and a must know trick. The only thing I want to now tackle is bundling in the base configuration file.