How to merge multiple assemblies into one?

人盡茶涼 提交于 2019-11-26 00:32:45

问题


I consuming my service stack using EXE project (startup task for azure application) in that I have copied following service stack\'s DLL & some Azure\'s DLLs in to EXE project.

\"dlls\"

When I build this EXE project then Azure DLLs will be bundled with my EXE but service stack\'s DLL will not be bundled with EXE, because to run my EXE on any machine I need to copy all service stack\'s DLL manually.

I have used this service stack\'s dll to use

JsonServiceClient client = new JsonServiceClient(servicepath);

What should I have to do to bundled all these DLLs in to my EXE?


回答1:


You have several options:

  • use ILMerge (free)
    For howto see here and here

OR

  • use some tool like SmartAssembly (commercial)
    it can embed and merge among other things (no need to change your source code)

OR

  • code that yourself in less than 10 lines (free but minimal source code change)
    mark all needed dependencies as "embedded resource" - this way they are included in the EXE file... you need to setup an AssemblyResolve handler which at runtime reads from Resources and returns the needed DLLs to the .NET runtime...



回答2:


The tool you are looking for is called ILMerge . It is a command line tool and can be used like this:

ilmerge /target:winexe /out:MyApp.exe 
        MyExe.exe ServiceStack.dll ServiceStack.Interfaces.dll ServiceStack.ServiceInterface.dll  ServiceStack.Text.dll

There is also an article that describes how to include ILMerge into your VS project setup here




回答3:


A great tool to include referenced assemblies as embedded resources is Costura (a Fody add-in). The author Simon Kropp describes it as follows:

[...] a combination of two methods:

  • Jeffrey Richter's suggestion of using embedded resources as a method of merging assemblies
  • Einar Egilsson's suggestion using cecil to create module initializers

The result is a super simple solution which merely requires to fetch Costura.Fody from NuGet.

Features:

  • Including debug symbols
  • Compression of embedded assemblies
  • Including/excluding specific assemblies
  • Others (see Readme)



回答4:


Try ILMerge-GUI, the .NET merger. It's a GUI based Ilmerge which avoids all command line work.




回答5:


If you have WPF dependencies your options may be more limited..... ILMerge doesn't appear to deal with these. Costura.Fody (as mentioned by Codefox above) worked perfectly for us however and took about 5 minutes to get going... a very good experience.

Install with Nuget (selecting the correct default project in the Package Manager Console).

It merges the all DLLs marked "Copy Local" = true and produces a merged .EXE (alongside the standard output, most of which is now not necessary) which is also compressed. This can then be used standalone.

The license is MIT as so you can modify/distribute as required.

https://github.com/Fody/Costura/




回答6:


Checkout the ServiceStack.Gap project which shows several examples of howto ILMerge ServiceStack into a single cross-platform .exe.

ServiceStack also includes a number of other features that's particularly well suited for these creating embedded apps where it:

  • Allows your services to be self-hosted using .NET's HTTP Listener
  • Supports pre-compiled Razor Views
  • Supports Embedded Resources
  • Supports an embedded database in Sqlite and OrmLite
  • Can be ILMerged into a single .exe


来源:https://stackoverflow.com/questions/8077570/how-to-merge-multiple-assemblies-into-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!