Thursday, October 7, 2010

Conditional Reference in VS 2010

In one of our projects, we are using some third party components that have separate dlls for 32 bit and 64 bit. Since our old development box and production server were 32 bit, so we used the 32 bit version. No problem whatsoever.

In a different project for different client, our development box is a 64 bit machine. But, since we are running VS 2010 and using Cassini for our debugging, we use the 32 bit reference. But since our production box is running 64 bit, last minute before build for release in our build server, we swap the reference to 64 bit, re-check-in and build. So we just keep doing it that way - until one day, we forgot to do that and a production build got pushed to production environment and (of course) it breaks. So we fix it and then we vowed to find a solution to the last minute reference swap thing.

So here is how we fix it:
In VS, right click on the project that has the reference to the 32/64bit ref, do an unload project and open using XML editor and in it, you will see something like this:
...
    <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    </PropertyGroup>
...

    <ItemGroup>
        <Reference Include="Project.Reference.MyProject">
            <HintPath>..\References"Project.Reference.MyProject.40.x86.dll</HintPath>
        </Reference>
    </ItemGroup>
...
What you need to do is add a condition in your project file, so on certain environment it will use the x86 and x64 for other. The easiest way to do this (which is what we did) is adding a conditional statement itself on the reference element - when compiled as "debug", use the x86 dll and to use the x64 when compiled as "release". So we changed it to be like this:
 
...
    <ItemGroup>
        <Reference Include="Project.Reference.MyProject" Condition="'$(Configuration)' == 'Debug'">
            <HintPath>..\References\"Project.Reference.MyProject.40.x86.dll</HintPath>
        </Reference>
    </ItemGroup>
    <ItemGroup>
        <Reference Include="Project.Reference.MyProject" Condition="'$(Configuration)' == 'Release'">
            <HintPath>..\References\"Project.Reference.MyProject.40.x64.dll</HintPath>
        </Reference>
    </ItemGroup>
...

1 comment:

Anonymous said...

Perfect and painless! Thank you for putting the info out there! mw