【问题标题】:"Can not resolve reference: `System.Memory`" or "Could not find `System.Memory`" after update to Visual Studio 2017 version 15.8 Preview更新到 Visual Studio 2017 版本 15.8 预览版后,“无法解析引用:`System.Memory`”或“找不到`System.Memory`”
【发布时间】:2018-07-12 03:10:37
【问题描述】:

如果它可能对其他人有所帮助,我将就从 Visual Studio 2017 版本 15.7 更新到版本 15.8 预览后可能出现在 Xamarin.Android 和 Xamarin.iOS 项目中的问题提出并回答这个小问题。

演示问题的示例步骤

  1. 创建一个新的空白 Xamarin.Android 或 Xamarin.iOS 项目。
  2. 将 Microsoft.AspNetCore.SignalR.Client.Core 版本 1.0.0 NuGet 包添加到项目中。
  3. 在项目中使用 NuGet 包中的类型。例如,将以下行添加到任何现有方法中:

    var connection = new Microsoft.AspNetCore.SignalR.Client.HubConnectionBuilder().Build();
    
  4. 对于 Xamarin.Android,尝试在调试配置中构建项目。对于 Xamarin.iOS,在 iPhone 模拟器上的调试配置中构建并运行应用程序。

Visual Studio 2017 版本 15.7 的结果

Xamarin.Android 应用和 Xamarin.iOS 应用都生成并运行无错误。

Visual Studio 2017 版本 15.8 Preview 4 的结果

对于 Xamarin.Android,构建失败:

Xamarin.Android.Common.targets(1831,2): error XA2002: Can not resolve reference: `System.Memory`, referenced by `Microsoft.AspNetCore.Connections.Abstractions`. Please add a NuGet package or assembly reference for `System.Memory`, or remove the reference to `Microsoft.AspNetCore.Connections.Abstractions`.
Xamarin.Android.Common.targets(1831,2): error XA2002: Can not resolve reference: `System.Memory`, referenced by `Microsoft.AspNetCore.SignalR.Client.Core`. Please add a NuGet package or assembly reference for `System.Memory`, or remove the reference to `Microsoft.AspNetCore.SignalR.Client.Core`.
Xamarin.Android.Common.targets(1831,2): error XA2002: Can not resolve reference: `System.Memory`, referenced by `Microsoft.AspNetCore.SignalR.Common`. Please add a NuGet package or assembly reference for `System.Memory`, or remove the reference to `Microsoft.AspNetCore.SignalR.Common`.
Xamarin.Android.Common.targets(1831,2): error XA2002: Can not resolve reference: `System.Memory`, referenced by `Microsoft.AspNetCore.SignalR.Protocols.Json`. Please add a NuGet package or assembly reference for `System.Memory`, or remove the reference to `Microsoft.AspNetCore.SignalR.Protocols.Json`.
Xamarin.Android.Common.targets(1831,2): error XA2002: Can not resolve reference: `System.Memory`, referenced by `Microsoft.Extensions.Primitives`. Please add a NuGet package or assembly reference for `System.Memory`, or remove the reference to `Microsoft.Extensions.Primitives`.
Xamarin.Android.Common.targets(1831,2): error XA2002: Can not resolve reference: `System.Memory`, referenced by `System.IO.Pipelines`. Please add a NuGet package or assembly reference for `System.Memory`, or remove the reference to `System.IO.Pipelines`.

对于 Xamarin.iOS,应用在模拟器上成功启动,但调试输出显示应用在运行时找不到 System.Memory:

Could not find `System.Memory` referenced by assembly `System.IO.Pipelines, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51`.
Could not find `System.Memory` referenced by assembly `Microsoft.AspNetCore.SignalR.Client.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60`.
Could not find `System.Memory` referenced by assembly `Microsoft.AspNetCore.SignalR.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60`.
Could not find `System.Memory` referenced by assembly `Microsoft.Extensions.Primitives, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60`.
Could not find `System.Memory` referenced by assembly `Microsoft.AspNetCore.Connections.Abstractions, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60`.
Could not find `System.Memory` referenced by assembly `Microsoft.AspNetCore.SignalR.Protocols.Json, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60`.

如何解决这些错误?追问一下,为什么我更新Visual Studio后会出现这些错误?

【问题讨论】:

    标签: xamarin xamarin.ios xamarin.android


    【解决方案1】:

    我使用 VS Entreprise 2017 版本 15.9.1

    解决我面临的问题(无法解决参考:System.Buffers,由System.Memory 引用。请为System.Buffers 添加NuGet 包或程序集引用,或删除对System.Memory 的引用。), 我只是通过这个命令安装 System.Buffers 包:

    安装包 System.Buffers -Version 4.5.0

    https://www.nuget.org/packages/System.Buffers/

    【讨论】:

      【解决方案2】:

      如何解决这些错误?

      要解决这些错误,请在所有使用它的项目中将 Microsoft.AspNetCore.SignalR.Client.Core NuGet 包更新到版本 1.0.1 或更高版本。例如:

      1. 转到 工具 > NuGet 包管理器 > 管理用于解决方案的 NuGet 包 > 更新

      2. 将 Microsoft.AspNetCore.SignalR.Client.Core 更新到 1.0.1 或更高版本。

      在 Microsoft.AspNetCore.SignalR.Client.Core 版本 1.0.1 中,System.Memory NuGet 依赖项现在是 (>= 4.5.1) 而不是 (>= 4.5.0)。由于 System.Memory 版本 4.5.1 no longer includes placeholder files for Xamarin project types,Xamarin 项目现在将直接使用 NuGet 包中的 .NET Standard 2.0 程序集。这解决了错误,因为 Xamarin 项目现在可以成功定位 System.Memory 引用。

      作为后续,为什么我更新Visual Studio后会出现这些错误?

      出现错误是因为 Visual Studio 2017 版本 15.7 和版本 15.8 中的 Xamarin SDK 版本与 System.Memory NuGet 包版本存在棘手的兼容性关系。在 Visual Studio 2017 版本 15.7 中,Xamarin.Android 和 Xamarin.iOS 使用该 NuGet 包中它们自己的内置类型实现,因此它们不需要引用 NuGet 包中的程序集。 System.Memory NuGet 包版本 4.5.0 相应地包含 Xamarin 项目类型的占位符文件以防止冲突。

      但当前 Xamarin SDK 版本中的内置实现尚未涵盖 NuGet 包的全部表面区域。因此,对于 Visual Studio 2017 版本 15.8,Mono 团队暂时隐藏了那些内置实现(通过 switching them to internalremoving the System.Memory facade assembly)。这意味着 Xamarin 项目现在依赖于 NuGet 包中的实现。 System.Memory NuGet 包版本 4.5.1 相应地包含 Xamarin 项目类型的占位符文件。

      要记住的另一件事是,Xamarin SDK 中的内置实现未来版本中覆盖 NuGet 包的全部表面区域。然后将取消隐藏内置实现,并将占位符文件添加回 NuGet 包。发生这种情况时,用户将需要再次更新他们的 NuGet 包。下一个更改将出现在 Visual Studio 2017 版本 15.9 或更高版本中。

      【讨论】:

      • 这太可怕了。我刚刚将 Visual Studio 升级到 15.8,现在我的 Xamarin 版本已完全损坏(不幸的是,安装 SignalR 不起作用)。这是否意味着我必须等到 V 15.9 才能再次工作?
      • @RobLyndon,作为澄清,听起来您的项目之前没有使用 any 版本的 SignalR?这个问题 + 答案特别适用于已经使用 SignalR 的项目。但是任何依赖于 System.Memory NuGet 包的 NuGet 包都可能出现同样的问题。关键是您需要将解决方案中所有项目中的 System.Memory NuGet 包更新到版本 4.5.1 或更高版本,以与 Visual Studio 2017 版本 15.8 中的 Xamarin SDK 版本兼容。您还有低于 4.5.1 的 System.Memory 版本吗?
      • 我不这么认为,但我会检查我的 bin 文件夹。
      • 啊,这很有趣。 Visual Studio 正在从 GAC 添加 System.Memory,但我在 fsproj 文件中添加了一个 HintPath,将其指向我的包文件夹中的 dll。感谢您的帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-15
      相关资源
      最近更新 更多