Xamarin.forms: Broken build for latest version of System.Runtime.CompilerServices.Unsafe

Created on 30 May 2018  ·  19Comments  ·  Source: xamarin/Xamarin.Forms

Description

Upgrading System.Runtime.CompilerServices.Unsafe to Version 4.5 generates the following errors:

On the iOS side: Can't resolve the reference 'System.ReadOnlySpan1', referenced from the method 'System.ReadOnlySpan1 Microsoft.Extensions.Primitives.StringSegment::AsSpan()' in 'System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'

On the Android side:
Exception while loading assemblies: System.IO.FileNotFoundException: Could not load assembly 'System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. Perhaps it doesn't exist in the Mono for Android profile?

Steps to Reproduce

  1. Take an existing Xamarin Forms project
  2. Upgrade System.Runtime.CompilerServices to Version 4.5
  3. 3.

Expected Behavior

Successful buiild

Actual Behavior

The build fails with the errors described above.

Basic Information

  • Version with issue:
  • Last known good version:
  • IDE:
  • Platform Target Frameworks:

    • iOS:

    • Android:

    • UWP:

  • Android Support Library Version:
  • Nuget Packages:
  • Affected Devices:

Screenshots

Reproduction Link

Most helpful comment

I solved it by downgrading all Microsoft.extensions.* packages to Version 2.0.0 by hand. To do this you need to edit the packagereferences in packages.config and/or in your csproj file. When i tried to downgrade in vs2017 for Mac using the nuget package manager it didnt work, thats the reason i recommend downgrading manually.

All 19 comments

I think this is going to be more of a Xamarin tooling issue then an issue with Forms itself

Are you using Package Reference? One temporary fix I've seen recommended for this is to use package.config. Would it be possible to see if switching to package.config fixes the problem? I had a similar issue like this with the IoT libraries and that was their recommendation

Possibly related ticket
https://github.com/xamarin/xamarin-android/issues/1196

I'm having the same Could not load assembly 'System.Memory, Version=4.0.1.0'. Perhaps it doesn't exist in the Mono for Android profile? issue, though for me it appears to come from using the 2.1.0 (ie. updates for .NET Core 2.1) releases of Microsoft.Extensions.* packages (Microsoft.Extensions.Primitives being the only one I can see having a direct dependency on System.Memory and System.Runtime.CompilerServices.Unsafe). I'm also using PackageReference at the moment (yet to try reverting to package.config).

The final solution from Xamarin/Xamarin-android#1196 seems to be VS v15.7.0+ (and possibly Xamarin.Forms v3.0.0.446417), however I'm using VS v15.7.2 and updated all Xamarin packages to the latest available and still got the issue.

I'll try changing the projects to use package.config, but for me (for now) the workaround seems to be using packages prior to the releases for .NET Core 2.1.

Edit: shared project is an SDK project, so packages.config can't be used.

@Sebbs128 or @SpiegelSoft do you have a small repro? I updated one of my personal projects to the latest unsafe 4.5 and it's compiling/running fine for me on ios and android

I am getting this as well but only when deploying to device, running in the simulator works which doesn't make sense to me.

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Xamarin\iOS\Xamarin.iOS.Common.targets(791,3): error : Can't resolve the reference 'System.ReadOnlySpan`1<System.Char> System.MemoryExtensions::AsSpan(System.String,System.Int32,System.Int32)', referenced from the method 'System.ReadOnlySpan`1<System.Char> Microsoft.Extensions.Primitives.StringSegment::AsSpan()' in 'System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

Upgrade to the latest Entity Framework.

What I've discovered is that the iOS build breaks precisely when the System.Memory NuGet package is present.

@SpiegelSoft I encountered the same problem today and opened a new issue for Xamarin.iOS: iOS app build fails when referencing package (e.g. Entity Framework Core 2.1) that depends on newer System.Memory. The sample project that I included to reproduce the problem doesn't reference Xamarin.Forms, so I don't think this is a Xamarin.Forms issue specifically.

@PureWeen For a small repro, I created a new Xamarin.Forms Blank App in VS2017 (15.7.2) with .NET Standard shared project, then in the shared project installed Microsoft.Extensions.Primitives from nuget and added this class (the class probably isn't necessary, just ensures the assembly is included in the build)

using Microsoft.Extensions.Primitives;
using System;

namespace SystemMemoryPackageTest.Test
{
    public class DummyChangeToken : IChangeToken
    {
        public bool HasChanged => throw new NotImplementedException();

        public bool ActiveChangeCallbacks => throw new NotImplementedException();

        public IDisposable RegisterChangeCallback(Action<object> callback, object state)
        {
            throw new NotImplementedException();
        }
    }
}

I agree it's likely not a Xamarin.Forms issue specifically.

I had the same issue with an Android project and worked around it by changing the Android Link Assemblies settings from SdkOnly to None.

I'm going to close this one for now as it's already being tracked here
https://github.com/xamarin/xamarin-macios/issues/4168

Let me know if that seems incorrect and I'll re-open

Error MT2101: Can't resolve the reference 'System.ReadOnlySpan1<System.Char> System.MemoryExtensions::AsSpan(System.String,System.Int32,System.Int32)', referenced from the method 'System.ReadOnlySpan1 Microsoft.Extensions.Primitives.StringSegment::AsSpan()' in 'System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. (MT2101)

how can i solve this error?

I solved it by downgrading all Microsoft.extensions.* packages to Version 2.0.0 by hand. To do this you need to edit the packagereferences in packages.config and/or in your csproj file. When i tried to downgrade in vs2017 for Mac using the nuget package manager it didnt work, thats the reason i recommend downgrading manually.

thanks for quick reply i'll just try this solution. @leonvandebroek

I'm having the same problem as @jamiewest - works in simulator get build error below if I try to deploy to a real device for testing:

Can't resolve the reference 'System.ReadOnlySpan1<System.Char> System.MemoryExtensions::AsSpan(System.String,System.Int32,System.Int32)', referenced from the method 'System.ReadOnlySpan1 Microsoft.Extensions.Primitives.StringSegment::AsSpan()' in 'System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

@leonvandebroek Would you be willing to show a portion of csproj file edits you made to manually downgrade?

For me it also worked by downgrading Microsoft.Extensions.* (I only had Primitives).
In my project, the problem appeared by installing EntityFramework packages(version 2.1.0).

After I downgraded relevant packages, I can (re)build the solution and also deploy the app (emulator and device) in release mode (with SDK assemblies linking).
So, currently I have all Microsoft.Extensions.* packages on 2.0.0, all EntityFramework related packages on 2.0.0, System.Runtime.CompilerServices on 4.5.0.

NOTE: If you update System.Runtime.CompilerServices.Unsafe package, please do the following:

Go to C:\Users%user%.nuget\system.runtime.compilerservices.unsafe\vX.Y.Z, and delete “ref” folder then make a copy of “lib” folder and rename the copy back to “ref”.
Cleanup all the “bin” and “obj” folders in the projects.
Rebuild and run.

There is an issue with referencing the right .dll when using package reference.

@leonvandebroek Thanks. Your solution worked! Downgrade the Microsoft extension nuget packages to 2.0.0 solved the broken iOS device build. Working well while waiting for next update.

Hello, you all seem to know this very well.

I have the same issue and another too so thought I'd post here to start... please point me in the right direction if you can for the second error if possible...

As of this morning, I'm seeing these errors and cannot debug an app on iOS at all, in any simulator.

I have the latest Visual Studio 2017 installed and updates on Windows 10. Same with XCode 9.4.1 and the Simulator 10.0.0 on OSX 10.13.4 All relevant xamarin plugins are updated too.

Running and testing on Android is not an issue and neither was iOS until this AM.

I keep getting this error on building the iOS App:

_Severity Code Description Project File Line Suppression State
Error Can't resolve the reference 'System.ReadOnlySpan1 System.MemoryExtensions::AsSpan(System.String,System.Int32,System.Int32)', referenced from the method 'System.ReadOnlySpan1 Microsoft.Extensions.Primitives.StringSegment::AsSpan()' in 'System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. myapplication.iOS_

If I then select the iOS project and build that only, the build succeeds.

Then, when I try to deploy to the simulator (and I've tried many from iOS 10.3 to 11.1), I get the following error:

_This debug engine does not support exception conditions. The condition(s) will be ignored.
The app has been terminated.
Launch failed. The app 'myapplication.iOS' could not be launched on 'iPhone 7 Plus iOS 10.3'. Error: error HE0046: Failed to install the app 'com.mycompany.myapplication' on the device 'iOS 10.3 (14E8301) - iPhone 7 Plus': Failed to chmod /Users/lindsaymiles/Library/Developer/CoreSimulator/Devices/FD1D91C1-65FE-49D2-96AB-78B12816704D/data/Library/Caches/com.apple.containermanagerd/Bundle/Application/0F4BED82-C36B-4E39-9ED9-7479915AAFAF/myapplication.iOS.app/myapplication.iOS : No such file or directory. Please check the logs for more details. The app has been terminated_

Can someone please advise on this as I just cannot seem to find a way around the issue.

EDIT:
After yet another clean and reboot and restart of all tools, the first error remains but the second error above is replaced with this:

_Launch failed. The app 'myapplication.iOS' could not be launched on 'iPhone 8 iOS 11.1'. Error: error MT1008: Failed to launch the simulator: Could not find file "/Users/lindsaymiles/Library/Caches/Xamarin/mtbs/builds/myapplication.iOS/2b8bb42efaa82e056d0923743e8a2829/bin/iPhoneSimulator/Debug/myapplication.iOS.app/myapplication.iOS". Please check the logs for more details. The app has been terminated._

Again, any help on this last error would be great!

SECOND EDIT:
After all that it was simply the Microsoft.Extensions.
All are now downgraded to 2.0.0 and iOS app seems to be behaving albeit slow...
MS ought to get their act together and fix... just don't hold one's breath on that :(

Was this page helpful?
0 / 5 - 0 ratings