Sunday, February 5, 2017

ILMerge workflows Dynamics CRM

If you are creating plugins or workflow that have dependencies on third party libraries then there are two options:
1. Deploy the third party DLL in the GAC
2. ILMerge the thrid party DLL to your workflow or plugin.

Option 1 might seem much easier at first as you can simply go and install the DLL in the GAC. You have to do it once and forget it. However you'll have to do it in each of your enviornment like DEV, Test and Prod.   This eventually becomes a nightmare when you have a big farm, deploying dll's to each of the server in a farm is tedious and error prone, specially say you start referencing a new version of dll.

Option 2 is better approach. In this option you can merge the third party DLL to your dll and deploy your solution. All your dependencies are then in the solution file and you really don't care how big your server farm is.

To do ILMerge you have to download ILMerge from Microsoft. In your plugin or worfklow project, go to project settings and Build Events and In the Post-Build event put the following code, note you might have to change it according to your project structure. In the below example, I have a SolutionItems folder and I have copied the ILMerge tool to this folder. Note also that I am pointing to the location of .net v4.5.2 as ILMerge will require some of the dependencies of the framework. The libraries I am ILMerging are Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll and I am outputing it to Workflow.dll which is my custom dll contianing workflow activities.

Yes I am merging 2 dlls with my workflow dll.
Note the double quotes as longer file paths and paths with space can break if quotes are not used.
The $(SolutionDir) etc are Visual studio macros, you can google them they are quite handy and using them means that if you have got a build system attached, you will not have to do anything extra.


"$(SolutionDir)$(SolutionName)\SolutionItems\ILMerge.exe"  /keyfile:"$(ProjectDir)key.snk" /ndebug:false /targetplatform:v4,"$(MSBuildProgramFiles32)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2" /target:"library" /copyattrs /out:Workflow.dll "$(TargetDir)Workflow.dll" "$(SolutionDir)$(SolutionName)\SolutionItems\Microsoft.SharePoint.Client.dll" "$(SolutionDir)$(SolutionName)\SolutionItems\Microsoft.SharePoint.Client.Runtime.dll"

It was quite tedious to work it out, but works beautifully.
Hope it saves someone some pain.