ILRepack – E Pluribus Unum: One assembly from many
Sometimes it is just much easier to deploy a single assembly that includes all dependencies. ILRepack and ILRepack.MSBuild.Task will do just that for you. Since I like to have Visual Studio and my build environment do this for me, I went with ILRepack.MSBuild.Task.
In order to showcase packing an assembly into my program, first I need a program that has a dependency. I decided to go with a simple console app that has a dependency on the Humanizer NuGet package to tell you how long you have to wait until your birthday.
>Repack.exe Usage: repack [date] Prints how long it is until your birthday. If you don't supply your birthday, it uses mine. 23 weeks until your birthday
I created a new console project in Visual Studio and named it Repack. I then included the Humanizer DLL using the NuGet package manager.
You can find the source code on github.
All you need to do is add the following snippet at the end of your .csproj file. To do this, you can open up the .csproj file in notepad or your favorite text [editor])(http://www.vim.org).
<Target Name="AfterBuild" Condition="'$(Configuration)' == 'Release'"> <ItemGroup> <InputAssemblies Include="$(OutputPath)\$(AssemblyName).exe" /> <InputAssemblies Include="$(OutputPath)\*.dll" /> </ItemGroup> <ILRepack Parallel="true" Internalize="true" InputAssemblies="@(InputAssemblies)" TargetKind="EXE" OutputFile="$(OutputPath)\$(AssemblyName).exe" /> </Target>
Because we name the target
AfterBuild, this code will automatically be run after msbuild or Visual Studio builds our project. Setting the condition ensures that this will only run when we are in release mode. You can definitely run this on debug builds, but it’s less likely that you’d want to.
The ItemGroup specifies lets us create a list of assemblies to include in the package. The first assembly should be your assembly. In my example it will be my executable file
Repack.exe. Next, I include all the DLLs in the output directory. This way, if I add a new dependency later, it will be included automatically.
Note that the order does matter. You will want to put the .exe first in this list.
Next all we need to do is call ILRepack. You can specify the output file to be anywhere you like, but in this example I overwrite the existing Repack.exe with the packed version.
Once you rebuild your project (in release mode), you can copy the EXE anywhere you want and it will run.
ILRepack.MSBuild.Task let’s you package DLL’s into your EXE file so you can copy just the executable anywhere and not have to worry about deploying the dependencies as well.
Full sample code can be found on github.
this post was originally on the MasterDevs Blog