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

ArgumentOutOfRangeException when inserting document with subdocument that extends class without adding any properties

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major - P3
    • Resolution: Unresolved
    • Affects Version/s: 2.8.1
    • Fix Version/s: None
    • Component/s: Serialization
    • Labels:
      None
    • Environment:
      .NET Core 2.2 running in a Linux container

      Description

      Here is my object model

      public class Course
      {
          public Course(string name, Teacher teacher)
          {
              Name = name;
              CourseTeacher = teacher;
          }
       
          public string Name { get; private set; }
          public Teacher CourseTeacher { get; private set; }
      }
      public class Teacher
      {
          public Teacher(string name)
          {
              Name = name;
          }
       
           public string Name { get; private set; }
      }
      public class SubstituteTeacher : Teacher
      {
          public SubstituteTeacher(string name) : base(name)
          {
          }
      }

       

      When I call InsertOneAsync like this:

      var course = new Course("Math", new SubstituteTeacher("Larry Smith"));
      await collection.InsertOneAsync(course);

      I get this error:

      System.ArgumentOutOfRangeException
       HResult=0x80131502
       Message=The memberInfo argument must be for class SubstituteTeacher, but was for class Teacher.
      Parameter name: memberInfo
       Source=MongoDB.Bson
       StackTrace:
       at MongoDB.Bson.Serialization.BsonClassMap.EnsureMemberInfoIsForThisClass(MemberInfo memberInfo)
       at MongoDB.Bson.Serialization.BsonClassMap.MapMember(MemberInfo memberInfo)
       at MongoDB.Bson.Serialization.Conventions.ImmutableTypeClassMapConvention.Apply(BsonClassMap classMap)
       at MongoDB.Bson.Serialization.Conventions.ConventionRunner.Apply(BsonClassMap classMap)
       at MongoDB.Bson.Serialization.BsonClassMap.AutoMapClass()
       at MongoDB.Bson.Serialization.BsonClassMap.LookupClassMap(Type classType)
       at MongoDB.Bson.Serialization.BsonClassMapSerializationProvider.GetSerializer(Type type, IBsonSerializerRegistry serializerRegistry)
       at MongoDB.Bson.Serialization.BsonSerializerRegistry.CreateSerializer(Type type)
       at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
       at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.Serialize(BsonSerializationContext context, BsonSerializationArgs args, TClass value)
       at MongoDB.Bson.Serialization.Serializers.SerializerBase`1.MongoDB.Bson.Serialization.IBsonSerializer.Serialize(BsonSerializationContext context, BsonSerializationArgs args, Object value)
       at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Serialize(IBsonSerializer serializer, BsonSerializationContext context, Object value)
       at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.SerializeClass(BsonSerializationContext context, BsonSerializationArgs args, TClass document)
       at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.Serialize(BsonSerializationContext context, BsonSerializationArgs args, TClass value)
       at MongoDB.Bson.Serialization.Serializers.SerializerBase`1.MongoDB.Bson.Serialization.IBsonSerializer.Serialize(BsonSerializationContext context, BsonSerializationArgs args, Object value)
       at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Serialize(IBsonSerializer serializer, BsonSerializationContext context, Object value)
       at MongoDB.Bson.Serialization.Serializers.BsonValueSerializerBase`1.Serialize(BsonSerializationContext context, BsonSerializationArgs args, TBsonValue value)
       at MongoDB.Bson.Serialization.Serializers.SerializerBase`1.MongoDB.Bson.Serialization.IBsonSerializer.Serialize(BsonSerializationContext context, BsonSerializationArgs args, Object value)
       at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Serialize(IBsonSerializer serializer, BsonSerializationContext context, Object value)
       at MongoDB.Bson.Serialization.Serializers.BsonValueSerializerBase`1.Serialize(BsonSerializationContext context, BsonSerializationArgs args, TBsonValue value)
       at MongoDB.Driver.Core.Operations.BulkInsertOperation.InsertRequestSerializer.SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, InsertRequest value)
       at MongoDB.Bson.Serialization.Serializers.SealedClassSerializerBase`1.Serialize(BsonSerializationContext context, BsonSerializationArgs args, TValue value)
       at MongoDB.Bson.Serialization.Serializers.SerializerBase`1.MongoDB.Bson.Serialization.IBsonSerializer.Serialize(BsonSerializationContext context, BsonSerializationArgs args, Object value)
       at MongoDB.Driver.Core.WireProtocol.Messages.Encoders.BinaryEncoders.CommandMessageBinaryEncoder.WriteType1Section(BsonBinaryWriter writer, Type1CommandMessageSection section, Int64 messageStartPosition)
       at MongoDB.Driver.Core.WireProtocol.Messages.Encoders.BinaryEncoders.CommandMessageBinaryEncoder.WriteSections(BsonBinaryWriter writer, IEnumerable`1 sections, Int64 messageStartPosition)
       at MongoDB.Driver.Core.WireProtocol.Messages.Encoders.BinaryEncoders.CommandMessageBinaryEncoder.WriteMessage(CommandMessage message)
       at MongoDB.Driver.Core.Connections.BinaryConnection.SendMessagesHelper.EncodeMessages(CancellationToken cancellationToken)
       at MongoDB.Driver.Core.Connections.BinaryConnection.<SendMessagesAsync>d__58.MoveNext()
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol`1.<ExecuteAsync>d__13.MoveNext()
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at MongoDB.Driver.Core.Servers.Server.ServerChannel.<ExecuteProtocolAsync>d__29`1.MoveNext()
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at MongoDB.Driver.Core.Operations.RetryableWriteOperationExecutor.<ExecuteAsync>d__3`1.MoveNext()
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at MongoDB.Driver.Core.Operations.BulkUnmixedWriteOperationBase`1.<ExecuteBatchAsync>d__46.MoveNext()
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at MongoDB.Driver.Core.Operations.BulkUnmixedWriteOperationBase`1.<ExecuteBatchesAsync>d__48.MoveNext()
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation.<ExecuteBatchAsync>d__51.MoveNext()
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation.<ExecuteAsync>d__44.MoveNext()
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at MongoDB.Driver.OperationExecutor.<ExecuteWriteOperationAsync>d__5`1.MoveNext()
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at MongoDB.Driver.MongoCollectionImpl`1.<ExecuteWriteOperationAsync>d__95`1.MoveNext()
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at MongoDB.Driver.MongoCollectionImpl`1.<BulkWriteAsync>d__26.MoveNext()
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at MongoDB.Driver.MongoCollectionImpl`1.<UsingImplicitSessionAsync>d__101`1.MoveNext()
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at MongoDB.Driver.MongoCollectionBase`1.<InsertOneAsync>d__70.MoveNext()
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at JohnKnoop.MongoRepository.ManualTesting.Program.<Main>d__0.MoveNext() in C:\work\JohnKnoop.MongoRepository.ManualTesting\Program.cs:line 132

      However, if I add a property to the SubstituteTeacher class, then the InsertOneAsync call completed without error:

      public class SubstituteTeacher : Teacher {
          public SubstituteTeacher(string name) : base(name) {
          }
          
          public string AdditionalProperty { get; private set; }
      }

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            knoopjohn@gmail.com John Knoop
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: