更新到Visual Studio 2017版本15.8预览后"无法解析引用:`System.Memory`"或"找不到`System.Memory`"

Bre*_*ski 9 xamarin.ios xamarin.android xamarin

如果它可以帮助其他任何人,我会询问并回答这个小问题,该问题可以在从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();
    
    Run Code Online (Sandbox Code Playgroud)
  4. 对于Xamarin.Android,尝试在Debug配置中构建项目.对于Xamarin.iOS,在iPhone模拟器上的Debug配置中构建并运行应用程序.

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`.
Run Code Online (Sandbox Code Playgroud)

对于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`.
Run Code Online (Sandbox Code Playgroud)

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

小智 6

我使用VS Entreprise 2017版本15.9.1

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

安装包System.Buffers-版本4.5.0

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


Bre*_*ski 5

我该如何解决这些错误?

要解决这些错误,请在使用它的所有项目中将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外观程序集).这意味着Xamarin项目现在依赖于NuGet包的实现.因此,System.Memory NuGet包版本4.5.1 包含Xamarin项目类型的占位符文件.

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