Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-526

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

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.6
    • Affects Version/s: 1.5
    • Component/s: 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.

        1. Program.cs
          1 kB
          Robert Stam

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            thoseamongus Gabriel Figueroa
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: