更新到 Visual Studio 2017 版本 15.8 预览版后"Can not resolve reference: `System.Memory`"或"Could not find `Sy



如果它可能对其他人有帮助,我会问并回答这个小问题,这个问题可能会在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,尝试在Debug配置中构建项目。对于Xamarin.iOS,在iPhone模拟器的调试配置中构建并运行该应用程序。

Visual Studio 2017 15.7版本的结果

Xamarin.Android应用程序和Xamarin.iOS应用程序都构建并运行无错误。

Visual Studio 2017版本15.8预览版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后会出现这些错误?

我使用VS Entreprise 2017版本15.9.1

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

安装软件包系统缓冲区-4.5.0版

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

如何解决这些错误?

若要解决这些错误,请在所有使用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不再包括Xamarin项目类型的占位符文件,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团队暂时隐藏了这些内置实现(通过将它们切换到internal并删除System.Memory facade程序集(。这意味着Xamarin项目现在依赖于NuGet包中的实现。相应地,System.Memory NuGet包4.5.1版不包含Xamarin项目类型的占位符文件。

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

相关内容

最新更新