Android Error With SignalR Core: Could not load assembly 'System.Memory' during startup registration

M_AllanM_Allan USMember ✭✭
edited December 2017 in Xamarin.Forms

Hi,
I am trying to use SignalR Core preview in a Xamarin Forms solution. UWP works fine but Android crashes with error : "Could not load assembly 'System.Memory' during startup registration".

I tried all of these:

  • Added Microsoft.AspNetCore.SignalR.Client and it's all dependencies including the preview "System.Memory"
  • Cleared nuget cache and nuget installation .nuget folder
  • Cloned SignalR repository, added the projects to my Xamarin forms project, referenced the SignalR projects
  • Cleaned obj and bin and tried new solutions from scratch, tried both shared project and .net standard library
  • Updated android build tools
  • Deployed a published apk to the device

But all that failed with the same error

I am using

  • Latest xamarin forms and VS 2017 community
  • Andoird compile version 8.0 Oreo - Min Version 6, and Target is same to comile (8.0 Oreo)
  • Test device Samsung Android SDK 6

To reproduce it just create xamarin forms empty solution, reference Microsoft.AspNetCore.SignalR.Client then run the android.

Help please!

Answers

  • Same issue here. I'm unable to deploy to my device. (Xperia Z1 - Android 5.1.1).

    First it throws a warning:
    Ignoring C:\Users\Bruno.Menezes.nuget\packages\system.memory\4.4.0-preview2-25405-01\ref\netstandard2.0 as it is a Reference Assembly

    Then at output window:
    Assembly Loader probing location: 'System.Memory'.
    Could not load assembly 'System.Memory' during startup registration.

    And the app crashes.

  • M_AllanM_Allan USMember ✭✭

    I think Monodroid needs to update, seems that SignalR core is really "core" not "standard"! System.Memory and System.Buffers should be part of the runtime which is .net core runtime not mono. SignalR core is written for .net core not mono! I added System.Memory to the package manually, the error then went to System.Buffers, adding it manually is failing =( .. lot's of mess

  • I dont think so.
    Just tried a iOS deployment here, no problem at all. At least for me, this issue occurs only with the Android version.

    Maybe it's something with the Xamarin.Forms version? Which one are you using?

  • M_AllanM_Allan USMember ✭✭

    I still think so. I didn't try iOS, my problem is Anroid and the errors and the way the assembly is skipped make me think that those dlls need to be part of the runtime and androind's mono doesn't have them. as for Xamarin forms I am using latest.

  • NikolayDemirevNikolayDemirev USMember ✭✭

    I have the same issue only with Xamarin.Android. If anyone has solved that issue, please help.

  • NikolayDemirevNikolayDemirev USMember ✭✭

    I was able to get my app running but the fix includes a little hack. Here is my workaround:

    1. Instal System.Memory NuGet version 4.4.0-preview2-25405-01
    2. Build your Xamarin.Android project and copy the C:\Users{your_user}.nuget\packages\system.runtime.compilerservices.unsafe\4.4.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll to {MyApp}.Droid\obj\Debug\android\assets\
    3. Start your Android app

    Sadly, I can not find how to skip step 2. If anyone can help me with that, I will greatly appreciate the help.

  • M_AllanM_Allan USMember ✭✭
    edited December 2017

    It is good solution Nikolay,

    I tried to install "System.Memory" preview2 in all possible ways but no difference

    What worked for me is putting "System.Memory" and "System.Runtime.CompilerServices.Unsafe" in "\obj\Debug\android\assets\"

    For now we can just do that copy in a pre-build event

  • ArtemTomilinArtemTomilin USMember

    Thanks, M_Allan. Your solution helped me.

  • MohamedBenSaidMohamedBenSaid USMember ✭✭

    Thanks, M_Allan. but your solution not work in Release mode.

  • nefgrixisnefgrixis Member ✭✭

    @MohamedBenSaid I think if you put it in the release folder it would work. I haven't tried it yet but I think it's worth a shot

  • W2KW2K Member
    It also works for me in debug mode but does not work in Release. Even though I copy the files to release folder it still misses System.Memory. Any tip on how to resolve it ?
  • NikolayDemirevNikolayDemirev USMember ✭✭
    edited April 2018

    Sorry for the late response, for some reason I didn't receive any notification on the thread.

    I have copied the System.Memory nuget folder and I have replaced the dlls in the "portable" folder with the correct ones. You can make the difference between the "real" and the "fake" dll by the size of the file. The "real" is slightly bigger. I have created a build task which is executed before each of my builds, which copies the modified nuget folder to the %UserProfile%.nuget\packages folder.

    PS: This is one of the ugliest hacks I have ever seen, but it works for me :blush:

  • NikolayDemirevNikolayDemirev USMember ✭✭

    Hello again :smile:.

    I have not worked on my project for a month. And now I have updated the VisualStudio, the Xamarin Studio and everything that can be updated except the nugets I have used. Now I deeply regret doing the updates... SignalR started bugging me again with build errors. Could someone help me? Here is the error I see this time:

    Can't resolve the reference 'System.Span`1<!!0> System.SpanExtensions::AsSpan(!!0[])', referenced from the method 'System.Void Microsoft.AspNetCore.SignalR.Internal.Formatters.BinaryMessageFormatter::WriteMessage(System.ReadOnlySpan`1<System.Byte>,System.IO.Stream)' in 'System.Memory, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
    

    Please note that the linker is set to Link Framework SDKs Only.

  • bytefishbytefish Member

    I have updated my project to Xamarin.Forms 3.4.0.1008975 and also have the following problems when running the App:

    • Could not load assembly 'System.Memory' during startup registration
    • Could not load assembly 'System.Runtime.CompilerServices' during startup registration
    • Could not load assembly 'System.Buffers' during startup registration

    Last year, when I wrote the app, it sometimes helped to explicitly reference the NuGet packages for these dependencies. But this time it didn't work anymore. So I have now copied the DLLs from the NuGet Folders, put them in a directory I named Libs and copy them directly into the bin and android/assets directory using a Pre-Build Event:

    xcopy "$(SolutionDir)..\Libs\*.*" "$(ProjectDir)\bin\$(ConfigurationName)" /Y /I /E
    xcopy "$(SolutionDir)..\Libs\*.*" "$(ProjectDir)\obj\$(ConfigurationName)\android\assets" /Y /I /E
    

    Now I can run the project in Debug Mode at least.

  • bytefishbytefish Member

    While the above works in Debug builds, it leads to errors when Linking. I switched the whole Android project back to the packages.config NuGet References. Now the HintPath is set correctly and Xamarin Forms is able to load the mentioned DLLs correctly.

Sign In or Register to comment.