Search code examples
c#buildenterprise-libraryilmerge

iLMerge Enterprise Library 5.0


Has anyone successfully iLMerged Enterprise Library 5.0? iLMerge successfully creates the merged dll and my mvc application compiles, but when launching the application I receive the following error:

SecurityTransparent and SecurityCritical attributes cannot 
be applied to the assembly scope at the same time.

Solution

  • ILMerge is great if you wrote all of the assemblies that you're trying to merge, and you know that none of them are making assumptions about assembly organization. But under many circumstances (especially ones where heavy reflection or the Dynamic Language Runtime are involved), ILMerge just doesn't work. Sometimes things fail in surprising and mysterious ways.

    When ILMerge fails, Jeffrey Richter h a more reliable way to get applications with multiple DLL dependencies to be deployable as a single assembly.

    With his approach, every assembly gets to retain its own name, strongname, and attributes, which greatly improves your chances of the resulting application just working.

    It isn't without trade-offs, but even the ILMerge author, Mike Barnett, said in the comment thread on that blog post "As the author of ILMerge, I think this is fantastic! If I had known about this, I never would have written ILMerge."

    If you can use Richter's method, you won't trip over most of the reflection or dynamism traps.

    The implementation steps

    1. Embed all of the third-party assemblies that you depend on in your application's Resources.
    2. Register a ResolveEventHandler with the AppDomain.CurrentDomain.AssemblyResolve event.
    3. When your handler gets called with an assembly that you stashed in Resources, load the assembly.

    You do part 3 as follows:

    var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
    return Assembly.Load(new BinaryReader(resourceStream).ReadBytes(int.MaxValue));