Introduction:
Recently we got a business requirement where we need to merge two assemblies into single exe i.e. one .net assembly and another assembly which would serialize and deserialize, so we used Newtonsoft.Json.dll for it. To merge Newtonsoft.Json.dll with .net dll in single exe it is recommended to use ILMerge.
ILMerge is a tool that can combine multiple DLLs into a single exe. For this every time, we need to manually merge the assembly using ILMerge UI as shown below.
This blog explains the basics of using ILMerge to create a single Workflow/Plugin DLL without using ILMerge tool externally every time.
Step 1: Add ILMerge.MSBuild.Task using Nuget to your Visual Studio project:
Navigate to Visual Studio Project → Tools → Library Package Manager → Package Manager Console
Step 2: Use command “Install-Package ILMerge.MSBuild.Task” and paste in package manager console.
Step 3: Also need to install the ILMerge Package using command “Install-Package ilmerge” in the package manager console
Step 4: Build your project. After successful build one new folder is created with the name ILMerge. The merged assembly will be stored in an ILMerge folder under the project output. (Need to merge Crm.Workflow.dll and Newtonsoft.Json.dll)
Step 5: Merge Output is present in the ILMerge folder (Under ILMerge Folder Crm.Workflow.dll is merged output).The merged dll contains the component of both the dlls (i.e. Newtonsoft.Json.dll, Crm.Workflow.dll). Now you have to use this dll (i.e. Crm.Workflow.dll) for further processes to reflect the changes in your CRM.
Step 6: By default, all references with Copy Local equals true are merged with your project output. To avoid merging of CRM SDK DLLs and Microsoft.IdentityModel.DLL into our DLL we need to select the three files and set the property “Copy Local” to false.
Conclusion:
Merging of multiple dlls using external tool is time-consuming and somewhat annoying. So simply type the commands Install-Package ILMerge.MSBuild.Task, Install-Package ilmerge in package manager console of your project and after successful installation, rebuild your project and you will get newly merged dll for further processing to reflect changes in your CRM.
Thanks for continuously sharing helpful D365 articles, I recall Microsoft stating “8/31/15This post has been edited to reflect that Dynamics CRM does not support ILMerge. It isn’t blocked, but it isn’t supported, as an option for referencing custom assemblies” – https://blogs.msdn.microsoft.com/crm/2010/11/09/how-to-reference-assemblies-from-plug-ins/
Does this mean Microsoft has since changed their stance on this, or is this still unsupported?
Microsoft mentions in their blog post that the use of ILMerge is unsupported. The method described above is therefore not supported by Microsoft and should be used with caution.
Thanks!
step 4 isn’t working for me. I see the new folder but I only get a JSON file in it. the assembly and the dll are still only in the original location.
Hi,
Could you please check whether all references with Copy Local equals false, keep only two references true which you want to merge and check if issue resolve.
Thanks!
Hi, thanks for the article
I am also only seeing a JSON file in the ILMerge folder that has been created. Looking at the file it shows that there are only 2 DLLs that are meant to be merged.
Doing this in VS2019, would that matter?
Yes this should work in VS2019 as well. If you are facing any issue using ILMerge version 3.0.2.1 like this user had in VS2019 in this link -> https://github.com/dotnet/ILMerge/issues/62
then you can refer the following link for the same.
https://github.com/dotnet/ILMerge#installation
Our blog explains how to use ILMerge and versions of a visual studio like Visual 2019 shouldn’t be an issue for using ILMerge. Hope this helps
Worked like a charm for me.. Thanks for wonderful post.
Hi ,
This pretty much help me achieve the result. But I want my dll to be placed in Package Deploy instead of I L merge folder.
This will pretty much help me deploy changes directly from VS. I am using CRMSDK Template for deployment.
Do you know where i can change dll output path.
Regards,
Ankit
Please refer below links,
https://github.com/emerbrito/ILMerge-MSBuild-Task/wiki
https://github.com/emerbrito/ILMerge-MSBuild-Task/wiki/Config-File
You would need to use OutputFile property to add custom location for the merged assembly.
Hope this helps!
Thanks!