Realm-dotnet: System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.

Created on 10 Jan 2017  路  23Comments  路  Source: realm/realm-dotnet

Just attempted to load my project in Visual Studio after developing solely in Xamarin Studio for the entire project duration.

I'm getting this error: System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.

The error occurs during this call.
_realm = Realm.GetInstance("salt.realm");

The project type is Xamarin.Android with a PCL project and uses the latest version of Realm (0.81.0).
I've tried all the different linking options to no avail.

All my realm objects contain the attribute:
[Preserve(AllMembers = true)]

I'm also using Nuget v3 with the project.json file. Not sure if that makes a difference.

T-Bug-Crash

All 23 comments

Hey @UKDeveloper99, could you please delve into the System.Reflection.ReflectionTypeLoadExceptionin the exception helper to see what the exact types that fail are?

Also, do you use the new backlinks feature? Try commenting it out if so.

Hey @fealebenpae I'm not using the backlink feature. I can't extract the full exception log currently, but will post it later this evening as soon as I'm home.

Cheers.

As an (unhappy) data point, I have seen that error intermittently in the last few weeks but in iOS projects. That includes right now, in a project that worked an hour ago. I'll try to work out more things to check but yes, if you can get into a stack trace or more details on the inner exception that would help.

Does this happen when deploying on device?

In my case, on simulator, and it works on device.

TypeLoadException building Draw with latest 0.82.0 nuget 2071-01-10

stack trace

Systen.Reflection.Assembly.GetTypes
Realms.Schema.RealmSchema.<BuilDefaultSchema>
...
Realms.Schema.RealmSchema.CreateSchemaForClasses
Realms.Schema.RealmSchema.BuildDefaultSchema

inner exception

System.IO.FileNotFoundException: Could not load file or assembly or one of is dependencies.
File name: 'System.Threading.Tasks, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f11d50a3a

Could not load file or assembly or one of its dependencies.

Oddly enough it has disappeared, with zero changes since the last time I received the error (aside from a machine restart). It was while deploying to an Android device (api 23). Unfortunately I didn't have a chance to test it in simulator. Sorry I couldn't be of further assistance.

  1. Checked out Draw on a different Mac
  2. Copied our latest 0.82.0 nugets to my local store
  3. Built and ran for simulator iPhone SE with OS 10.2
  4. Try logging in - get same error

Note this is the Draw app which was working not a new app which may have missing settings.

@AndyDentFree I'm giving this over to you since you already have a repro.

Makes no difference with the latest stable OR beta (Preview of Cycle 9) but I think that's at least because the nugets haven't been rebuilt. As the problem has gone away for the original user, will leave this as _current_ but wait until I have more rebuilds to try reproducing.

Just realised the only apps I have tested it with that exhibit this are trying to do sync so will try to repro with local Realm only in a new app.

This has disappeared for me as well using our builds as of release 0.82.0

I'm getting this again whilst testing 1.0.3 for release, with both my Stable and Beta channel Xamarin Studios, on different Macs.

  • Create a simple iOS Single View App
  • add Realm from NuGet
  • change ViewController.cs to the following
using System;
using Realms;
using UIKit;

namespace Try103LinkDevice
{

    class Fred : RealmObject
    {
        public string Name { get; set; }
    }

    public partial class ViewController : UIViewController
    {
        protected ViewController(IntPtr handle) : base(handle)
        {
            // Note: this .ctor should not contain any initialization logic.
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            var realm = Realm.GetInstance();
            realm.Write(() =>
            {
                realm.Add(new Fred { Name = "Bloggs" });
                realm.Add(new Fred { Name = "Sally" });
            });                        
        }
    }
}
System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
  at at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (System.Reflection.Assembly,bool)
  at System.Reflection.Assembly.GetTypes () [0x00000] in /Users/builder/data/lanes/3985/4e311372/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/Assembly.cs:406
  at Realms.Schema.RealmSchema.<BuildDefaultSchema>m__2 (System.Reflection.Assembly a) [0x00000] in <20208f704a7341f08ec3ecdfad52e94f>:0
  at System.Linq.Enumerable+<SelectManyIterator>c__Iterator2`2[TSource,TResult].MoveNext () [0x00059] in /Users/builder/data/lanes/3985/4e311372/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/System.Core/System/Linq/Enumerable.cs:423
  at System.Linq.Enumerable+WhereEnumerableIterator`1[TSource].MoveNext () [0x00062] in /Users/builder/data/lanes/3985/4e311372/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/System.Core/System/Linq/Enumerable.cs:147
  at Realms.Schema.RealmSchema.CreateSchemaForClasses (System.Collections.Generic.IEnumerable`1[T] classes) [0x00025] in <20208f704a7341f08ec3ecdfad52e94f>:0
  at Realms.Schema.RealmSchema.BuildDefaultSchema () [0x00093] in <20208f704a7341f08ec3ecdfad52e94f>:0
  at System.Lazy`1[T].CreateValue () [0x00075] in /Users/builder/data/lanes/3985/4e311372/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/Lazy.cs:437

Still getting them with latest 1.0.3 built in CPH 12:35pm 13/2

System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
  at at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (System.Reflection.Assembly,bool)
  at System.Reflection.Assembly.GetTypes () [0x00000] in /Users/builder/data/lanes/3969/7beaef43/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/Assembly.cs:405
  at Realms.Schema.RealmSchema.<BuildDefaultSchema>m__2 (System.Reflection.Assembly a) [0x00000] in <20208f704a7341f08ec3ecdfad52e94f>:0
  at System.Linq.Enumerable+<SelectManyIterator>c__Iterator2`2[TSource,TResult].MoveNext () [0x00059] in /Users/builder/data/lanes/3969/7beaef43/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/System.Core/System/Linq/Enumerable.cs:423
  at System.Linq.Enumerable+WhereEnumerableIterator`1[TSource].MoveNext () [0x00062] in /Users/builder/data/lanes/3969/7beaef43/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/System.Core/System/Linq/Enumerable.cs:147
  at Realms.Schema.RealmSchema.CreateSchemaForClasses (System.Collections.Generic.IEnumerable`1[T] classes) [0x00025] in <20208f704a7341f08ec3ecdfad52e94f>:0
  at Realms.Schema.RealmSchema.BuildDefaultSchema () [0x00093] in <20208f704a7341f08ec3ecdfad52e94f>:0
  at System.Lazy`1[T].CreateValue () [0x00075] in /Users/builder/data/lanes/3969/7beaef43/source/xamarin-macios/_ios-build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/referencesource/mscorlib/system/Lazy.cs:437

Building the current RealmDrawLite from realm-dotnet-private and running on iOS 10.2 on a simulated iPhone SE

Apart from the fixes being applied by the latest 1.0.3, I was being caught by a very nasty gotcha - If if you have a local nuget source pointing at a directory, it appears to do a depth-first search of any folders in there. Thus, an old version of nugets was being silently retrieved.

The moral is, be very careful you only have one copy of things in a subtree.

I'm still getting this problem, I've done a complete wipe of the packages folder and bin + obj folders. I'm using v1.0.4 with System.Reflection v4.3.0. I'm Deploying to a Android simulator (API 23) with multi-dex enabled.

[MonoDroid] System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
[MonoDroid]   at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (System.Reflection.Assembly,bool)
[MonoDroid]   at System.Reflection.Assembly.GetTypes () [0x00000] in /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/corlib/System.Reflection/Assembly.cs:406 
[MonoDroid]   at Realms.Schema.RealmSchema.<BuildDefaultSchema>m__4 (System.Reflection.Assembly a) [0x00000] in <9ff1ff623e524361bfdafbfff272346c>:0 
[MonoDroid]   at System.Linq.Enumerable+<SelectManyIterator>c__Iterator2`2[TSource,TResult].MoveNext () [0x00059] in /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/System.Core/System/Linq/Enumerable.cs:423 
[MonoDroid]   at System.Linq.Enumerable+WhereEnumerableIterator`1[TSource].MoveNext () [0x00062] in /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/System.Core/System/Linq/Enumerable.cs:147 
[MonoDroid]   at Realms.Schema.RealmSchema.CreateSchemaForClasses (System.Collections.Generic.IEnumerable`1[T] classes) [0x00025] in <9ff1ff623e524361bfdafbfff272346c>:0 
[MonoDroid]   at Realms.Schema.RealmSchema.BuildDefaultSchema () [0x000d7] in <9ff1ff623e524361bfdafbfff272346c>:0 
[MonoDroid]   at System.Lazy`1[T].CreateValue () [0x00075] in /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/mscorlib/system/Lazy.cs:437 
[MonoDroid] --- End of stack trace from previous location where exception was thrown ---
[MonoDroid]   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
[MonoDroid]   at System.Lazy`1[T].get_Value () [0x00032] in /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/mscorlib/system/Lazy.cs:330 
[MonoDroid]   at Realms.Schema.RealmSchema.get_Default () [0x00000] in <9ff1ff623e524361bfdafbfff272346c>:0 
[MonoDroid]   at Realms.Realm.GetInstance (Realms.RealmConfigurationBase config, Realms.Schema.RealmSchema schema) [0x00034] in <9ff1ff623e524361bfdafbfff272346c>:0 
[MonoDroid]   at Realms.Realm.GetInstance (Realms.RealmConfigurationBase config) [0x0000d] in <9ff1ff623e524361bfdafbfff272346c>:0 
[MonoDroid]   at Yoke.Helpers.RealmHelper.GetInstance () [0x0002d] in /Users/h/Yoke/Xamarin/Yoke/Helpers/RealmHelper.cs:14 
[MonoDroid]   at Yoke.Controllers.DataAccessController..ctor () [0x00007] in /Users/h/Yoke/Xamarin/Yoke/Controllers/DataAccessController.cs:15 
[MonoDroid]   at Yoke.App..ctor () [0x0001a] in /Users/h/Yoke/Xamarin/Yoke/App.xaml.cs:46 
[MonoDroid]   at MainPage.Droid.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x00033] in /Users/h/Yoke/Xamarin/Droid/MainActivity.cs:25 
[MonoDroid]   at Android.Support.V4.App.FragmentActivity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x00011] in <7a2a36256f1648ecbd0c15a75bc5a349>:0 
[MonoDroid]   at (wrapper dynamic-method) System.Object:8bce930d-3a6f-43f9-b949-5d18e80c87a3 (intptr,intptr,intptr)

Here is a list of the packages I've added to the Xamarin.Droid Project:
packages.txt

Are you using the stable Xamarin channel? Also, can you get it, can you share the LoaderExceptions and Types?

I don't think I am. I'm using the package added from standard nuget.

It seems the problems are derived from google play gsm services package

<package id="Xamarin.GooglePlayServices.Gcm" version="42.1001.0" targetFramework="monoandroid71" />

There is one Loader Exception

{System.TypeLoadException: Could not resolve type with token 01000031}
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146233054
    HelpLink: (null)
    InnerException: (null)
    Message (System.Exception): "Could not resolve type with token 01000031"
    Message (System.TypeLoadException): "Could not resolve type with token 01000031"
    Source: (null)
    StackTrace: (null)
    TargetSite: (null)
    TypeName: "Android.Gms.Gcm.Iid.InstanceIDListenerService"
    Static members: 
    Non-public members: {System.TypeLoadException: Could not resolve type with token 01000031}
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146233054
    HelpLink: (null)
    InnerException: (null)
    Message (System.Exception): "Could not resolve type with token 01000031"
    Message (System.TypeLoadException): "Could not resolve type with token 01000031"
    Source: (null)
    StackTrace: (null)
    TargetSite: (null)
    TypeName: "Android.Gms.Gcm.Iid.InstanceIDListenerService"
    Static members: 
    Non-public members: {System.TypeLoadException: Could not resolve type with token 01000031}
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146233054
    HelpLink: (null)
    InnerException: (null)
    Message (System.Exception): "Could not resolve type with token 01000031"
    Message (System.TypeLoadException): "Could not resolve type with token 01000031"
    Source: (null)
    StackTrace: (null)
    TargetSite: (null)
    TypeName: "Android.Gms.Gcm.Iid.InstanceIDListenerService"
    Static members: 
    Non-public members: 

And the exceptions type

{System.TypeLoadException}
    Assembly: {mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e}
    AssemblyQualifiedName: "System.TypeLoadException, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e"
    Attributes: System.Reflection.TypeAttributes.BeforeFieldInit | System.Reflection.TypeAttributes.Public | System.Reflection.TypeAttributes.Serializable
    BaseType: {System.SystemException}
    ContainsGenericParameters (System.RuntimeType): false
    ContainsGenericParameters (System.Type): false
    CustomAttributes: Count = 1
    DeclaredConstructors: {System.Reflection.ConstructorInfo[6]}
    DeclaredEvents: {System.Reflection.EventInfo[0]}
    DeclaredFields: {System.Reflection.FieldInfo[4]}
    DeclaredMembers: {System.Reflection.MemberInfo[16]}
    DeclaredMethods: {System.Reflection.MethodInfo[4]}
    DeclaredNestedTypes: {System.Reflection.TypeInfo}
    DeclaredProperties: {System.Reflection.PropertyInfo[2]}
    DeclaringMethod (System.Type): (null)
    System.InvalidOperationException: DeclaringMethod can only be used on generic arguments
    DeclaringType (System.RuntimeType): (null)
    DeclaringType (System.Type): (null)
    FullName: "System.TypeLoadException"
    GUID: {00000000-0000-0000-0000-000000000000}
    System.InvalidOperationException: Method may only be called on a Type for which Type.IsGenericParameter is true.
    System.InvalidOperationException: Method may only be called on a Type for which Type.IsGenericParameter is true.
    System.InvalidOperationException: Method may only be called on a Type for which Type.IsGenericParameter is true.
    System.InvalidOperationException: Method may only be called on a Type for which Type.IsGenericParameter is true.
    GenericTypeArguments: {System.Type[0]}
    GenericTypeParameters: {System.Type[0]}
    HasElementType: false
    ImplementedInterfaces: {System.RuntimeType[1]}
    IsAbstract: false
    IsAnsiClass: true
    IsArray: false
    IsAutoClass: false
    IsAutoLayout: true
    IsByRef: false
    IsCOMObject: false
    IsClass: true
    IsConstructedGenericType (System.RuntimeType): false
    IsConstructedGenericType (System.Type): false
    IsContextful: false
    IsEnum (System.RuntimeType): false
    IsEnum (System.Type): false
    IsExplicitLayout: false
    IsGenericParameter (System.Type): false
    IsGenericParameter (System.RuntimeType): false
    IsGenericType (System.Type): false
    IsGenericType (System.RuntimeType): false
    IsGenericTypeDefinition (System.RuntimeType): false
    IsGenericTypeDefinition (System.Type): false
    IsImport: false
    IsInterface: false
    IsLayoutSequential: false
    IsMarshalByRef: false
    IsNested: false
    IsNestedAssembly: false
    IsNestedFamANDAssem: false
    IsNestedFamORAssem: false
    IsNestedFamily: false
    IsNestedPrivate: false
    IsNestedPublic: false
    IsNotPublic: false
    IsPointer: false
    IsPrimitive: false
    IsPublic: true
    IsSealed: false
    System.NotImplementedException: The method or operation is not implemented.
    System.NotImplementedException: The method or operation is not implemented.
    System.NotImplementedException: The method or operation is not implemented.
    IsSerializable: true
    IsSpecialName: false
    IsUnicodeClass: false
    IsValueType: false
    IsVisible: true
    MemberType (System.RuntimeType): System.Reflection.MemberTypes.TypeInfo
    MemberType (System.Type): System.Reflection.MemberTypes.TypeInfo
    MetadataToken (System.RuntimeType): 33555787
    MetadataToken (System.Reflection.MemberInfo): 33555787
    Module (System.Reflection.MemberInfo): {mscorlib.dll}
    Module (System.RuntimeType): {mscorlib.dll}
    Name: "TypeLoadException"
    Namespace: "System"
    ReflectedType (System.RuntimeType): (null)
    ReflectedType (System.Type): (null)
    StructLayoutAttribute (System.Type): {System.Runtime.InteropServices.StructLayoutAttribute}
    StructLayoutAttribute (System.RuntimeType): {System.Runtime.InteropServices.StructLayoutAttribute}
    TypeHandle (System.Type): {System.RuntimeTypeHandle}
    TypeHandle (System.RuntimeType): {System.RuntimeTypeHandle}
    TypeInitializer: (null)
    UnderlyingSystemType: {System.TypeLoadException}
    Static members: 
    Non-public members: 

The problem does not occur when the Google Play Services version is 29.0.0.1

That's so annoying :/ We're working on a general-purpose fix for that, but we don't expect it to ship in the next 1-2 weeks.

@nirinchev Will this be version 1.0.5?

No, it doesn't have a name yet, but likely 1.2.0.

i just started seeing this issue after updating to the latest stable build from Xamarin (for iOS). i am using realm 0.80 and i am seeing the exact same error as above

`System.IO.FileNotFoundException: Could not load file or assembly or one of is dependencies.
File name: 'System.Threading.Tasks, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f11d50a3a

Could not load file or assembly or one of its dependencies.`

I am wondering if this issue has been fixed in 1.1.1

@EugeneUr if your issue is related to google play services - no, but if your issue is related to Xamarin Playbooks - yes. Best to update and test for yourself :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MtMovo picture MtMovo  路  3Comments

AndyDentFree picture AndyDentFree  路  7Comments

salgiza picture salgiza  路  10Comments

egimbernat picture egimbernat  路  9Comments

Arminkhodaei picture Arminkhodaei  路  6Comments