Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Can not resolve reference: `System.Memory`" or "Could not find `System.Memory`" after update to Visual Studio 2017 version 15.8 Preview

In case it might help anyone else, I'll ask and answer this little question about an issue that can appear in Xamarin.Android and Xamarin.iOS projects after an update from Visual Studio 2017 version 15.7 to version 15.8 Preview.

Example steps to demonstrate the issue

  1. Create a new blank Xamarin.Android or Xamarin.iOS project.
  2. Add the Microsoft.AspNetCore.SignalR.Client.Core version 1.0.0 NuGet package to the project.
  3. Use a type from the NuGet package in the project. For example, add the following line to any existing method:

    var connection = new Microsoft.AspNetCore.SignalR.Client.HubConnectionBuilder().Build();
    
  4. For Xamarin.Android, attempt to build the project in the Debug configuration. For Xamarin.iOS, build and run the app in the Debug configuration on iPhone simulator.

Results with Visual Studio 2017 version 15.7

Both the Xamarin.Android app and the Xamarin.iOS app build and run without error.

Results with Visual Studio 2017 version 15.8 Preview 4

For Xamarin.Android, the build fails:

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

For Xamarin.iOS, the app launches successfully on the simulator, but the debug output shows that the app fails to find System.Memory when it's running:

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

How do I resolve these errors? As a follow-up, why did these errors appear after I updated Visual Studio?

like image 837
Brendan Zagaeski Avatar asked Jul 12 '18 03:07

Brendan Zagaeski


2 Answers

I use VS Entreprise 2017 Version 15.9.1

To solve the problem I face ( Can not resolve reference: System.Buffers, referenced by System.Memory. Please add a NuGet package or assembly reference for System.Buffers, or remove the reference to System.Memory.), I just install System.Buffers package by this command:

Install-Package System.Buffers -Version 4.5.0

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

like image 80
A. Blaise Avatar answered Nov 20 '22 01:11

A. Blaise


How do I resolve these errors?

To resolve these errors, update the Microsoft.AspNetCore.SignalR.Client.Core NuGet package to version 1.0.1 or higher in all the projects that use it. For example:

  1. Go to Tools > NuGet Package Manager > Manage NuGet Packages for Solution > Updates.

  2. Update Microsoft.AspNetCore.SignalR.Client.Core to version 1.0.1 or higher.

In Microsoft.AspNetCore.SignalR.Client.Core version 1.0.1, the System.Memory NuGet dependency is now (>= 4.5.1) rather than (>= 4.5.0). And because System.Memory version 4.5.1 no longer includes placeholder files for Xamarin project types, Xamarin projects will now use the .NET Standard 2.0 assembly directly from the NuGet package. This solves the errors because the Xamarin projects can now successfully locate the System.Memory reference.

As a follow-up, why did these errors appear after I updated Visual Studio?

The errors appeared because the Xamarin SDK versions in Visual Studio 2017 version 15.7 and version 15.8 have a tricky compatibility relationship with the System.Memory NuGet package versions. In Visual Studio 2017 version 15.7, Xamarin.Android and Xamarin.iOS use their own built-in implementations of the types from that NuGet package, so they don't need to reference the assembly from the NuGet package. The System.Memory NuGet package version 4.5.0 accordingly includes placeholder files for Xamarin project types to prevent conflicts.

But the built-in implementations in the current Xamarin SDK releases do not yet cover the full surface area of the NuGet package. So for Visual Studio 2017 version 15.8, the Mono team temporarily hid those built-in implementations (by switching them to internal and removing the System.Memory facade assembly). This means that Xamarin projects now depend on the implementations from the NuGet package instead. And the System.Memory NuGet package version 4.5.1 accordingly does not include placeholder files for Xamarin project types.

One more thing to keep in mind is that the built-in implementations in the Xamarin SDKs will cover the full surface area of the NuGet package in a future release. The built-in implementations will then be un-hidden and the placeholder files will be added back to the NuGet package. Users will need to update their NuGet packages once more when that happens. That next change will be coming in Visual Studio 2017 version 15.9 or later.

like image 35
Brendan Zagaeski Avatar answered Nov 20 '22 01:11

Brendan Zagaeski