Search code examples
c#.netvisual-studiowcfilmerge

Merging WCF references via ILMerge


I have a WinForm project in Visual Studio 2017 where I have created a WCF endpoint reference from a service's wsdl file. My project also uses several 3rd party dlls.

I want my project to be a standalone exe file that does not require installation, so I use ILMerge to package my dll files into a single exe file.

When running my compacted exe, I am getting exceptions about missing references to the WCF project endpoint:

Could not find default endpoint element that references contract in the ServiceModel client configuration section.

I tried looking up how to add WCF references via ILMerge, but I couldn't find anything relevant. I also cannot find anything WCF related in my project's bin output folder.

My current usage of ILMerge is via cmd:

ILMerge.exe /t:winexe /out:target.exe /targetplatform:v4,<.net v4 path> output.exe dll1.dll ... dlln.dll

I am looking for a way to add the WCF references to the standalone exe via ILMerge, or locating the WCF reference outputs generated by Visual Studio in my project's bin out directory.


Solution

Following Abraham's comment, I found a way around the 'service reference' issue.

I used Svcutil.exe to generate the definitions to my service, and manually set the binding to it like so:

BasicHttpBinding myBinding = new BasicHttpBinding();
myBinding.Security.Mode = BasicHttpSecurityMode.Transport;
EndpointAddress myEndpoint = new EndpointAddress(SERVICE_URL);
SoapServiceClient client = new SoapServiceClient(myBinding, myEndpoint);

Now everything gets packed when using ILMerge.


Solution

  • By adding service reference, there are also some extra configurations generated in the configuration files. Therefore, it might cause the failure of packing the project. Alternatively, we manually copy the System.servicemodel section to the new project. I suggest you change the way to consume the service. your project seems that you call the service by adding service reference. this will bring some other class libraries and references, which resulted that it is difficult to pack the project.
    Please consider the below way to call the service.
    https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory
    https://learn.microsoft.com/en-us/dotnet/framework/wcf/samples/channel-factory
    it fully encapsulates the required class library in the main project.
    Feel free to let me know if there is anything I can help with.