如何将多个程序集合并为一个程序集?

我使用 EXE 项目(蔚蓝应用程序的启动任务)来消费我的服务栈,因为我已经将下面的服务栈的 DLL 和一些 Azure 的 DLL 复制到 EXE 项目中。

dlls

当我建立这个 EXE 项目,然后 Azure DLL 将捆绑与我的 EXE,但服务堆栈的 DLL 将不捆绑与 EXE,因为要运行我的 EXE 在任何机器上,我需要复制所有服务堆栈的 DLL 手动。

我已经使用了这个服务栈的 dll 来使用

JsonServiceClient client = new JsonServiceClient(servicepath);

我应该怎样做才能把所有这些 DLL 捆绑到我的 EXE 中?

101610 次浏览

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

You have several options:

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...

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

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

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:

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)

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/

.net core 3 introduces two new options in the project configuration, called single file publish and trimming.

You can find more details on docs here, project configuration copied here for reference.

  1. Project Configuration:
    <PropertyGroup>
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
</PropertyGroup>


<PropertyGroup>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
  1. Using CLI:
    dotnet publish -r win10-x64 -p:PublishSingleFile=true
dotnet publish -r <rid> -c Release

It is fully supported to combine the two options together to get a trimmed single assembly for your application.