JamesFaix
JamesFaix

Reputation: 8645

.NETStandard multi-targeting library packages do not get restored when building

Background:

I have created a multi-targeting library that targets net40, net462, and netstandard2.0. The purpose of this library is to wrap StackExchange.Redis and some related reconnect logic. For net40 I need to use an older version of the package than the newer targets. Here is my csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net40;net462;netstandard2.0</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
    <PackageReference Include="StackExchange.Redis">
      <Version>2.0.513</Version>
    </PackageReference>
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'net462'">
    <PackageReference Include="StackExchange.Redis">
      <Version>2.0.513</Version>
    </PackageReference>
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'net40'">
    <PackageReference Include="StackExchange.Redis.StrongName">
      <Version>1.1.608</Version>
    </PackageReference>
  </ItemGroup>

</Project>

Generally this library seems to work and can be consumed by applications on the different target frameworks.


Problem:

The problem I have now is trying to restore nuget packages for this project.

This project is the only one in the solution that uses multi-targeting and the csproj <PackageReference> package reference format.

I need restoring packages to be integrated into my build so that


Failed attempts:

Here are a few things I have tried so far:


Updates


I tried @Martin Ulrich's solution. This seemed to not work without also updating the references in my csproj files and removing package.config files. For this I used Visual Studio's built in conversion from the old format to the PackageReference format.

This changes my errors up a bit. The example repo I posted is probably not complex enough. I'm now getting a lot of

Could not locate {project folder}\packages.config. Ensure that this project has Microsoft.Bcl.Build installed and packages.config is located next to the project file.,

a few

This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {path to project}\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets.

and a few CS0246 errors like I was getting before.

The CS0234 errors are gone now.

Upvotes: 2

Views: 1591

Answers (1)

Martin Ullrich
Martin Ullrich

Reputation: 100543

You are referencing a PackageReference based project from a classic csproj file.

This, by default, means you need to manually install nuget packages to the classic project, as it is not using the transitive behavior of PackageReference.

To work around this issue, you can add the following to the <PropertyGroup> of your classic csproj file:

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>

Upvotes: 3

Related Questions