How to specify different nuget package versions for different build configurations?

Viewed 3770

I'd like to specify different package versions for different project configurations. I know what this would look like in my .csproj file for non-NuGet dependencies ("bare" DLLs), but I'd like to try and get this working with NuGet packages too.

The problem is that NuGet dependencies are specified in packages.config and not in the .csproj file, where I could use the MSBuild Condition attribute. This is compounded by the fact that there does not appear to be any documentation for packages.config and the attributes it supports.

Can this be done? How?

3 Answers

In 2019 PackageReference which is using the PackageReference node to manage NuGet dependencies directly within project files (as opposed to a separate packages.config file) you can do it.

<ItemGroup>
<!-- ... -->
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" Condition="'$(TargetFramework)' == 'net452'" />
<!-- ... -->
</ItemGroup>

For more information you can check this link.

Visual Studio 2017 Version 15.7 and later supports migrating a project from the packages.config management format to the PackageReference format.

Related