REALM - System.TypeInitializationException:'Realms.Realm'的类型初始值设定项引发了异常。 ---> System.DllNotFoundException

时间:2017-12-15 07:51:00

标签: android xamarin.android 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 }

当我调用Realm realm = Realm.GetInstance(config);我收到的错误位于顶部。我从nugget packager下载了这个领域,所以我不知道它的内容是什么DllNotFoundException

我尝试下载这些版本,但异常的结果相同:2.1.0,0.82.1和0.80(与我以前的应用程序相同,没有例外)

这是我的示例代码。

    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; }
    }

这是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);
    }
}

如果有帮助,可以增加额外的信息。我将我的xamarin更新到最新版本,visual studio 2015 professional。

2 个答案:

答案 0 :(得分:6)

我在这里找到了答案 - https://github.com/realm/realm-dotnet/issues/1354提供了屏幕截图。

enter image description here

真正的问题是当您实现业务层并使用DLL项目来存储数据库连接时。如果是这样,我们不仅要包含DLL或DAL中的领域,还要包含在我们的主项目中,它可以100%解决问题

答案 1 :(得分:0)

请记住将其安装到每个项目(PCL,Android,iOS和UWP)