LiteDB exception when deployed to ARM device developed withUnity 2019.4.12f1

492 views Asked by At

I'm trying to use LiteDB (net45, V5.0.9) in Unity 2019 targeting an ARM device. The sample code (provided in the docs) to perform CRUD operations works fine in the Unity editor but in the device I'm getting this exception:

System.NotSupportedException: Invalid BsonExpression when converted from Linq expression: x => x.Name - $.Name ---> System.TypeInitializationException: The type initializer for 'LiteDB.BsonExpression' threw an exception. ---> System.ArgumentNullException: Value cannot be null.
Parameter name: method
at System.Dynamic.Utils.ContractUtils.RequiresNotNull (System.Object value, System.String paramName) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Linq.Expressions.Expression.Call (System.Reflection.MethodInfo method, System.Linq.Expressions.Expression arg0, System.Linq.Expressions.Expression arg1) [0x00000] in <00000000000000000000000000000000>:0 
  at LiteDB.BsonExpressionParser.TryParsePath (LiteDB.Tokenizer tokenizer, LiteDB.ExpressionContext context, LiteDB.BsonDocument parameters, LiteDB.DocumentScope scope) [0x00000] in <00000000000000000000000000000000>:0 
  at LiteDB.BsonExpressionParser.ParseSingleExpression (LiteDB.Tokenizer tokenizer, LiteDB.ExpressionContext context, LiteDB.BsonDocument parameters, LiteDB.DocumentScope scope) [0x00000] in <00000000000000000000000000000000>:0 
  at LiteDB.BsonExpressionParser.ParseFullExpression (LiteDB.Tokenizer tokenizer, LiteDB.ExpressionContext context, LiteDB.BsonDocument parameters, LiteDB.DocumentScope scope) [0x00000] in <00000000000000000000000000000000>:0 
  at LiteDB.BsonExpression.Create (System.String expression, LiteDB.BsonDocument parameters) [0x00000] in <00000000000000000000000000000000>:0 
  at LiteDB.BsonExpression.Create (System.String expression) [0x00000] in <00000000000000000000000000000000>:0 
  at LiteDB.BsonExpression..cctor () [0x00000] in <00000000000000000000000000000000>:0 
  at LiteDB.LinqExpressionVisitor.Resolve (System.Boolean predicate) [0x00000] in <00000000000000000000000000000000>:0 
  at LiteDB.BsonMapper.GetExpression[T,K] (System.Linq.Expressions.Expression1[TDelegate] predicate) [0x00000] in <00000000000000000000000000000000>:0 
  at LiteDB.LiteCollection1[T].GetIndexExpression[K] (System.Linq.Expressions.Expression1[TDelegate] keySelector) [0x00000] in <00000000000000000000000000000000>:0 
  at LiteDB.LiteCollection1[T].EnsureIndex[K] (System.Linq.Expressions.Expression1[TDelegate] keySelector, System.Boolean unique) [0x00000] in <00000000000000000000000000000000>:0 

I did some googling and found a stack overflow question asking about this, in which the one answer states that this could be due to missing dependencies. However, I am using the .NETFramework 4.5 DLL which according to the NuGet page does not have any dependencies.

Can someone help me out with this?

EDIT :

This is the sample code which I used:

// Create your POCO class
public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public string[] Phones { get; set; }
    public bool IsActive { get; set; }
}

// Open database (or create if doesn't exist)
using(var db = new LiteDatabase(@"MyData.db"))
{
    // Get customer collection
    var col = db.GetCollection<Customer>("customers");

    // Create your new customer instance
    var customer = new Customer
    { 
        Name = "John Doe", 
        Phones = new string[] { "8000-0000", "9000-0000" }, 
        Age = 39,
        IsActive = true
    };

    // Create unique index in Name field
    col.EnsureIndex(x => x.Name, true);

    // Insert new customer document (Id will be auto-incremented)
    col.Insert(customer);

    // Update a document inside a collection
    customer.Name = "Joana Doe";

    col.Update(customer);

    // Use LINQ to query documents (with no index)
    var results = col.Find(x => x.Age > 20);
}

Could the issue be because of running this on IL2CPP backend?

1

There are 1 answers

0
Vip12 On

Figured out what was causing the issue.

Unity was stripping assemblies as these were referenced through reflection. I specified the assemblies not to be stripped in the link.xml file as mentioned here. This solves the issue.