This has been reported at [Bug]: BsonSerializationException when accessing realm offline. · Issue #2749 · realm/realm-dotnet · GitHub but no reply yet.
My Xamarin application uses Realm Sync, and accesses the realm by creating a SyncConfiguration
and then calling GetInstance()
on it. This works fine when the application is online, but if the app is offline, the SyncConfiguration
constructor throws the following exception:
**System.TypeInitializationException:** 'The type initializer for 'Realms.Helpers.SerializationHelper' threw an exception.'
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'Realms.Helpers.SerializationHelper' threw an exception. ---> MongoDB.Bson.BsonSerializationException: There is already a serializer registered for type DateTimeOffset.
[mono-rt] at MongoDB.Bson.Serialization.BsonSerializerRegistry.RegisterSerializer (System.Type type, MongoDB.Bson.Serialization.IBsonSerializer serializer) [0x000a5] in <3323b1fef8254a9a8e9fe351c6349ba3>:0
[mono-rt] at MongoDB.Bson.Serialization.BsonSerializer.RegisterSerializer (System.Type type, MongoDB.Bson.Serialization.IBsonSerializer serializer) [0x00000] in <3323b1fef8254a9a8e9fe351c6349ba3>:0
[mono-rt] at MongoDB.Bson.Serialization.BsonSerializer.RegisterSerializer[T] (MongoDB.Bson.Serialization.IBsonSerializer`1[TValue] serializer) [0x00000] in <3323b1fef8254a9a8e9fe351c6349ba3>:0
[mono-rt] at Realms.Helpers.SerializationHelper..cctor () [0x0002f] in D:\a\realm-dotnet\realm-dotnet\Realm\Realm\Helpers\SerializationHelper.cs:42
[mono-rt] --- End of inner exception stack trace ---
[mono-rt] at Example.Services.RealmService.GetInstanceAsync (System.String partitionKey) [0x0017a] in D:\TextflowMobile\Example\Services\RealmService.cs:80
[mono-rt] at Example.Services.UpdaterService.StartUpdater (System.Boolean quickStart) [0x00057] in D:\TextflowMobile\Example\Services\UpdaterService.cs:149
[mono-rt] at Example.ViewModels.LoginViewModel.<get_LoginCommand>b__21_0 () [0x00106] in D:\TextflowMobile\Example\ViewModels\LoginViewModel.cs:87
[mono-rt] at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.28(intptr,intptr)
[mono-rt] at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.28(intptr,intptr)
To reproduce, do the following:
- Sign in a Realm Sync user when online.
- Turn the phone’s internet off and redeploy the app.
- Get the last logged in user.
- Create a sync configuration object as follows:
SyncConfiguration config = new SyncConfiguration("PUBLIC", _realmUser);
(This is the stage where the exception is thrown).