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

こ雲淡風輕ζ 提交于 2021-02-10 05:44:15

问题


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.

  • When I build in Visual Studio,

    • I get several instances of this error coming from projects that indirectly reference my multi-targeting project:

      The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568.

    • I also get many more instances of this error, from various projects that directly or indirectly reference my multi-targeting library. The {Identifier} and {Namespace} vary, some refer to entities in my multi-targeting library, some refer to entities in projects that depend on that.

      CS0234 The type or namespace name '{Identifier}' does not exist in the namespace '{Namespace}' (are you missing an assembly reference?)

    • If I build a second time, as the first error suggests, I get the same errors.

  • If I right-click the solution in the Solution Explorer and choose Restore Nuget Packages after building, it does nothing and says: All packages are already installed and there is nothing to restore.

  • If I choose Tools > Nuget Package Manager > Package Manager Console and enter the command dotnet restore then the build will work without error.

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

  • I don't need to explain to my coworkers how to work-around using the Package Manager Console.
  • I can build this solution using third-party tools like TeamCity without requiring additional setup.

Failed attempts:

Here are a few things I have tried so far:

  • Added dotnet restore as a project pre-build event. This doesn't do anything.
  • Used the Visual Studio Solution Build Events extension to run dotnet restore as a solution pre-build event. This works locally, but that extension is generally kind of unreliable and it won't help with third-party builds like TeamCity.
  • Update the csproj file to use the older package reference format with a packages.config file. This introduced a lot of new build errors.

Updates

  • From the command line I get hundreds of CS0246 (The type of namespace name 'Foo' could not be found (are you missing a using directive or an assembly reference?)) and CS0234 errors, some even referring to types in base class libraries like System.Net. Using the -restore switch doesn't seem to change that. I may be overlooking something due to the amount of output there.

  • Here is a minimal example that shows similar behavior https://github.com/JamesFaix/RestoreFailExample It generates a single CS0246 error, which is a little different. It also still fails when I try dotnet restore in the package manager console.

  • I am using VS 15.9.9


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.


回答1:


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>


来源:https://stackoverflow.com/questions/56938270/netstandard-multi-targeting-library-packages-do-not-get-restored-when-building

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!