ILMerge generated assembly doesn't run, although log output reports no errors - why is this?

前端 未结 5 1051
一向
一向 2021-02-03 11:41

I\'m testing out ILMerge for a new project, and although the .exe file seems to be created correctly, it won\'t run.

I have installed ILMerge via the .msi installer (fou

5条回答
  •  时光说笑
    2021-02-03 12:29

    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 has a more reliable way to get applications with multiple DLL dependencies to be deployable as a single assembly.

    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));
    

提交回复
热议问题