11

We want to upgrade our solution with several projects to .NET 4.5. We already use Visual Studio 2012. We use ILMerge to merge the assemblies to a single EXE.

Our current .csproj file for the main project look like this:

<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release' ">
    <CreateItem Include="@(ReferenceCopyLocalPaths)" Condition="'%(Extension)'=='.dll'">
        <Output ItemName="AssembliesToMerge" TaskParameter="Include" />
    </CreateItem>
    <PropertyGroup>
        <ReferenceAssemblies>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0</ReferenceAssemblies>
    </PropertyGroup>
    <Message Importance="high" Text="Executing ILMerge...with target platform from $(ReferenceAssemblies)" />
    <Exec Command="&quot;$(SolutionDir)LIB\ILMerge.exe&quot; /out:@(MainAssembly) /internalize /targetplatform:v4,&quot;$(ReferenceAssemblies)&quot; &quot;@(IntermediateAssembly)&quot; @(AssembliesToMerge->'&quot;%(FullPath)&quot;', ' ')" />
    <Delete Files="@(ReferenceCopyLocalPaths->'$(OutDir)%(DestinationSubDirectory)%(Filename)%(Extension)')" />
</Target>

How should this look for .NET 4.5?

I've read here that there are some issues using ILMerge with .NET 4.5.

leppie
  • 115,091
  • 17
  • 196
  • 297
Christian Fredh
  • 899
  • 3
  • 12
  • 29
  • It seems to work locally on my machine with the same configuration, but I want to get some more information before I check the changes in... And the path to the .NET Framework assemblies looks like it should be updated at least. – Christian Fredh Jan 03 '13 at 13:01
  • Found some info in this question: http://stackoverflow.com/questions/10137937/c-merge-dll-into-exe-beginners-guide-needed – Christian Fredh Jan 03 '13 at 13:05
  • You could use the MSBuildExtensions library for this perhaps. – leppie Jan 03 '13 at 15:17
  • @leppie That library look interesting, I found a ILMerge task in that library (tested with ILMerge v2.10.0526, newest is v2.12.0803), haven't tested it, at least not yet... – Christian Fredh Jan 04 '13 at 08:25

1 Answers1

13

I can't find any good documentation on this, but as suggested by Matt Wrocks blog post and another question about ILMerge, I first tried to use the same Reference Assemblies path as for .NET 4.

This seemed to work at first, before re-targeting our NuGet packages to .NET 4.5. (Specifically Microsoft.AspNet.WebApi.Client which adds a reference to the new .NET 4.5 assembly System.Net.Http.WebRequest which before was included in the NuGet package.)

After updating reference assemblies path to .NET 4.5 it worked:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5

<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release' ">
    <CreateItem Include="@(ReferenceCopyLocalPaths)" Condition="'%(Extension)'=='.dll'">
        <Output ItemName="AssembliesToMerge" TaskParameter="Include" />
    </CreateItem>
    <PropertyGroup>
        <ReferenceAssemblies>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5</ReferenceAssemblies>
    </PropertyGroup>
    <Message Importance="high" Text="Executing ILMerge...with target platform from $(ReferenceAssemblies)" />
    <Exec Command="&quot;$(SolutionDir)LIB\ILMerge.exe&quot; /out:@(MainAssembly) /internalize /targetplatform:v4,&quot;$(ReferenceAssemblies)&quot; &quot;@(IntermediateAssembly)&quot; @(AssembliesToMerge->'&quot;%(FullPath)&quot;', ' ')" />
    <Delete Files="@(ReferenceCopyLocalPaths->'$(OutDir)%(DestinationSubDirectory)%(Filename)%(Extension)')" />
</Target>

In most cases a path to .NET 4 assemblies would also work, but when referencing new assemblies in .NET 4.5, the path need to be updated.

Note that in the example ILMerge.exe is downloaded to a folder in the solution directory called LIB.

Community
  • 1
  • 1
Christian Fredh
  • 899
  • 3
  • 12
  • 29
  • Error 1 The command ""d:\Work\DotNetWise\ExcludeLines\LIB\ILMerge.exe" /out:bin\Release\ExcludeLines.exe /internalize /targetplatform:v4,"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5" "obj\Release\ExcludeLines.exe" "d:\Work\DotNetWise\ExcludeLines\packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll"" exited with code 3. d:\Work\DotNetWise\ExcludeLines\ExcludeLines\ExcludeLines.csproj 71 5 ExcludeLines – DATEx2 May 26 '13 at 20:48
  • 1
    One thing that may further streamline this process is to add the nuget ilmerge package and reference the ilmerge.exe file from the packages folder under the solution. – BlueSam Mar 19 '14 at 05:15