.NETStandard 多目标库包在构建时不会恢复 [英] .NETStandard multi-targeting library packages do not get restored when building

查看:47
本文介绍了.NETStandard 多目标库包在构建时不会恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个面向 net40net462netstandard2.0 的多目标库.这个库的目的是包装 StackExchange.Redis 和一些相关的重新连接逻辑.对于 net40,我需要使用比新目标更旧版本的包.这是我的 csproj 文件:

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.

我现在遇到的问题是试图为这个项目恢复 nuget 包.

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

  • 当我在 Visual Studio 中构建时,

  • When I build in Visual Studio,

  • 我从几个项目中得到了这个错误的实例间接引用我的多目标项目:

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

构建恢复了 NuGet 包.再次构建项目以在构建中包含这些包.有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=317568.

我还从直接或间接引用我的多目标库的各种项目中获得了更多此错误的实例.{Identifier}{Namespace} 有所不同,有些是指我的多目标库中的实体,有些是指依赖于它的项目中的实体.

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 命名空间{Namespace}"中不存在类型或命名空间名称{Identifier}"(您是否缺少程序集引用?)

如果我第二次构建,正如第一个错误所暗示的那样,我会得到同样的错误.

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

如果我在解决方案资源管理器中右键单击解决方案并选择Restore Nuget Packages 构建后,它什么都不做,并说:所有包都已经安装,没有什么可以恢复.

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.

如果我选择工具 > Nuget 包管理器 > 包管理器控制台并输入命令 dotnet restore 然后构建将正常工作.

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

该项目是解决方案中唯一使用多目标和 csproj 包参考格式的项目.

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

  • 我不需要向我的同事解释如何使用包管理器控制台.
  • 我可以使用 TeamCity 等第三方工具构建此解决方案,而无需额外设置.

以下是我迄今为止尝试过的一些方法:

Here are a few things I have tried so far:

  • 添加了 dotnet restore 作为项目预构建事件.这没有任何作用.
  • 使用 Visual Studio 解决方案构建事件 扩展来运行 dotnet restore 作为解决方案预构建事件.这在本地有效,但该扩展程序通常有点不可靠,并且对 TeamCity 等第三方构建没有帮助.
  • 更新 csproj 文件以使用带有 packages.config 文件的旧包参考格式.这引入了许多新的构建错误.
  • 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.
  • 从命令行我得到数百个 CS0246(找不到命名空间名称Foo"的类型(您是否缺少 using 指令或程序集引用?))和CS0234 错误,有些甚至是指 System.Net 等基类库中的类型.使用 -restore 开关似乎并没有改变这一点.由于那里的输出量太大,我可能忽略了一些东西.

  • 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.

这是一个显示类似行为的最小示例 https://github.com/JamesFaix/RestoreFailExample它会生成一个 CS0246 错误,这有点不同.当我在包管理器控制台中尝试 dotnet restore 时,它仍然失败.

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.

我使用的是 VS 15.9.9

I am using VS 15.9.9

我尝试了@Martin Ulrich 的解决方案.如果不更新我的 csproj 文件中的引用并删除 package.config 文件,这似乎不起作用.为此,我使用了 Visual Studio 从旧格式到 PackageReference 格式的内置转换.

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.

这稍微改变了我的错误.我发布的示例 repo 可能不够复杂.我现在得到了很多

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

找不到{项目文件夹}\packages.config.确保此项目已安装 Microsoft.Bcl.Build,并且 packages.config 位于项目文件旁边.,

一些

此项目引用了此计算机上缺少的 NuGet 包.使用 NuGet Package Restore 来下载它们.有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=322105.丢失的文件是 {path to project}\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets.

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.

还有一些 CS0246 错误,就像我之前遇到的一样.

and a few CS0246 errors like I was getting before.

CS0234 错误现已消失.

推荐答案

您正在从经典的 csproj 文件中引用基于 PackageReference 的项目.

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

默认情况下,这意味着您需要将 nuget 包手动安装到经典项目,因为它不使用 PackageReference 的传递行为.

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.

要解决此问题,您可以将以下内容添加到经典 csproj 文件的 :

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

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>

这篇关于.NETStandard 多目标库包在构建时不会恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆