LINQ queries against AsQueryable<T> where T is an interface aren't supported

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Done
    • Priority: Major - P3
    • 1.6
    • Affects Version/s: 1.5
    • Component/s: None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      When you want to deserialize an object using an interface using this setup. It crashes with BsonSerializationException "No serializer found for type X". The way i have mongo setup is like this.

      MongoRepository<TObject>
      {
      public MongoRepository(string serverUrl, string databaseName, MongoCredentials credentials)

      { var mongourl = new MongoUrl(serverUrl); _mongoServer = MongoServer.Create(mongourl); _mongoCollection = _mongoDatabase.GetCollection<TObject>(typeof(TObject).Name); }

      public TObject Find(Expression<Func<TObject,bool>> predicate)

      { return _mongoCollection.AsQueryable().FirstOrDefault(predicate); }

      }

      When TObject is an Interface type it crashes with BsonSerializationException. A fix that works pretty well on git commit 6a30fd35ea2d45b3aa8f2c7b176a24e90980f150 in the BsonSerializer.cs file line 589 in the LookupSerializer method near where the exception is thrown i put this snippet in

      if(serializer == null)
      {
      foreach (var bsonSerializer in __serializers)
      {
      if(type.IsAssignableFrom(bsonSerializer.Key))

      { serializer = bsonSerializer.Value; break; }

      }
      }

      it will allow AsQueryable to use an interface type and deserialize it properly.

              Assignee:
              Robert Stam
              Reporter:
              Gabriel Figueroa
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved: