Search code examples
pluginsdynamics-crmilmergefakexrmeasy

CRM using ILMerge to merge framework library with plugin projects


I have two assemblies:

  1. Main plugin assembly - Plugin used for my project
  2. Framework assembly - I want to merge this assembly with the main plugin so I can reuse some common methods used a lot in different projects.

I installed ILMerge on the main plugin assembly and referenced the built out framework dlls while simultaneously setting project build order on the solution.

Now, this looks okay before I deploy. The main problem comes when I try to debug using my unit tests project.

In my unit tests project, I have a reference to the main plugin assembly which allows me to use fakexrmeasy to run my unit tests. However, now that the assemblies have been merged I expect that I should be able to use the framework methods within my unit tests project. I cant seem to access those methods when referencing the main plugin assemblies in the test classes.

All of the projects mentioned above exist within the same solution.

Im fairly new to ILMerge so might be doing something wrong thats glaringly obvious. I just compile using the built in visual studio compiler.

Can anyone suggest what could be going wrong?


Solution

  • First, my advice is to use ILRepack, since ILMerge is no longer being actively maintained. ILRepack is based on ILMerge and is open source. Add NuGet package ILRepack.Lib.MSBuild.Task to your project.

    Then add this file to your project and name it ILRepack.targets:

    <?xml version="1.0" encoding="utf-8" ?>
    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <Target Name="ILRepacker" AfterTargets="Build">
        <ItemGroup>
          <InputAssemblies Include="$(OutputPath)$(TargetName)$(TargetExt)" />
          <InputAssemblies Include="$(OutputPath)YourFramework.dll" />
        </ItemGroup>
        <ILRepack
            Parallel="true"
            Internalize="false"
            InternalizeExclude="@(DoNotInternalizeAssemblies)"
            InputAssemblies="@(InputAssemblies)"
            LibraryPath="$(OutputPath)"
            Wildcards="false"
            TargetKind="SameAsPrimaryAssembly"
            DebugInfo="false"
            KeyFile="YourCompany.snk"
            OutputFile="$(OutputPath)Merged\$(AssemblyName).dll"
            LogFile="$(OutputPath)Merged\ILRepack.log"
        />
      </Target>
    </Project>
    

    Note according to this configuration your merged dll is created in a separate folder named "Merged". This is the library you register on Dynamics 365. Your unit tests should simply use the plugin project's regular build output.