[CSHARP-1493] Unable to cast object exception thrown when trying to access FileInfo.Id from GridFs.FindAsync() results Created: 05/Dec/15  Updated: 05/Dec/15  Resolved: 05/Dec/15

Status: Closed
Project: C# Driver
Component/s: GridFS
Affects Version/s: 2.1.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Christian Sleight [X] Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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"



 Comments   
Comment by Robert Stam [ 05/Dec/15 ]

The GridFS spec requires that the Id for all new GridFS files be of type ObjectId.

Drivers may optionally support reading existing GridFS files where the Id is not an ObjectId.

To access the Id where the value is not an ObjectId, use the IdAsBsonValue property instead of the Id property of the GridFSFileInfo class.

Generated at Wed Feb 07 21:39:46 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.