[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 HResult: -2147467262 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' |
| 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. |