NuGet update and conditional links

Our nupkg packages contain several versions of the same DLL (x86, x64, AnyCPU) and in csproj files. In links, I use conditional links to select a specific dll depending on the current platform set. As a result, I have several links to the same library (only different platform compilation).

here is a snippet of my csproj file:

<Reference Include="xxxx" Condition="'$(Platform)'=='x86'"> <HintPath>..\..\packages\xxxx.2.7.0.1093\lib\net45\x86\xxxx.dll</HintPath> </Reference> <Reference Include="xxxx" Condition="'$(Platform)'=='x64'"> <HintPath>..\..\packages\xxxx.2.7.0.1093\lib\net45\x64\xxxx.dll</HintPath> </Reference> <Reference Include="xxxx" Condition="'$(Platform)'=='AnyCPU'"> <HintPath>..\..\packages\xxxx.2.7.0.1093\lib\net45\AnyCPU\xxxx.dll</HintPath> </Reference> 

This design works great in both MSBuild and Visual Studio.

Unfortunately, after updating nuget, csproj links are messed up. Here is the result:

 <Reference Include="xxxx" Condition="'$(Platform)'=='x86'"> <HintPath>..\..\packages\xxxx.2.7.0.1093\lib\net45\x86\xxxx.dll</HintPath> </Reference> <Reference Include="xxxx" Condition="'$(Platform)'=='x64'"> <HintPath>..\..\packages\xxxx.2.7.0.1093\lib\net45\x64\xxxx.dll</HintPath> </Reference> <Reference Include="xxxx"> <HintPath>..\..\packages\xxxx.2.7.0.1094\lib\net45\x86\xxxx.dll</HintPath> </Reference> 

So it looks like only one link has been updated and ... the status section has been deleted and the first dll in the list has also been used.

Not what I expected. Any ideas how best to get around this problem? Does anyone use conditional links in your csproj with nuget? Any advice would be greatly appreciated!

+6
source share
1 answer

Nuget allows you to deploy a .targets file that is automatically included in your project (see Nuget docs ). You can include conditional links in your custom targets file and deploy the DLLs in the package tool folder so that they are not automatically added as links by Nuget.

Suppose your package is called "PackageWithConditionalReferences". The folder structure from which your nuget package is created may look like this:

 tools lib\net45\x86\xxxx.dll lib\net45\x64\xxxx.dll lib\net45\AnyCPU\xxxx.dll build PackageWithConditionalReferences.targets 

where PackageWithConditionalReferences.targets has the content:

  <?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <MyLibDir>$(MSBuildThisFileDirectory)..\tools\net45</MyLibDir> </PropertyGroup> <ItemGroup> <Reference Include="xxxx", Condition="'$(Platform)' == 'x64'"> <SpecificVersion>False</SpecificVersion> <HintPath>$(MyLibDir)\x64\xxxx.dll</HintPath> <Private>True</Private> </Reference> <Reference Include="xxxx", Condition="'$(Platform)' == 'x86'"> <SpecificVersion>False</SpecificVersion> <HintPath>$(MyLibDir)\x86\xxxx.dll</HintPath> <Private>True</Private> </Reference> <Reference Include="xxxx", Condition="'$(Platform)' == 'AnyCPU'"> <SpecificVersion>False</SpecificVersion> <HintPath>$(MyLibDir)\AnyCPU\xxxx.dll</HintPath> <Private>True</Private> </Reference> </ItemGroup> </Project> 

Make sure your .targets file is named your favorite package. After installing the package, you must restart VisualStudio so that the links become visible (checked using VisualStudio 2015).

+3
source

All Articles