6

I have a solution in Visual Studio with 5 projects. They are:

  • Foo.Core: Core functionality
  • Foo.Api: Generated code built on top of core
  • Foo.Web: Web-specific extensions
  • Foo.Web.Mvc: MVC-specific extensions
  • Newtonsoft.Json: 3rd party library

I want to use ILMerge to merge Foo.Core, Foo.Api and Newtonsoft.Json into a single assembly, called Foo. That's the easy part.

The problem I'm running into is that Foo.Web and Foo.Web.Mvc both need to reference all three of the merged assemblies.

If I reference the original assemblies, they will have invalid references after I do the ILMerge.

If I reference the ILMerged assembly, I have to reference a debug assembly and then change it before I package everything up, which doesn't seem ideal.

I've tried creating a project called Foo, which references the 3 merged assemblies and replaces its own output with the ILmerged assembly, but that doesn't seem to work at all.

Is there a reliable way to do this?

Daniel Schaffer
  • 56,753
  • 31
  • 116
  • 165

2 Answers2

3

I know I'm coming really late to the game, but we were struggling with this exact same problem and here is how we solved it.

First, we edited the csproj files of all non-merged or satellite projects to use a conditional reference to an external assembly based upon existence of a file. In this case, we're going to test for the existence of the merged assembly. Then we ran a build script which does the following:

  1. Builds the solution.
  2. Runs ILMerge on your primary assemblies with the output being the file in the conditional reference.
  3. Rebuild the solution, but this time because the merged assembly exists, the non-merged or satellite assemblies will now have the correct reference.
Jonathan Oliver
  • 5,207
  • 32
  • 31
  • Here is an example of the above concept. be sure to Look at the ProjectReference and Reference tags: https://github.com/joliver/EventStore/blob/5fce28b92fa875ea8acdfea387161abdee3c8b00/src/proj/EventStore.Persistence.RavenPersistence/EventStore.Persistence.RavenPersistence.csproj – Jonathan Oliver Jan 07 '11 at 17:01
2

ILMerge is designed to create a new package/component as a boxed 'product' (API,Program...) to simplify assembly management like deployment, gac referencement, assembly visibility and more either for all-in-one use or external use, not mixed.

My guess is you have to setup a PostBuildEvent on your main assembly/project (Foo.Api?) if you have one or a PreBuildEvent in your Foo.Web and Foo.Web.Mvc projects to generate your merged Foo which will be referenced in your Foo.Web and Foo.Web.Mvc projects as an external assembly.

You can make this more integrated in Visual Studio by setting up a MsBuild task. A sample (from Stackoverflow).

Community
  • 1
  • 1
JoeBilly
  • 3,057
  • 29
  • 35
  • Yeah, I've tried doing that, but couldn't get the effect I was looking for. It looks like the ILMerge is going to need to be reserved for a final step before a release rather than a compile-time thing. – Daniel Schaffer May 08 '10 at 21:00
  • I think you can achieve this if your projects are build in the correct order. Have you set your project dependencies to ensure the build order ? – JoeBilly May 08 '10 at 23:41