Search code examples
nugetportable-class-library

Why does Nuget package pick the wrong platform?


I have a nuget package http://www.nuget.org/packages/Tavis.UriTemplates/ that has the following nuspec file,

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata>
    <id>Tavis.UriTemplates</id>
    <version>0.4</version>
    <authors>Darrel Miller</authors>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <title>URI Template resolution library</title>
    <description>Implementation of RFC 6570</description>
    <tags>http</tags>
        <releaseNotes>Added PCL version</releaseNotes>
        <projectUrl>https://github.com/tavis-software/UriTemplates</projectUrl>
        </metadata>
    <files>
        <file src="Packages\temp\UriTemplates\lib\Net35\*.*" target="lib\Net35" />
        <file src="Packages\temp\UriTemplates\lib\portable\*.*" target="lib\Portable-Net40+WinRT45+WP71+sl4" />
    </files>
</package>

If I install this package in a project that is .net40 or .net45 it selects the .net 35 DLL. Anyone have any idea why it doesn't select the PCL library?


Solution

  • NuGet treats PCL assemblies as less compatible than assemblies that target a specific version of the .NET framework.

    For your NuGet package you can either add a .NET 4.0 and a .NET 4.5 assembly or remove the .NET 3.5 assembly.

    Looking at the NuGet source code, in the VersionUtility class, a PCL assembly's weighting will be halved compared to an assembly that references the full version of .NET.

    // we divide by 2 to ensure Portable framework has less compatibility value than specific framework.
    return GetCompatibilityBetweenPortableLibraryAndNonPortableLibrary(projectFrameworkName, packageTargetFrameworkName) / 2;
    

    With your NuGet package, even though the PCL assembly's compatiblity is higher based on the value returned from the GetCompatibilityBetweenPortableLibraryAndNonPortableLibrary method, the division by two makes the .NET 3.5 assembly more compatible.