Search code examples
c#asp.net-web-apiself-hostingilmerge

WebAPI and ILMerge (aka self-hosted single exe)


I try to build a single-executable self-hosted application with Web API. I started using OWIN and katana, but were not able to merge them in the end. So I made a very tiny sample project without OWIN and katana, Web API only.

In my output folder I get these files:

  • Newtonsoft.Json.dll
  • System.Net.Http.Formatting.dll
  • System.Web.Http.dll
  • System.Web.Http.SelfHost.dll

and of course the console app itself, in this case:

  • WebAPI_self_hosted.exe

If I copy all 5 files to a different machinge, everything works fine.

When I try to ILMerge them, I get the following error:

ILMerge.Merge: ERROR!!: Duplicate type 'System.Net.Http.HttpRequestMessageExtensions' found in
assembly 'System.Web.Http.SelfHost'. Do you want to use the /alllowDup option?
at ILMerging.ILMerge.MergeInAssembly(AssemblyNode a, Boolean makeNonPublic, Boolean targetAssemblyIsComVisible)
at ILMerging.ILMerge.Merge()
at ILMerging.ILMerge.Main(String[] args)

Please excuse me, if this question is not really up to stackoverflow standards, but I tried a lot the last two days (even ilrepack) and right now, I am thankful for every hint :-)

See also this post with projects attached: Error 2 The type <T> exists in both... with ILMerge


Solution

  • There are duplicate types so you need to configure ILMerge to properly handle them, such as using /allowDup option. The error message says it all.