I have a Windows service that must be able connect to CRM systems of version 2011 or newer. Previously, the strategy for this scenario had been to use the lowest version of dependencies, which means CRM SDK 5 (2011) and .NET Framework 4.0. This used to work with CRM 2011, 2013, 2015 and 2016 OnPremises. But there was a problem with CRM 2016 Online due to changes in the login mechanism. Connecting to CRM 2016 Online was only possible using the current SDK version 8.0.
Two obvious solutions to this problem presented themselves:
- Always compile using the latest CRM SDK version.
- Maintain a second branch in version control.
However, both solutions have major disadvantages:
- That would have changed the dependancy of .NET Framework to 4.5.2, which was not an option.
- Another branch means additional maintenance effort.
So, I was looking for a third, more elegant solution using msbuild-magic.
Even though this example uses the Microsoft CRM SDK, it can be applied to any third-party library. Additionally, by using a conditional compilation symbol, the behaviour of your application may be adopted to different versions of your api (e.g. use new features, omit deprecated functions).
Creating the example solution
Usually, if you run into this problem, you will already have a rather complex solution in place. So, I will keep it brief.
I use a simple console application based on .NET 4.0 in this example. Additionally, I will create a _lib directory containing sub-directories for each version of my third-party assemblies. (A solution using nuget would be preferable. I have not tried this yet, however.)
My example solution looks like this:
I now create a new build configuration Release_2016, copying settings from the existing Release configuration.
Subsequently, I define a conditional compilation symbol CRM2016 (any name will do) in the project settings on the Builds tab. This is an optional step and may be used for conditional compile. However, it’s not necessary for what we want to achieve in this blog entry.
The next steps require editing the project file itself. Therefore, the project has to be unloaded in the Solution Explorer (right click → Unload project) and afterwards opened in the XML editor (right click → Edit ConsoleApplication_Example.csproj).
Conditional compilation: .NET Framework Version
To overwrite the .NET Framework 4.0 version, which is used by default for the configuration „Release_2016“, a TargetFrameworkVersion element must be added to the approriate PropertyGroup and set to Version 4.5.2:
Now, when choosing the build configuration Release_2016, the application is compiled against .NET 4.5.2. For all other configurations version 4.0 is used.
Conditional compilation: Third Party Assemblies
This is where things get a little tricky. First, the references to the third-party assemblies must be removed from the default ItemGroup element. Therefore, the approriate Reference elements must be deleted.
Subsequently, a MSBuild Choose element is used to add the references to the specific version in a new ItemGroup. When choosing configuration „Release_2016“, we want the assemblies from _lib\CRM 2016 to be referenced. In all other cases, those from _lib\CRM 2011 should be referenced.
This looks like the following:
After reloading the project in the Solution Explorer, we can conditionally compile against either .NET 4.0 and CRM 2011 SDK or .NET 4.5.2 and CRM 2016 SDK, depending on the build configuration.
Since only standard MSBuild mechanisms are used, this solution works both when compiling in Visual Studio as well as in continuous integration Builds, for example TFS.