I wanted to create different builds for my application “Test Case Manager” for the different versions of Visual Studio because of the referenced Microsoft TFS assemblies. Also, I didn’t want to create different projects because there were no changes in the methods that I was using. So I created conditional referencing of the assemblies depending on build configurations. In the post, I will show you how you can accomplish the same thing.
Main goal to achieve:
- Build Configuration 1 -> Reference “Microsoft.TeamFoundation” assembly version 220.127.116.11
- Build Configuration 2 -> Reference “Microsoft.TeamFoundation” assembly version 18.104.22.168
How to Specify Assembly References Based On Build Configuration?
First, you need to create two new build configurations in you solution- VisualStudio_2012 and VisualStudio_2013. In order to do that, follow the steps below:
- Open your solution
- Click Configuration Manager button in the builds configurations drop down
- Choose “New” from the “Active solution configurations” drop down
- Create new configuration copied from the Release configuration- name it “VisualStudio_2012“.
- Repeat the same actions for the second configuration “VisualStudio_2013“.
Next, you need to the MSBuild file of your project. In order to accomplish that, you need to unload the project and edit it.
The line below is the standard MSBuild code which includes an assembly to your project.
<Reference Include="Microsoft.TeamFoundation.Client, Version=22.214.171.124, Culture=neutral,
The code adds
Microsoft.TeamFoundation.Client DLL version 126.96.36.199.
In order to add the DLL only when the build configuration is set to “VisualStudio_2012”, you can add the following code.
<Reference Include="Microsoft.TeamFoundation.Client, Version=188.8.131.52,
processorArchitecture=MSIL" Condition="'$(Configuration)' == 'VisualStudio_2012' OR
'$(Configuration)' == 'Debug' OR '$(Configuration)' == 'Release'"/>
We use MSBuild condition statement where we access the current build configuration through the property
$(Configuration). Now when you build your project in Release, Debug or VisualStudio_2012 build configurations- version 184.108.40.206 of the
Microsoft.TeamFoudation.Client will be added.
In order to add version 220.127.116.11 of the same DLL in “VisualStudio_2013” configuration, add the code below.
<Reference Include="Microsoft.TeamFoundation.Client, Version=18.104.22.168, Culture=neutral,
Condition="'$(Configuration)' == 'VisualStudio_2013'"/>
Finally, save the file and reload your project.
When you open the “References” of your project after the new code is added, you will notice that the “conditional” DLLs are duplicated and one of them is marked as “not found” by Visual Studio.
Don’t worry, the project is building just fine in all build configurations, the only difference is that when you change the build configurations- different versions of the referenced assemblies are added.
So Far in the C# Series
1. Implement Copy Paste C# Code
2. MSBuild TCP IP Logger C# Code
3. Windows Registry Read Write C# Code
4. Change .config File at Runtime C# Code
5. Generic Properties Validator C# Code
6. Reduced AutoMapper- Auto-Map Objects 180% Faster
7. 7 New Cool Features in C# 6.0
8. Types Of Code Coverage- Examples In C#
9. MSTest Rerun Failed Tests Through MSTest.exe Wrapper Application
10. Hints For Arranging Usings in Visual Studio Efficiently
11. 19 Must-Know Visual Studio Keyboard Shortcuts – Part 1
12. 19 Must-Know Visual Studio Keyboard Shortcuts – Part 2
13. Specify Assembly References Based On Build Configuration in Visual Studio
14. Top 15 Underutilized Features of .NET
15. Top 15 Underutilized Features of .NET Part 2
16. Neat Tricks for Effortlessly Format Currency in C#
17. Assert DateTime the Right Way MSTest NUnit C# Code
18. Which Works Faster- Null Coalescing Operator or GetValueOrDefault or Conditional Operator
19. Specification-based Test Design Techniques for Enhancing Unit Tests
20. Get Property Names Using Lambda Expressions in C#
21. Top 9 Windows Event Log Tips Using C#
If you enjoy my publications, feel free to SUBSCRIBE
Also, hit these share buttons. Thank you!
The post- Specify Assembly References Based On Build Configuration in Visual Studio appeared first on Automate The Planet.
CTO and Co-founder of Automate The Planet Ltd, inventor of BELLATRIX Test Automation Framework, author of "Design Patterns for High-Quality Automated Tests: High-Quality Test Attributes and Best Practices" in C# and Java. Nowadays, he leads a team of passionate engineers helping companies succeed with their test automation. Additionally, he consults companies and leads automated testing trainings, writes books, and gives conference talks. You can find him on LinkedIn every day.