Click here to Skip to main content
15,512,456 members
Articles / All Topics
Technical Blog
Posted 20 Feb 2015

Stats

19.4K views
17 bookmarked

Specify Assembly References Based On Build Configuration in Visual Studio

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
20 Feb 2015CPOL3 min read
Specify Assembly References Based On Build Configuration in Visual Studio

Introduction

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 11.0.0.0
  • Build Configuration 2 -> Reference “Microsoft.TeamFoundation” assembly version 12.0.0.0

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

    Image 1

  • Choose “New” from the “Active solution configurations” drop down

    Image 2

  • Create new configuration copied from the Release configuration- name it “VisualStudio_2012“.

    New Build Configuration Window

  • 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.

Unload Project Visual Studio

Edit Project Visual Studio

The line below is the standard MSBuild code which includes an assembly to your project.

XML
<Reference Include="Microsoft.TeamFoundation.Client, Version=11.0.0.0, Culture=neutral, 
    PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"/>

The code adds Microsoft.TeamFoundation.Client DLL version 11.0.0.0.

In order to add the DLL only when the build configuration is set to “VisualStudio_2012”, you can add the following code.

XML
<Reference Include="Microsoft.TeamFoundation.Client, Version=11.0.0.0, 
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, 
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 11.0.0.0 of the Microsoft.TeamFoudation.Client will be added.

In order to add version 12.0.0.0 of the same DLL in “VisualStudio_2013” configuration, add the code below.

XML
<Reference Include="Microsoft.TeamFoundation.Client, Version=12.0.0.0, Culture=neutral, 
PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" 
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.

Duplicated Assemblies 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!

Source Code

The post- Specify Assembly References Based On Build Configuration in Visual Studio appeared first on Automate The Planet.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Automate The Planet
Bulgaria Bulgaria
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.

Comments and Discussions

 
QuestionAnd simpler still... Pin
aodennison20-Mar-19 14:06
aodennison20-Mar-19 14:06 
SuggestionUse Choose/When conditional Pin
Member 120876415-Nov-15 5:13
Member 120876415-Nov-15 5:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.