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

CSharp driver exception when Saving a an object with an IList<T> property as opposed to List<T>

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 0.9
    • Affects Version/s: 0.9
    • Component/s: None
    • None
    • Environment:
      Visual Studio 2008

      Long story short, if I call MongoCollection.Save<T>() on a class with
      an IList<T> property it throws an exception. If I change it it List<T>
      it does not.

      Long Explanation where I take actual code, and scrub it:

      So I have class that kinda looks like this:

      [DataContract]
      public class ChildClass : ParentClass
      {
      [BsonIgnoreIfNull]
      [DataMember(Order = 1)]
      [BsonKnownTypes(typeof(List<SomeClass>))]
      public IList<SomeClass> SomeProperty

      { get; set; }
      }


      And then I do something like this:

      var obj = new ChildClass{
      SomeProperty = new List<SomeClass>();
      };
      _db["someCollection"].Save(obj, SafeMode.True);

      Then I get the following
      GenericArraySerializer cannot be used with type:
      System.Collections.Generic.IList`1[[SomeClass, SomeAssembly
      Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
      at MongoDB.Bson.DefaultSerializer.GenericArraySerializer.VerifyNominalType(Type
      nominalType) in d:
      justin's
      projects\\mongo-csharp\\Bson\\DefaultSerializer\\Serializers
      GenericArraySerializer.cs:line
      113\r\n at MongoDB.Bson.DefaultSerializer.GenericArraySerializer.Serialize(BsonWriter
      bsonWriter, Type nominalType, Object value, Boolean serializeIdFirst)
      in d:
      justin's
      projects\\mongo-csharp\\Bson\\DefaultSerializer\\Serializers
      GenericArraySerializer.cs:line
      83\r\n at MongoDB.Bson.DefaultSerializer.BsonClassMapSerializer.SerializeMember(BsonWriter
      bsonWriter, Object obj, BsonMemberMap memberMap) in d:
      justin's
      projects\\mongo-csharp\\Bson\\DefaultSerializer
      BsonClassMapSerializer.cs:line
      221\r\n at MongoDB.Bson.DefaultSerializer.BsonClassMapSerializer.Serialize(BsonWriter
      bsonWriter, Type nominalType, Object value, Boolean serializeIdFirst)
      in d:
      justin's
      projects\\mongo-csharp\\Bson\\DefaultSerializer
      BsonClassMapSerializer.cs:line
      181\r\n at MongoDB.Bson.Serialization.BsonSerializer.Serialize(BsonWriter
      bsonWriter, Type nominalType, Object value, Boolean serializeIdFirst)
      in d:
      justin's
      projects\\mongo-csharp\\Bson\\Serialization
      BsonSerializer.cs:line
      245\r\n at MongoDB.Bson.Serialization.BsonSerializer.Serialize(BsonWriter
      bsonWriter, Type nominalType, Object value) in d:
      justin's
      projects\\mongo-csharp\\Bson\\Serialization
      BsonSerializer.cs:line
      229\r\n at MongoDB.Bson.DefaultSerializer.EnumerableSerializer`1.Serialize(BsonWriter
      bsonWriter, Type nominalType, Object value, Boolean serializeIdFirst)
      in d:
      justin's
      projects\\mongo-csharp\\Bson\\DefaultSerializer\\Serializers
      CollectionSerializersGeneric.cs:line
      89\r\n at MongoDB.Bson.DefaultSerializer.BsonClassMapSerializer.SerializeMember(BsonWriter
      bsonWriter, Object obj, BsonMemberMap memberMap) in d:
      justin's
      projects\\mongo-csharp\\Bson\\DefaultSerializer
      BsonClassMapSerializer.cs:line
      221\r\n at MongoDB.Bson.DefaultSerializer.BsonClassMapSerializer.Serialize(BsonWriter
      bsonWriter, Type nominalType, Object value, Boolean serializeIdFirst)
      in d:
      justin's
      projects\\mongo-csharp\\Bson\\DefaultSerializer
      BsonClassMapSerializer.cs:line
      181\r\n at MongoDB.Bson.Serialization.BsonSerializer.Serialize(BsonWriter
      bsonWriter, Type nominalType, Object value, Boolean serializeIdFirst)
      in d:
      justin's
      projects\\mongo-csharp\\Bson\\Serialization
      BsonSerializer.cs:line
      245\r\n at MongoDB.Bson.Serialization.BsonSerializer.Serialize(BsonWriter
      bsonWriter, Type nominalType, Object value) in d:
      justin's
      projects\\mongo-csharp\\Bson\\Serialization
      BsonSerializer.cs:line
      229\r\n at MongoDB.Bson.DefaultSerializer.EnumerableSerializer`1.Serialize(BsonWriter
      bsonWriter, Type nominalType, Object value, Boolean serializeIdFirst)
      in d:
      justin's
      projects\\mongo-csharp\\Bson\\DefaultSerializer\\Serializers
      CollectionSerializersGeneric.cs:line
      89\r\n at MongoDB.Bson.DefaultSerializer.BsonClassMapSerializer.SerializeMember(BsonWriter
      bsonWriter, Object obj, BsonMemberMap memberMap) in d:
      justin's
      projects\\mongo-csharp\\Bson\\DefaultSerializer
      BsonClassMapSerializer.cs:line
      221\r\n at MongoDB.Bson.DefaultSerializer.BsonClassMapSerializer.Serialize(BsonWriter
      bsonWriter, Type nominalType, Object value, Boolean serializeIdFirst)
      in d:
      justin's
      projects\\mongo-csharp\\Bson\\DefaultSerializer
      BsonClassMapSerializer.cs:line
      181\r\n at MongoDB.Bson.Serialization.BsonSerializer.Serialize(BsonWriter
      bsonWriter, Type nominalType, Object value, Boolean serializeIdFirst)
      in d:
      justin's
      projects\\mongo-csharp\\Bson\\Serialization
      BsonSerializer.cs:line
      245\r\n at MongoDB.Bson.Serialization.BsonSerializer.Serialize[T](BsonWriter
      bsonWriter, T value, Boolean serializeIdFirst) in d:
      justin's
      projects\\mongo-csharp\\Bson\\Serialization
      BsonSerializer.cs:line
      221\r\n at MongoDB.Driver.Internal.MongoUpdateMessage`2.WriteBody()
      in d:
      justin's
      projects\\mongo-csharp\\Driver\\Internal
      MongoUpdateMessage.cs:line
      58\r\n at MongoDB.Driver.Internal.MongoRequestMessage.WriteToBuffer()
      in d:
      justin's
      projects\\mongo-csharp\\Driver\\Internal
      MongoRequestMessage.cs:line
      81\r\n at MongoDB.Driver.Internal.MongoConnection.SendMessage(MongoRequestMessage
      message, SafeMode safeMode) in d:
      justin's
      projects\\mongo-csharp\\Driver\\Internal
      MongoConnection.cs:line
      324\r\n at MongoDB.Driver.MongoCollection.Update[TQuery,TUpdate](TQuery
      query, TUpdate update, UpdateFlags flags, SafeMode safeMode) in
      d:
      justin's projects\\mongo-csharp\\Driver\\Core
      MongoCollection.cs:line
      662\r\n at MongoDB.Driver.MongoCollection.Save[TDocument](TDocument
      document, SafeMode safeMode) in d:
      justin's
      projects\\mongo-csharp\\Driver\\Core
      MongoCollection.cs:line 584\r\n
      at
      ... ... ... My Code .......


      So the fix is as follows:

      [DataContract]
      public class ChildClass : ParentClass
      {
      [BsonIgnoreIfNull]
      [DataMember(Order = 1)]
      - [BsonKnownTypes(typeof(List<SomeClass>))]
      - public IList<SomeClass> SomeProperty { get; set; }

      + public List<SomeClass> SomeProperty

      { get; set; }

      }

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            zippy1981 Justin Dearing
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: