Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REALM - System.TypeInitializationException: The type initializer for 'Realms.Realm' threw an exception. ---> System.DllNotFoundException

This is the exception from realm

{System.TypeInitializationException: The type initializer for 'Realms.Realm' threw an exception. ---> 
System.DllNotFoundException: realm-wrappers
  at (wrapper managed-to-native) Realms.NativeCommon:register_notify_realm_changed 
(Realms.NativeCommon/NotifyRealmCallback)
  at Realms.Realm..cctor () [0x0004f] in <f0097c463d884e09ba2bde58b530c6d7>:0 

--- End of inner exception stack trace ---
  at DAL.t_MobileUser..ctor () [0x0001e] in 
D:\visual studio 2015\Projects\RBankCI\DAL\t_MobileUser.cs:17 
  at BLL.BLL_MobileUser.m05_selectSpecific (System.Int32 id) [0x00013] in 
D:\visual studio 2015\Projects\RBankCI\BLL\BLL_MobileUser.cs:34 
  at RBankCI.Activity_PDRN.m7_btnSaveClick () [0x00008] in 
D:\visual studio 2015\Projects\RBankCI\RBankCI\Activities\Activity_PDRN.cs:96 }

When I call Realm realm = Realm.GetInstance(config); I am receiving an error which is at the top. I downloaded the realm from nugget packager so I don't know what it says as DllNotFoundException

I tried to download these versions but same result of exception: 2.1.0, 0.82.1 and 0.80 (same from my previous app which has no exception)

Here is my sample code.

    private Realm realm;

    public void insert(object obj)
    {
        try
        {
            realm = Realm.GetInstance(DAL_DBAccessVariable.config); //Error occur here
            ENT_MobileUser entity = (ENT_MobileUser)obj;
            using (var transaction = realm.BeginWrite())
            {
                DAL_MobileUserEntity dbTable = realm.CreateObject<DAL_MobileUserEntity>();
                dbTable.id = entity.id;
                dbTable.firstName = entity.firstName;
                dbTable.middleName = entity.middleName;
                dbTable.lastName = entity.lastName;
                dbTable.userID = entity.userID;
                dbTable.userPinCode = entity.userPinCode;
                dbTable.licenseKey = entity.licenseKey;
                dbTable.licenseExpiry = entity.licenseExpiry;

                transaction.Commit();
            }
        }
        catch (Exception e)
        { throw; }
    }

this is the DAL_DBAccessVariables

using Realms;
using System;
using System.IO;

namespace DAL
{
    public class DAL_DBAccessVariable
    {
        private const string databaseName = "name.realm";
        public static readonly string dbPath =
            Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), databaseName);
        public static readonly RealmConfiguration config = new RealmConfiguration(dbPath);
    }
}

Additional infos if may help. I updated my xamarin to latest version, visual studio 2015 professional.

like image 201
jace Avatar asked Dec 15 '17 07:12

jace


1 Answers

I found the answer here - https://github.com/realm/realm-dotnet/issues/1354 provided the screenshot.

enter image description here

The real problem is when you are implementing a business layer and having a DLL project to store your database connections. If so, we have to include the realm not just in DLL or DAL but also in our main project and it solve the problem 100%

like image 127
jace Avatar answered Oct 19 '22 22:10

jace