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

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
Create a new blank Xamarin.Android or Xamarin.iOS project.
Add the Microsoft.AspNetCore.SignalR.Client.Core version 1.0.0 NuGet package to the project.
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();
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?

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/

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:
Go to Tools > NuGet Package Manager > Manage NuGet Packages for Solution > Updates.
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.

Related

The type 'xct:TabView' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built

I'm getting the error of the tittle when trying to use XCT TabView.
I'm using Microsoft Visual Studio Community 2022. I installed XCT in my project by running both the commands in NuGet Package Manager Console:
Install-Package Xamarin.CommunityToolkit
Install-Package Xamarin.CommunityToolkit.Markup
In NuGet solution manager I selected both packages, I checked every checkbook responding to my projects and I hit "Install".
I get the following errors and warnings though:
Package restore failed. Rolling back package changes for 'MyProject'.
and
Error NU1605 Detected package downgrade: Xamarin.Forms from 5.0.0.2291 to 5.0.0.2196. Reference the package directly from the project to select a different version.
MyProject -> Xamarin.CommunityToolkit.Markup 2.0.0 -> Xamarin.Forms (>= 5.0.0.2291)
MyProject -> Xamarin.Forms (>= 5.0.0.2196)
I also added the relative namespace to XAML: xmlns:xct="http://xamarin.com/schemas/2020/toolkit
By the way, I get the same error too for xct:TabViewItem but I additionally get another error for it: Cannot resolve type "xct:TabViewItem"
Any ideas?
I had tried to do this and met the same problem as you.
This issue happened because the version of xamarin.forms is too low. So you can open the nuget package manager to update the xamarin.forms to the lastest version.
You can also download the earlier version of the Xamarin.CommunityToolkit and Xamarin.CommunityToolkit.Markup such as 1.3.1

.NET Core MSTest project can't find VisualStudio type or namespace in Microsoft namespace

Microsoft Visual Studio Professional 2019
Version 16.0.0
VisualStudio.16.Release/16.0.0+28729.10
I added a .NET Core 2.2 MSTest project and it added dependencies :
Microsoft.NET.Test.Sdk v16.0.1 ( I updated to v16.2.0 )
Microsoft.NET.Test.Sdk v1.3.2 ( I updated to v1.4.0 )
MSTest.TestFramework v1.3.2 ( I updated to v1.4.0 )
I compile and get this error :
CS0234 The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) ...\packages\microsoft.net.test.sdk\16.2.0\build\netcoreapp1.0\Microsoft.NET.Test.Sdk.Program.cs
Microsoft.NET.Test.Sdk.Program.cs
// <auto-generated> This file has been auto generated. </auto-generated>
using System;
[Microsoft.VisualStudio.TestPlatform.TestSDKAutoGeneratedCode]
class AutoGeneratedProgram {static void Main(string[] args){}}
Couple of observations :
Project is .NET Core 2.2 but it's using package from
netcoreapp1.0 folder
NuGet package manager successfully added package Microsoft.NET.Test.Sdk v16.0.1 but on build it has an unresolved
reference
The type or namespace name 'VisualStudio' does not exist in the
namespace 'Microsoft' (are you missing an assembly reference?)
...\packages\microsoft.net.test.sdk\16.2.0\build\netcoreapp1.0\Microsoft.NET.Test.Sdk.Program.cs
If we consume the Microsoft.NET.Test.Sdk package, when we compile and build our project, the Microsoft.NET.Test.Sdk.Program.cs will also be compiled by the csc.exe during the build.
So the error message indicates the Microsoft.VisualStudio.TestPlatform.TestSDKAutoGeneratedCode attribute can't be recognized well because something is wrong with the reference to Microsoft.TestPlatform.ObjectModel.dll.
Note:
1.Microsoft.VisualStudio.TestPlatform.TestSDKAutoGeneratedCode comes from assembly Microsoft.TestPlatform.ObjectModel.dll.
2.Microsoft.NET.Test.Sdk package depends on Microsoft.TestPlatform.TestHost package, and Microsoft.TestPlatform.TestHost package depends on Microsoft.TestPlatform.ObjectModel package.
If those packages are installed successfully, the compile and build should work. So actually I think something is wrong in your installed packages.
Some simple suggestions you can try:
1.Go Tools=>Nuget Package Manager=>Package Manager settings to make sure two options of Package Restore are enabled. And then delete the bin, obj folders and run a rebuild.
2.If #1 not works, try clean the nuget cache by UI in VS IDE, and then again delete the bin, obj folders, then run a rebuild.
3.Please update your VS IDE to latest version, 16.0.0 is too old, and latest version has fixed some issues.
Project is .NET Core 2.2 but it's using package from netcoreapp1.0
folder
If you open the package in path ...\packages\microsoft.net.test.sdk\16.2.0\build you can find this package only have three version of assemblies, net40, netcoreapp1.0 and uap10.0. Since your project is .net core 2.2, it will reference the assembly in netcoreapp1.0 folder. It's expected behavior.

Xamarin Forms App Fails to Deploy to Android After Adding AndroidX Migration Packages, error MSB4018 Xamarin.Android.Support.v7.AppCompat.dll

I recently added the AndroidX migration nugget packages to my Xamarin.Android project, within my Xamarin.Forms solution. I followed the following links for migrating to AndroidX:
Introduction to AndroidX for Xamarin
Link to AndroidX NuGet Packages used in the project
I had to add the AndroidX migration packages one-by-one to match the Android.Support libraries and rebuild the project till there are no more build errors. However, when I try to deploy the app to my Android device/emulator I get the following error:
k/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets(2283,3): error MSB4018: The "GenerateJavaStubs" task failed unexpectedly.
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets(2283,3): error MSB4018: System.IO.FileNotFoundException: Could not load assembly 'Xamarin.Android.Support.v7.AppCompat, Version=1.0.0.0, Culture=neutral, PublicKeyToken='. Perhaps it doesn't exist in the Mono for Android profile?
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets(2283,3): error MSB4018: File name: 'Xamarin.Android.Support.v7.AppCompat.dll'
Please also refer to the following link for the detailed build output from Visual Studio:
App's build output from Visual Studio
Any suggestions?

Could not load assembly 'Newtonsoft.Json. Perhaps it doesn't exist in the Mono for Android profile?

The whole exception message below:
Exception while loading assemblies: System.IO.FileNotFoundException:
Could not load assembly 'Newtonsoft.Json, Version=11.0.0.0,
Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. Perhaps it doesn't
exist in the Mono for Android profile?
I have changed my PCL library to .Net standard 1.6 and i can run library and IOS project successfully. But my android project gave me the above error. If i add the Newtonsoft.Json package to it it gives me different error.
error: package com.squareup.okhttp does not exist
com.squareup.okhttp.Callback
I am stuck with these errors.
This error happens at compile-time when you have the Newtonsoft.Json NuGet Package installed in the .NET Standard project and do not have it installed in the Android project.
To fix it, add the same version of the Newtonsoft.Json NuGet Package to the Android project as well as the .NET Standard project.

After updating Xamarin I get referenced component *** could not be found

After Updating Xamarin Forms via NuGet I can't build my Android App anymore. This are the errors:
Error This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see ... The missing file is ..\..\packages\Xamarin.Forms.2.5.0.121934\build\netstandard1.0\Xamarin.Forms.props
Warning The referenced component 'Xamarin.Forms.Platform.Android' could not be found.
Warning The referenced component '***' could not be found. (for all my references except my own added reference "shared code")
Warning IDE0006 Error encountered while loading the project. Some project features, such as full solution analysis for the failed project and projects that depend on it, have been disabled.
Warning "\packages\Xamarin.Forms.2.5.0.122203\build\netstandard1.0\Xamarin.Forms.props" cannot be imported again. It was already imported at "\RajaChat.Android\RajaChat.Android.csproj (3,3)". This is most likely a build authoring error. This subsequent import will be ignored.
I already tried the following from answers here on stack overflow or xamarin forums:
Deleting the package folder and let nuget restore restore the packets
Using nuget console with: "Update-Package –reinstall"
Clean Build and Rebuild with deleting obj and bin folders
Downgrade to Xamarin.Build.Download 0.4.6
Unload and Reload the Android Project
Nothing helped, always the same output...
I turned on the TRACEDESIGNTIME for debugging the IDE0006 Error but it only gave me the same error as above in the *.designtime.log:
\RajaChat\RajaChat\RajaChat.Android\RajaChat.Android.csproj(308,5): error : This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see . The missing file is ..\..\packages\Xamarin.Forms.2.5.0.121934\build\netstandard1.0\Xamarin.Forms.props.
Using Visual Studio 2017 and Xamarin Forms 2.5.0.122203
From your solution remove the nuget package Xamarin.Forms, in the .csproj of your Android project, remove the import (check if there are multiple references and delete them) of your Xamarin.Forms package. It should be something like:
<PackageReference Include="Xamarin.Forms" Version="2.5.0.121934" />
And then remove the Xamarin.Forms package from the packages folder.
Now the solution should be free from Xamarin.Forms package references, add the Xamarin.Forms Nuget package and rebuild.
Remove all the references and delete bin and object folders in .Droid. then save and re-open the solution. Wait few seconds for VS to download and add all references from nauget.

Resources