Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Specify Assembly References Based On Build Configuration in Visual Studio

5.00/5 (11 votes)
20 Feb 2015CPOL3 min read 22.2K  
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)