Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Runtime.CompilerServices.Unsafe linked away in release leading to crashes #1196

Closed
ddobrev opened this issue Jan 16, 2018 · 60 comments
Closed
Assignees

Comments

@ddobrev
Copy link

@ddobrev ddobrev commented Jan 16, 2018

This is most probably related to dotnet/efcore#8922 . None of the workarounds there help. The major problem is that it's simply impossible to force the linker to preserve an entire assembly.

Steps to Reproduce

  1. Use a library, such as Microsoft.Extensions.Identity.Stores, which (indirectly) depends on System.Runtime.CompilerServices.Unsafe, in a Xamarin.Android project;
  2. Build the project in Release - ensure linking is not set to "None";
  3. Run it on a device.

Expected Behavior

The application runs.

Actual Behavior

The application does not load at all with:
Could not load assembly 'System.Runtime.CompilerServices.Unsafe' during startup registration.

Version Information

Microsoft Visual Studio Community 2017
Version 15.5.3
VisualStudio.15.Release/15.5.3+27130.2020
Microsoft .NET Framework
Version 4.7.02558

Installed Version: Community

Visual Basic 2017 00369-60000-00001-AA416
Microsoft Visual Basic 2017

Visual C# 2017 00369-60000-00001-AA416
Microsoft Visual C# 2017

Visual C++ 2017 00369-60000-00001-AA416
Microsoft Visual C++ 2017

Visual F# 4.1 00369-60000-00001-AA416
Microsoft Visual F# 4.1

Application Insights Tools for Visual Studio Package 8.10.01106.1
Application Insights Tools for Visual Studio

ASP.NET and Web Tools 2017 15.0.31127.0
ASP.NET and Web Tools 2017

ASP.NET Core Razor Language Services 1.0
Provides languages services for ASP.NET Core Razor.

ASP.NET Web Frameworks and Tools 2017 5.2.51007.0
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0 15.0.31106.0
Azure App Service Tools v3.0.0

Common Azure Tools 1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

EZ-GIT 1.7.2

GradleBindings.VisualStudio 1.0
Creates Xamarin Android Binding projects from a gradle file.

JavaScript Language Service 2.0
JavaScript Language Service

Merq 1.1.17-rc (cba4571)
Command Bus, Event Stream and Async Manager for Visual Studio extensions.

Microsoft Azure Tools 2.9
Microsoft Azure Tools for Microsoft Visual Studio 2017 - v2.9.51120.3

Microsoft Continuous Delivery Tools for Visual Studio 0.3
Simplifying the configuration of continuous build integration and continuous build delivery from within the Visual Studio IDE.

Microsoft JVM Debugger 1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft MI-Based Debugger 1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards 1.0
Microsoft Visual C++ Wizards

Microsoft Visual Studio Tools for Containers 1.1
Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Microsoft Visual Studio VC Package 1.0
Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio 4.8.4-pre (3fe64e3)
Support for debugging Mono processes with Visual Studio.

NpgsqlVSPackage Extension 1.0
NpgsqlVSPackage Visual Studio Extension Detailed Info

NuGet Package Manager 4.5.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

Qt Visual Studio Tools 2.1.2
The Qt Visual Studio Tools allow developers to use the standard development environment without having to worry about any Qt-related build steps or tools.

SQL Server Data Tools 15.1.61710.120
Microsoft SQL Server Data Tools

Visual Studio Code Debug Adapter Host Package 1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Tools for CMake 1.0
Visual Studio Tools for CMake

VisualStudio.Mac 1.0
Mac Extension for Visual Studio

Xamarin 4.8.0.757 (7f9ec2a)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 4.8.188 (c5813fa34)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin.Android SDK 8.1.3.0 (HEAD/ef47226b7)
Xamarin.Android Reference Assemblies and MSBuild support.

Xamarin.iOS and Xamarin.Mac SDK 11.6.1.2 (6857dfc)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

@jonpryor

This comment has been minimized.

Copy link
Member

@jonpryor jonpryor commented Jan 16, 2018

it's simply impossible to force the linker to preserve an entire assembly

It should be possible to preserve an entire assembly, either via the $(AndroidLinkSkip) MSBuild property or via the @(LinkDescription) Build action and a linker configuration file.

@ddobrev

This comment has been minimized.

Copy link
Author

@ddobrev ddobrev commented Jan 16, 2018

Either does not work. I think the problem might have to do with System.Runtime.CompilerServices.Unsafe being considered a "reference-only" assembly.

@radekdoulik

This comment has been minimized.

Copy link
Contributor

@radekdoulik radekdoulik commented Jan 16, 2018

Not sure what "reference-only" assembly mean here. Could you please attach a project which reproduces the issue?

Are you sure the AndroidLinkSkip is indeed set? Do you have diagnostic build output?

@jonpryor

This comment has been minimized.

Copy link
Member

@jonpryor jonpryor commented Jan 16, 2018

@radekdoulik: "Reference only assembly" means Issue #1154. It is an assembly with the System.Runtime.CompilerServices.ReferenceAssemblyAttribute assembly-level custom attribute.

Mono will not load reference assemblies.

@ddobrev

This comment has been minimized.

Copy link
Author

@ddobrev ddobrev commented Jan 16, 2018

@ddobrev

This comment has been minimized.

Copy link
Author

@ddobrev ddobrev commented Jan 16, 2018

In case it doesn't work, try a Xamarin.Forms application in which the .NET Standard has the reference to Stores.

@radekdoulik

This comment has been minimized.

Copy link
Contributor

@radekdoulik radekdoulik commented Jan 16, 2018

Wasn't able to reproduce it with XA 8.1.1.1. I will try 8.1.5 next.

BTW, I don't see AndroidLinkSkip property set in the project file.

@ddobrev

This comment has been minimized.

Copy link
Author

@ddobrev ddobrev commented Jan 16, 2018

@radekdoulik try "AndroidLinkSkip" yourself, I have little intention of reapplying once more all the workarounds which failed to work. Also, see the issue I have linked at the beginning.

@radekdoulik

This comment has been minimized.

Copy link
Contributor

@radekdoulik radekdoulik commented Jan 16, 2018

Works OK for me in XA 8.1.5 as well.

@ddobrev

This comment has been minimized.

Copy link
Author

@ddobrev ddobrev commented Jan 16, 2018

Try Xamarin.Forms.

@radekdoulik

This comment has been minimized.

Copy link
Contributor

@radekdoulik radekdoulik commented Jan 16, 2018

OK, I have got a link to a simple app from @JonDouglas which reproduces the issue - https://github.com/aspnet/EntityFrameworkCore/files/1372571/Desktop.zip (App2). Looks like it is indeed related to #1154

@JonDouglas

This comment has been minimized.

Copy link
Contributor

@JonDouglas JonDouglas commented Jan 16, 2018

A simple reproduction is the following:

  1. Create a File->New Xamarin.Android Project
  2. Include Microsoft.EntityFrameworkCore.Sqlite as a <PackageReference> from NuGet.

Here are some relevant logs similar to those in #1154

2>BuildApk Task
2>  ApkInputPath: obj\Debug\android\bin\packaged_resources
    ...
2>    obj\Debug\android\assets\System.Runtime.CompilerServices.Unsafe.dll
2>BuildApk Task
    ...
2>Task Parameter:
2>    EmbeddedNativeLibraryAssemblies=
   ...
2>                Version=
2>        C:\Users\dougl\.nuget\packages\system.runtime.compilerservices.unsafe\4.4.0\ref
2>etstandard2.0\System.Runtime.CompilerServices.Unsafe.dll
2>                CopyLocal=false
2>                FusionName=System.Runtime.CompilerServices.Unsafe, Version=4.0.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
2>                ImageRuntime=v4.0.30319
2>                NuGetIsFrameworkReference=false
2>                NuGetPackageId=System.Runtime.CompilerServices.Unsafe
2>                NuGetPackageVersion=4.4.0
2>                NuGetSourceType=Package
2>                OriginalItemSpec=C:\Users\dougl\.nuget\packages\system.runtime.compilerservices.unsafe\4.4.0\ref
2>etstandard2.0\System.Runtime.CompilerServices.Unsafe.dll
2>                Private=false
2>                ReferenceAssembly=C:\Users\dougl\.nuget\packages\system.runtime.compilerservices.unsafe\4.4.0\ref
2>etstandard2.0\System.Runtime.CompilerServices.Unsafe.dll
2>                ReferenceSourceTarget=ResolveAssemblyReference
2>                ResolvedFrom={RawFileName}
1>Task "LinkAssemblies" (TaskId:327)
1>  Task Parameter:UseSharedRuntime=True (TaskId:327)
1>  Task Parameter:MainAssembly=obj\Debug\android\assets\EntityFrameworkFailure.dll (TaskId:327)
1>  Task Parameter:OutputDirectory=obj\Debug\linkdst\ (TaskId:327)
1>  Task Parameter:OptionalDestinationDirectory=obj\Debug\android\assets\ (TaskId:327)
1>  Task Parameter:LinkMode=None (TaskId:327)
1>  Task Parameter:LinkOnlyNewerThan=obj\Debug\link.flag (TaskId:327)
1>  Task Parameter:
1>      ResolvedAssemblies=
    ...
1>          C:\Users\dougl\.nuget\packages\system.runtime.compilerservices.unsafe\4.4.0\ref\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll
@ddobrev

This comment has been minimized.

Copy link
Author

@ddobrev ddobrev commented Jan 16, 2018

@radekdoulik until the fix is released, is there a reliable workaround?

@dazinator

This comment has been minimized.

Copy link

@dazinator dazinator commented Jan 16, 2018

I have just hit this after converting my csproj from using packages.config to using packagereference. Prior to that my app was working but I had to use linker mode "SdkOnly". Now after converting to package reference, system.runtime.compilerservices.unsafe gets linked away.

@radekdoulik

This comment has been minimized.

Copy link
Contributor

@radekdoulik radekdoulik commented Jan 17, 2018

@ddobrev I don't know about any workaround at this point. At least not from linker perspective as the assembly is not removed by the linker. It is present in the apk, but it is not loaded because it has System.Runtime.CompilerServices.ReferenceAssemblyAttribute.

As can be seen from logcat output:

D/Mono    (27963): Image addref System.Runtime.CompilerServices.Unsafe[0xadddc760] -> System.Runtime.CompilerServices.Unsafe.dll[0xb01af600]: 2
D/Mono    (27963): Image for assembly 'System.Runtime.CompilerServices.Unsafe' (System.Runtime.CompilerServices.Unsafe.dll) has ReferenceAssemblyAttribute, skipping

In case you are using <PackageReference> in your csproj file, you might try to use package.config file instead as a temporary solution?

@ddobrev

This comment has been minimized.

Copy link
Author

@ddobrev ddobrev commented Jan 17, 2018

@radekdoulik I see. Could you please at least verify in your reproduction that using package.config instead does work?

@radekdoulik

This comment has been minimized.

Copy link
Contributor

@radekdoulik radekdoulik commented Jan 17, 2018

@ddobrev OK, tried it with the App2 repro case and it works. It looks like it might work for you too.

@ddobrev

This comment has been minimized.

Copy link
Author

@ddobrev ddobrev commented Jan 17, 2018

@radekdoulik thank you. If you could just upload the project here as a future reference, I think it would be of immense help to everyone.

@radekdoulik

This comment has been minimized.

Copy link
Contributor

@radekdoulik radekdoulik commented Jan 17, 2018

App2.zip

This is an updated app from #1196 (comment). It uses packages.config file for nuget packages instead of <PackageReference> element in the App2.csproj file.

The updated app runs OK, so using packages.config might be temporary workaround before the #1154 is fixed.

@JORGEGO

This comment has been minimized.

Copy link

@JORGEGO JORGEGO commented Jan 18, 2018

Any new workaround... after migrate from legay Xamarin.forms to .NET standard 2.0 same issue when start debug it stop...
Today has delivery 15.5.4 visual studio 2017 and persist the same error...

@JonDouglas

This comment has been minimized.

Copy link
Contributor

@JonDouglas JonDouglas commented Jan 18, 2018

@JORGEGO 15.5.4 had no fixes for Xamarin.Android. We do not have a direct fix for this issue yet as the only known workaround is to use packages.config instead of <PackageReference> when referencing from NuGet. You can change this setting in your NuGet options in Visual Studio. We are still investigating and will be providing any other known workarounds prior to a release.

@JORGEGO

This comment has been minimized.

Copy link

@JORGEGO JORGEGO commented Jan 18, 2018

Let me ask it is possible use package.config for .NET standard 2.0 projects?

@ddobrev

This comment has been minimized.

Copy link
Author

@ddobrev ddobrev commented Jan 18, 2018

@JORGEGO yes but it requires a little manual work. You need to remove all package references, ensure there are no "RestoreProjectStyle" properties in your csproj and readd your packages.

@ddobrev

This comment has been minimized.

Copy link
Author

@ddobrev ddobrev commented Jan 18, 2018

@radekdoulik this one finally works. Thank you very much.

@smitter86

This comment has been minimized.

Copy link

@smitter86 smitter86 commented Jan 23, 2018

I've resolved it by following:

  1. Re-add nuget packages in Mobile.Android project with using packages.config;
  2. Add nuget package System.Runtime.CompilerServices.Unsafe to Android project (it should be located in ../packages folder in your root solution folder);
  3. Removed lib System.Runtime.CompilerServices.Unsafe from C:\Users%User%.nuget\packages\netstandard.library\2.0.0;
  4. Add reference to this lib manualy, using project references.
@ddobrev

This comment has been minimized.

Copy link
Author

@ddobrev ddobrev commented Jan 23, 2018

@smitter86 I don't know, just 1. works for me.

@wyldebill

This comment has been minimized.

Copy link

@wyldebill wyldebill commented Mar 13, 2018

I tried a file new project with Xamarin Forms and EF 2.0.1 in VS2017 v15.6.1 to see where migrations is at with SQLite. This combination is a no-go right now with the assembly not being loaded at runtime.

@brian8227

This comment has been minimized.

Copy link

@brian8227 brian8227 commented Mar 13, 2018

Having the same problem as well. Any news of a fix?

@radekdoulik

This comment has been minimized.

Copy link
Contributor

@radekdoulik radekdoulik commented Mar 13, 2018

It should be fixed soon by #1356

@filoe

This comment has been minimized.

Copy link

@filoe filoe commented Mar 24, 2018

Already fixed now?

@EhRom

This comment has been minimized.

Copy link

@EhRom EhRom commented Mar 29, 2018

I have the same problem with VS 2017 15.6.4. @smitter86, how to "re-add nuget packages in Mobile.Android project with using packages.config" ? I did not found enough clear instructions to do this. I'd like to make this only for the Android project.

@dyardyGIT

This comment has been minimized.

Copy link

@dyardyGIT dyardyGIT commented Apr 7, 2018

Solution?

@EhRom

This comment has been minimized.

Copy link

@EhRom EhRom commented Apr 7, 2018

Yes. First you have to delete all the packages in your project. Then re-add the first pacakge. A prompt should appear: choose the package.config method. Re-add the other packages. It should work.

@dyardyGIT

This comment has been minimized.

Copy link

@dyardyGIT dyardyGIT commented Apr 7, 2018

This is a netstandard 2 project (xamarin.forms). I have tried a number of ways to get it to revert to packages.config approach. Including this approach as shown on SO https://stackoverflow.com/questions/45962998/switch-back-to-packages-config

So, at this moment wanting to use ef.core sqllite with xamarin forms .netstandard2 project and struggling to get it back to packages.config approach.

@tipa

This comment has been minimized.

Copy link

@tipa tipa commented Apr 7, 2018

@EhRom that is a workaround, not a solution

@dyardyGIT

This comment has been minimized.

Copy link

@dyardyGIT dyardyGIT commented Apr 7, 2018

Gosh, I have tried a number of the variations above, modifying nupkg, removing lib's, refs, dlls, reinstalled etc. Still no solution to this error

04-07 15:46:50.825 D/Mono (29229): Assembly Loader probing location: 'System.Runtime.CompilerServices.Unsafe'.
04-07 15:46:50.825 F/monodroid-assembly(29229): Could not load assembly 'System.Runtime.CompilerServices.Unsafe' during startup registration.
04-07 15:46:50.825 F/monodroid-assembly(29229): This might be due to an invalid debug installation.
04-07 15:46:50.825 F/monodroid-assembly(29229): A common cause is to 'adb install' the app directly instead of doing from the IDE.

@denmarksdev

This comment has been minimized.

Copy link

@denmarksdev denmarksdev commented Apr 7, 2018

I follow these steps of hongliyu2002 https://forums.realm.io/t/could-not-load-assembly-system-runtime-compilerservices-unsafe-during-startup-registration/974/4

1. Go to C:\Users%user%.nuget\system.runtime.compilerservices.unsafe\4.4.0, and delete "ref" folder then make a copy of "lib" folder and rename the copy back to "ref".
2. Cleanup all the "bin" and "obj" folders in the projects.
3. Rebuild and run..

This simple workaround , works for me :)
My current version is Xamarin.Forms 2.5.1.444934

@AhmedShamel

This comment has been minimized.

Copy link

@AhmedShamel AhmedShamel commented Apr 9, 2018

@denmarksdev
Thanks man, your solution worked for me.

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Apr 9, 2018

Can please give anybody a hint if that does help although with the problem that the Command does not fire?

xamarin/Xamarin.Forms#1599

We have no crash or exception anywhere...

@jonpryor

This comment has been minimized.

Copy link
Member

@jonpryor jonpryor commented Apr 20, 2018

I suspect that this issue is in part duplicative of #1154 and #1162 (plus many followup commits), which revolve around handling of reference assemblies. It would be interesting to re-try this against e.g. Visual Studio 15.7 Preview 4.

@dgerding

This comment has been minimized.

Copy link

@dgerding dgerding commented May 7, 2018

I'm getting the problem on release version of VS and .net Core. Trying different workarounds. Anyone know if it's still a problem in 15.7 Preview 4? (Maybe that will ship this week at Build?)

@SalimiHabib

This comment has been minimized.

Copy link

@SalimiHabib SalimiHabib commented May 7, 2018

it is also appear after Realm installation
may be related to ABI settings , for Debug builds on armeabi (it notified in Realm documentation)
but i still have this problem unsolved .

@dominik-weber

This comment has been minimized.

Copy link

@dominik-weber dominik-weber commented May 7, 2018

@jonpryor I have just tested this with VS 15.7.0, seems to be fixed! 😊

@cheles

This comment has been minimized.

Copy link

@cheles cheles commented May 8, 2018

I can confirm that latest vs 15.7 with XF 3.0.0.446417 fixed this issue.

@JonDouglas

This comment has been minimized.

Copy link
Contributor

@JonDouglas JonDouglas commented May 17, 2018

Based on two confirmations and also confirming it myself on a local machine using 15.7, I am going to close this issue based on #1154 #1356 and #1162 being available in 15.7 builds.

Please feel free to comment on this issue if you are still running into this issue.

@JonDouglas JonDouglas closed this May 17, 2018
@wjvii

This comment has been minimized.

Copy link

@wjvii wjvii commented May 18, 2018

We are running Xamarin Forms 3.0 and 3.1 and Visual Studio 2017 15.7.1 and when we try to run the apk we create in VS or App Center we still get "Could not load assembly 'System.IO.Packaging' during startup registration."

@alexdrl

This comment has been minimized.

Copy link

@alexdrl alexdrl commented Jun 13, 2018

I am getting this error with a fresh copy of https://github.com/cwrea/XamarinTodo
Using VS 15.7.3.

@cwrea

This comment has been minimized.

Copy link

@cwrea cwrea commented Jun 13, 2018

@alexdrl Odd -- I am not encountering the issue. I am able to compile and run the Android app in the latest version of the sample (https://github.com/cwrea/XamarinTodo/tree/NETStd20_EFCore203 and corresponding to master at the moment), and I'm also using Visual Studio 15.7.3, with Xamarin.Android SDK 8.3.3.2 (HEAD/dffc59120).

When I created that version, I did specifically remove the packages.config workaround as it was no longer necessary for Visual Studio 15.7.x. I tested initially on 15.7.1 or 15.7.2, and today I'm still seeing it work with 15.7.3.

Did you upgrade any packages or make any other changes, or ran the build from unmodified source?

@alexdrl

This comment has been minimized.

Copy link

@alexdrl alexdrl commented Jun 13, 2018

@cwrea The only thing that I do is to extract the zip downloaded from GitHub, and build+run the Project in VS.
I ran the Project in an Android 4.4 virtual phone, if that matters.

I confirm that the versions of VS and Xamarin.Android SDK are the same that you mention

Doing this solves the problem #1196 (comment)

@cwrea

This comment has been minimized.

Copy link

@cwrea cwrea commented Jun 13, 2018

@alexdrl In that case, I suggest you open a new issue. Attach a copy of the zip file to your issue, your machine config, your build logs, etc. as described in the new issue template.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
You can’t perform that action at this time.