|
I neglected to read the compiler warnings of my repro, which clearly state:
/Users/james/Dropbox/code/cases/csharp4056/Program.cs(27,49): warning CS1062: The best overloaded Add method 'BsonValue.implicit operator BsonValue(Guid)' for the collection initializer element is obsolete. Use the BsonBinaryData constructor instead and specify a Guid representation. [/Users/james/Dropbox/code/cases/csharp4056/csharp4056.csproj]
|
1 Warning(s)
|
0 Error(s)
|
BsonValue serialization is completely deterministic and does not rely on the SerializerRegistry. This is why registering a default serializer for GUIDs does not affect the serialization for a GUID contained in a BsonDocument.
When a GUID is included in a BsonDocument, we are relying on an implicit conversion from System.Guid to BsonValue. This implicit conversion does not allow specification of the GuidRepresentation. The correct way to add a GUID to a BsonDocument is:
var doc = new BsonDocument {{ "_id", new BsonBinaryData(Guid.NewGuid(), GuidRepresentation.Standard) }};
|
Use the BsonBinaryData(Guid, GuidRepresentation) constructor when adding GUIDs directly to BsonDocuments.
Note: You must opt into GuidRepresentationMode.V3 in order to set the GuidRepresentation on a per-field basis. See GuidRepresentationMode for more information.
|