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

Unable to cast object exception thrown when trying to access FileInfo.Id from GridFs.FindAsync() results

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.1.1
    • Component/s: GridFS
    • None

      I migrated from the legacy driver to the new implementation of GridFs and when I run a FindAsync() that returns files that were previously stored with legacy version an invalid cast exception is thrown when trying to access the ObjectId via FileInfo.Id property.

      I am wondering if this is a result of earlier code we had that set the Id during upload? My assumption is that these Id's are not compatible with ObjectId. [GridFs.Upload(file, id, new MongoGridFSCreateOptions()

      { Id = id }

      );]

      Details of Exception

      fileInfo.Id
      'fileInfo.Id' threw an exception of type 'System.InvalidCastException'
      Data:

      {System.Collections.ListDictionaryInternal}

      HResult: -2147467262
      HelpLink: null
      InnerException: null
      Message: "Unable to cast object of type 'MongoDB.Bson.BsonString' to type 'MongoDB.Bson.BsonObjectId'."
      Source: "MongoDB.Driver.GridFS"
      StackTrace: " at MongoDB.Driver.GridFS.GridFSFileInfo.get_Id()"
      TargetSite:

      {MongoDB.Bson.ObjectId get_Id()}

      fileInfo.IdAsBsonValue

      {55b66883acaf044ce05919ff}
      AsBoolean: 'fileInfo.IdAsBsonValue.AsBoolean' threw an exception of type 'System.InvalidCastException'
      AsBsonArray: 'fileInfo.IdAsBsonValue.AsBsonArray' threw an exception of type 'System.InvalidCastException'
      AsBsonBinaryData: 'fileInfo.IdAsBsonValue.AsBsonBinaryData' threw an exception of type 'System.InvalidCastException'
      AsBsonDateTime: 'fileInfo.IdAsBsonValue.AsBsonDateTime' threw an exception of type 'System.InvalidCastException'
      AsBsonDocument: 'fileInfo.IdAsBsonValue.AsBsonDocument' threw an exception of type 'System.InvalidCastException'
      AsBsonJavaScript: 'fileInfo.IdAsBsonValue.AsBsonJavaScript' threw an exception of type 'System.InvalidCastException'
      AsBsonJavaScriptWithScope: 'fileInfo.IdAsBsonValue.AsBsonJavaScriptWithScope' threw an exception of type 'System.InvalidCastException'
      AsBsonMaxKey: 'fileInfo.IdAsBsonValue.AsBsonMaxKey' threw an exception of type 'System.InvalidCastException'
      AsBsonMinKey: 'fileInfo.IdAsBsonValue.AsBsonMinKey' threw an exception of type 'System.InvalidCastException'
      AsBsonNull: 'fileInfo.IdAsBsonValue.AsBsonNull' threw an exception of type 'System.InvalidCastException'
      AsBsonRegularExpression: 'fileInfo.IdAsBsonValue.AsBsonRegularExpression' threw an exception of type 'System.InvalidCastException'
      AsBsonSymbol: 'fileInfo.IdAsBsonValue.AsBsonSymbol' threw an exception of type 'System.InvalidCastException'
      AsBsonTimestamp: 'fileInfo.IdAsBsonValue.AsBsonTimestamp' threw an exception of type 'System.InvalidCastException'
      AsBsonUndefined: 'fileInfo.IdAsBsonValue.AsBsonUndefined' threw an exception of type 'System.InvalidCastException'
      AsBsonValue: {55b66883acaf044ce05919ff}

      AsByteArray: 'fileInfo.IdAsBsonValue.AsByteArray' threw an exception of type 'System.InvalidCastException'
      AsDateTime: 'fileInfo.IdAsBsonValue.AsDateTime' threw an exception of type 'System.InvalidCastException'
      AsDouble: 'fileInfo.IdAsBsonValue.AsDouble' threw an exception of type 'System.InvalidCastException'
      AsGuid: 'fileInfo.IdAsBsonValue.AsGuid' threw an exception of type 'System.InvalidCastException'
      AsInt32: 'fileInfo.IdAsBsonValue.AsInt32' threw an exception of type 'System.InvalidCastException'
      AsInt64: 'fileInfo.IdAsBsonValue.AsInt64' threw an exception of type 'System.InvalidCastException'
      AsLocalTime: 'fileInfo.IdAsBsonValue.AsLocalTime' threw an exception of type 'System.InvalidCastException'
      AsNullableBoolean: 'fileInfo.IdAsBsonValue.AsNullableBoolean' threw an exception of type 'System.InvalidCastException'
      AsNullableDateTime: 'fileInfo.IdAsBsonValue.AsNullableDateTime' threw an exception of type 'System.InvalidCastException'
      AsNullableDouble: 'fileInfo.IdAsBsonValue.AsNullableDouble' threw an exception of type 'System.InvalidCastException'
      AsNullableGuid: 'fileInfo.IdAsBsonValue.AsNullableGuid' threw an exception of type 'System.InvalidCastException'
      AsNullableInt32: 'fileInfo.IdAsBsonValue.AsNullableInt32' threw an exception of type 'System.InvalidCastException'
      AsNullableInt64: 'fileInfo.IdAsBsonValue.AsNullableInt64' threw an exception of type 'System.InvalidCastException'
      AsNullableObjectId: 'fileInfo.IdAsBsonValue.AsNullableObjectId' threw an exception of type 'System.InvalidCastException'
      AsObjectId: 'fileInfo.IdAsBsonValue.AsObjectId' threw an exception of type 'System.InvalidCastException'
      AsRegex: 'fileInfo.IdAsBsonValue.AsRegex' threw an exception of type 'System.InvalidCastException'
      AsString: "55b66883acaf044ce05919ff"
      AsUniversalTime: 'fileInfo.IdAsBsonValue.AsUniversalTime' threw an exception of type 'System.InvalidCastException'
      BsonType: String
      IsBoolean: false
      IsBsonArray: false
      IsBsonBinaryData: false
      IsBsonDateTime: false
      IsBsonDocument: false
      IsBsonJavaScript: false
      IsBsonJavaScriptWithScope: false
      IsBsonMaxKey: false
      IsBsonMinKey: false
      IsBsonNull: false
      IsBsonRegularExpression: false
      IsBsonSymbol: false
      IsBsonTimestamp: false
      IsBsonUndefined: false
      IsDateTime: false
      IsDouble: false
      IsGuid: false
      IsInt32: false
      IsInt64: false
      IsNumeric: false
      IsObjectId: false
      IsString: true
      IsValidDateTime: false
      RawValue: "55b66883acaf044ce05919ff"
      Value: "55b66883acaf044ce05919ff"

            Assignee:
            Unassigned Unassigned
            Reporter:
            cs-NET Christian Sleight [X]
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: