From 8426b91e1c4896880cdcf97f37b930b4351dd81e Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Tue, 27 Aug 2024 18:49:26 +0200 Subject: [PATCH] fix issue of internal serializator registry selection --- src/MongoDB.Bson/BsonDefaults.cs | 87 ++++++++++--------- src/MongoDB.Bson/BsonExtensionMethods.cs | 6 +- src/MongoDB.Bson/ObjectModel/BsonDocument.cs | 12 ++- .../MaterializedOnDemandBsonDocument.cs | 14 ++- .../ObjectModel/RawBsonDocument.cs | 27 ++++-- .../Serialization/BsonDeserializationArgs.cs | 12 +++ .../BsonDeserializationContext.cs | 21 +++-- .../Serialization/BsonSerializationArgs.cs | 10 +++ .../Serialization/BsonSerializationContext.cs | 4 +- .../BsonSerializationProviderBase.cs | 6 +- .../Serialization/BsonSerializer.cs | 37 +++++--- .../IBsonSerializerExtensions.cs | 26 +++++- .../Serializers/BsonClassMapSerializer.cs | 4 +- .../Serializers/BsonDocumentSerializer.cs | 2 +- .../BsonDocumentWrapperSerializer.cs | 2 +- .../Serializers/BsonValueSerializerBase.cs | 4 +- .../Serializers/ClassSerializerBase.cs | 4 +- .../Serializers/DictionarySerializerBase.cs | 2 +- .../DiscriminatedInterfaceSerializer.cs | 2 +- .../DiscriminatedWrapperSerializer.cs | 2 +- .../Serializers/EnumerableSerializerBase.cs | 4 +- ...ableDeserializingAsCollectionSerializer.cs | 2 +- ...mpliedImplementationInterfaceSerializer.cs | 4 +- .../Serializers/KeyValuePairSerializer.cs | 2 +- .../Serializers/NullableSerializer.cs | 2 +- .../Serializers/ObjectSerializer.cs | 4 +- .../SerializeAsNominalTypeSerializer.cs | 2 +- .../ThreeDimensionalArraySerializer.cs | 2 +- .../Serializers/TupleSerializers.cs | 16 ++-- .../TwoDimensionalArraySerializer.cs | 2 +- .../UndiscriminatedActualTypeSerializer.cs | 2 +- .../Serializers/ValueTupleSerializers.cs | 16 ++-- .../Core/Connections/BinaryConnection.cs | 4 +- .../CommandUsingQueryMessageWireProtocol.cs | 14 +-- .../CommandMessageBinaryEncoder.cs | 7 +- .../CommandRequestMessageBinaryEncoder.cs | 7 +- .../CommandResponseMessageBinaryEncoder.cs | 7 +- .../CompressedMessageBinaryEncoder.cs | 7 +- .../QueryMessageBinaryEncoder.cs | 24 +++-- .../ReplyMessageBinaryEncoder.cs | 19 ++-- .../Messages/Encoders/IMessageEncoder.cs | 9 +- .../JsonEncoders/CommandMessageJsonEncoder.cs | 7 +- .../CommandRequestMessageJsonEncoder.cs | 7 +- .../CommandResponseMessageJsonEncoder.cs | 7 +- .../CompressedMessageJsonEncoder.cs | 7 +- .../JsonEncoders/QueryMessageJsonEncoder.cs | 7 +- .../JsonEncoders/ReplyMessageJsonEncoder.cs | 7 +- .../GridFSBucketCompat.cs | 4 +- .../GridFSBucketOptions.cs | 2 +- src/MongoDB.Driver/ChangeStreamHelper.cs | 2 +- ...plicitEncryptionLibMongoCryptController.cs | 2 +- .../FieldValueSerializerHelper.cs | 2 +- .../Expressions/ISerializationExpression.cs | 4 +- ...MethodToAggregationExpressionTranslator.cs | 2 +- .../InjectMethodToFilterTranslator.cs | 2 +- .../AppendStageMethodToPipelineTranslator.cs | 2 +- src/MongoDB.Driver/MongoClient.cs | 2 +- src/MongoDB.Driver/MongoDatabaseImpl.cs | 4 +- src/MongoDB.Driver/MongoDatabaseSettings.cs | 2 +- src/MongoDB.Driver/PipelineDefinition.cs | 2 +- src/MongoDB.Driver/PipelineStageDefinition.cs | 2 +- .../PipelineStageDefinitionBuilder.cs | 2 +- src/MongoDB.Driver/UpdateDefinition.cs | 2 +- 63 files changed, 333 insertions(+), 187 deletions(-) diff --git a/src/MongoDB.Bson/BsonDefaults.cs b/src/MongoDB.Bson/BsonDefaults.cs index d1b086a43ff..6967f4c64ab 100644 --- a/src/MongoDB.Bson/BsonDefaults.cs +++ b/src/MongoDB.Bson/BsonDefaults.cs @@ -51,46 +51,6 @@ static BsonDefaults() } // public static properties - /// - /// Gets or sets the dynamic array serializer. - /// - public static IBsonSerializer DynamicArraySerializer - { - get - { - if (!__dynamicArraySerializerWasSet) - { - __dynamicArraySerializer = BsonSerializer.LookupSerializer>(); - } - return __dynamicArraySerializer; - } - set - { - __dynamicArraySerializerWasSet = true; - __dynamicArraySerializer = value; - } - } - - /// - /// Gets or sets the dynamic document serializer. - /// - public static IBsonSerializer DynamicDocumentSerializer - { - get - { - if (!__dynamicDocumentSerializerWasSet) - { - __dynamicDocumentSerializer = BsonSerializer.LookupSerializer(); - } - return __dynamicDocumentSerializer; - } - set - { - __dynamicDocumentSerializerWasSet = true; - __dynamicDocumentSerializer = value; - } - } - /// /// Gets or sets the default representation to be used in serialization of /// Guids to the database. @@ -153,5 +113,52 @@ public static int MaxSerializationDepth get { return __maxSerializationDepth; } set { __maxSerializationDepth = value; } } + + //public static methods + /// + /// Gets the dynamic array serializer. + /// + public static IBsonSerializer GetDynamicArraySerializer( + bool forceStaticSerializerRegistry = false) + { + if (!__dynamicArraySerializerWasSet) + { + __dynamicArraySerializer = BsonSerializer.LookupSerializer>(forceStaticSerializerRegistry); + } + + return __dynamicArraySerializer; + } + + /// + /// Gets or sets the dynamic document serializer. + /// + public static IBsonSerializer GetDynamicDocumentSerializer( + bool forceStaticSerializerRegistry = false) + { + if (!__dynamicDocumentSerializerWasSet) + { + __dynamicDocumentSerializer = BsonSerializer.LookupSerializer(forceStaticSerializerRegistry); + } + + return __dynamicDocumentSerializer; + } + + /// + /// Sets the dynamic array serializer. + /// + public static void SetDynamicArraySerializer(IBsonSerializer value) + { + __dynamicArraySerializerWasSet = true; + __dynamicArraySerializer = value; + } + + /// + /// Gets or sets the dynamic document serializer. + /// + public static void SetDynamicDocumentSerializer(IBsonSerializer value) + { + __dynamicDocumentSerializerWasSet = true; + __dynamicDocumentSerializer = value; + } } } diff --git a/src/MongoDB.Bson/BsonExtensionMethods.cs b/src/MongoDB.Bson/BsonExtensionMethods.cs index 16381f2c4ba..04cf5631347 100644 --- a/src/MongoDB.Bson/BsonExtensionMethods.cs +++ b/src/MongoDB.Bson/BsonExtensionMethods.cs @@ -84,7 +84,7 @@ public static byte[] ToBson( if (serializer == null) { - serializer = BsonSerializer.LookupSerializer(nominalType); + serializer = BsonSerializer.LookupSerializer(nominalType, args.ForceStaticSerializerRegistry); } if (serializer.ValueType != nominalType) { @@ -165,7 +165,7 @@ public static BsonDocument ToBsonDocument( return convertibleToBsonDocument.ToBsonDocument(); // use the provided ToBsonDocument method } - serializer = BsonSerializer.LookupSerializer(nominalType); + serializer = BsonSerializer.LookupSerializer(nominalType, args.ForceStaticSerializerRegistry); } if (serializer.ValueType != nominalType) { @@ -236,7 +236,7 @@ public static string ToJson( if (serializer == null) { - serializer = BsonSerializer.LookupSerializer(nominalType); + serializer = BsonSerializer.LookupSerializer(nominalType, args.ForceStaticSerializerRegistry); } if (serializer.ValueType != nominalType) { diff --git a/src/MongoDB.Bson/ObjectModel/BsonDocument.cs b/src/MongoDB.Bson/ObjectModel/BsonDocument.cs index 37713c19f78..040a022521d 100644 --- a/src/MongoDB.Bson/ObjectModel/BsonDocument.cs +++ b/src/MongoDB.Bson/ObjectModel/BsonDocument.cs @@ -949,8 +949,12 @@ public virtual BsonValue GetValue(string name) /// /// The name of the element. /// The default value returned if the element is not found. + /// Force to use static serializer registry /// The value of the element or the default value if the element is not found. - public virtual BsonValue GetValue(string name, BsonValue defaultValue) + public virtual BsonValue GetValue( + string name, + BsonValue defaultValue, + bool forceStaticSerializerRegistry = false) { if (name == null) { @@ -1265,8 +1269,12 @@ public virtual bool TryGetElement(string name, out BsonElement value) /// /// The name of the element. /// The value of the element. + /// Force to use static serializer registry /// True if an element with that name was found. - public virtual bool TryGetValue(string name, out BsonValue value) + public virtual bool TryGetValue( + string name, + out BsonValue value, + bool forceStaticSerializerRegistry = false) { if (name == null) { diff --git a/src/MongoDB.Bson/ObjectModel/MaterializedOnDemandBsonDocument.cs b/src/MongoDB.Bson/ObjectModel/MaterializedOnDemandBsonDocument.cs index 65708e298e3..c5c851de75d 100644 --- a/src/MongoDB.Bson/ObjectModel/MaterializedOnDemandBsonDocument.cs +++ b/src/MongoDB.Bson/ObjectModel/MaterializedOnDemandBsonDocument.cs @@ -574,13 +574,17 @@ public override BsonValue GetValue(string name) /// /// The name of the element. /// The default value returned if the element is not found. + /// Force to use static serializer registry /// /// The value of the element or the default value if the element is not found. /// - public override BsonValue GetValue(string name, BsonValue defaultValue) + public override BsonValue GetValue( + string name, + BsonValue defaultValue, + bool forceStaticSerializerRegistry = false) { EnsureIsMaterialized(); - return base.GetValue(name, defaultValue); + return base.GetValue(name, defaultValue, forceStaticSerializerRegistry); } /// @@ -726,10 +730,14 @@ public override bool TryGetElement(string name, out BsonElement value) /// /// The name of the element. /// The value of the element. + /// Force to use static serializer registry /// /// True if an element with that name was found. /// - public override bool TryGetValue(string name, out BsonValue value) + public override bool TryGetValue( + string name, + out BsonValue value, + bool forceStaticSerializerRegistry = false) { EnsureIsMaterialized(); return base.TryGetValue(name, out value); diff --git a/src/MongoDB.Bson/ObjectModel/RawBsonDocument.cs b/src/MongoDB.Bson/ObjectModel/RawBsonDocument.cs index 00d47efa35c..f23466235ef 100644 --- a/src/MongoDB.Bson/ObjectModel/RawBsonDocument.cs +++ b/src/MongoDB.Bson/ObjectModel/RawBsonDocument.cs @@ -668,14 +668,18 @@ public override BsonValue GetValue(string name) /// /// The name of the element. /// The default value returned if the element is not found. + /// Force to use static serializer registry /// /// The value of the element or the default value if the element is not found. /// - public override BsonValue GetValue(string name, BsonValue defaultValue) + public override BsonValue GetValue( + string name, + BsonValue defaultValue, + bool forceStaticSerializerRegistry = false) { ThrowIfDisposed(); BsonValue value; - if (TryGetValue(name, out value)) + if (TryGetValue(name, out value, forceStaticSerializerRegistry)) { return value; } @@ -697,14 +701,19 @@ public override void InsertAt(int index, BsonElement element) /// Materializes the RawBsonDocument into a regular BsonDocument. /// /// The binary reader settings. + /// Force to use static serializer registry /// A BsonDocument. - public BsonDocument Materialize(BsonBinaryReaderSettings binaryReaderSettings) + public BsonDocument Materialize( + BsonBinaryReaderSettings binaryReaderSettings, + bool forceStaticSerializerRegistry = false) { ThrowIfDisposed(); using (var stream = new ByteBufferStream(_slice, ownsBuffer: false)) using (var reader = new BsonBinaryReader(stream, binaryReaderSettings)) { - var context = BsonDeserializationContext.CreateRoot(reader); + var context = BsonDeserializationContext.CreateRoot( + reader, + forceStaticSerializerRegistry: forceStaticSerializerRegistry); return BsonDocumentSerializer.Instance.Deserialize(context); } } @@ -853,16 +862,22 @@ public override bool TryGetElement(string name, out BsonElement element) /// /// The name of the element. /// The value of the element. + /// Force to use static serializer registry /// /// True if an element with that name was found. /// - public override bool TryGetValue(string name, out BsonValue value) + public override bool TryGetValue( + string name, + out BsonValue value, + bool forceStaticSerializerRegistry = false) { ThrowIfDisposed(); using (var stream = new ByteBufferStream(_slice, ownsBuffer: false)) using (var bsonReader = new BsonBinaryReader(stream, _readerSettings)) { - var context = BsonDeserializationContext.CreateRoot(bsonReader); + var context = BsonDeserializationContext.CreateRoot( + bsonReader, + forceStaticSerializerRegistry: forceStaticSerializerRegistry); bsonReader.ReadStartDocument(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) diff --git a/src/MongoDB.Bson/Serialization/BsonDeserializationArgs.cs b/src/MongoDB.Bson/Serialization/BsonDeserializationArgs.cs index 38cf52888b4..aeaf4d52b65 100644 --- a/src/MongoDB.Bson/Serialization/BsonDeserializationArgs.cs +++ b/src/MongoDB.Bson/Serialization/BsonDeserializationArgs.cs @@ -24,16 +24,28 @@ namespace Etherna.MongoDB.Bson.Serialization public struct BsonDeserializationArgs { // private fields + private bool _forceStaticSerializerRegistry; private Type _nominalType; // constructors private BsonDeserializationArgs( + bool forceStaticSerializerRegistry, Type nominalType) { + _forceStaticSerializerRegistry = forceStaticSerializerRegistry; _nominalType = nominalType; } // public properties + /// + /// Gets or sets when to force using of static serialization registry + /// + public bool ForceStaticSerializerRegistry + { + get { return _forceStaticSerializerRegistry; } + set { _forceStaticSerializerRegistry = value; } + } + /// /// Gets or sets the nominal type. /// diff --git a/src/MongoDB.Bson/Serialization/BsonDeserializationContext.cs b/src/MongoDB.Bson/Serialization/BsonDeserializationContext.cs index 0746b31ccbf..c87b21f1b00 100644 --- a/src/MongoDB.Bson/Serialization/BsonDeserializationContext.cs +++ b/src/MongoDB.Bson/Serialization/BsonDeserializationContext.cs @@ -93,14 +93,16 @@ public IBsonReader Reader /// /// The reader. /// The configurator. + /// Force to use static serializer registry /// /// A root context. /// public static BsonDeserializationContext CreateRoot( IBsonReader reader, - Action configurator = null) + Action configurator = null, + bool forceStaticSerializerRegistry = false) { - var builder = new Builder(null, reader); + var builder = new Builder(null, reader, forceStaticSerializerRegistry); if (configurator != null) { configurator(builder); @@ -113,13 +115,15 @@ public static BsonDeserializationContext CreateRoot( /// Creates a new context with some values changed. /// /// The configurator. + /// Force to use static serializer registry /// /// A new context. /// public BsonDeserializationContext With( - Action configurator = null) + Action configurator = null, + bool forceStaticSerializerRegistry = false) { - var builder = new Builder(this, _reader); + var builder = new Builder(this, _reader, forceStaticSerializerRegistry); if (configurator != null) { configurator(builder); @@ -140,7 +144,10 @@ public class Builder private IBsonReader _reader; // constructors - internal Builder(BsonDeserializationContext other, IBsonReader reader) + internal Builder( + BsonDeserializationContext other, + IBsonReader reader, + bool forceStaticSerializerRegistry) { if (reader == null) { @@ -156,8 +163,8 @@ internal Builder(BsonDeserializationContext other, IBsonReader reader) } else { - _dynamicArraySerializer = BsonDefaults.DynamicArraySerializer; - _dynamicDocumentSerializer = BsonDefaults.DynamicDocumentSerializer; + _dynamicArraySerializer = BsonDefaults.GetDynamicArraySerializer(forceStaticSerializerRegistry); + _dynamicDocumentSerializer = BsonDefaults.GetDynamicDocumentSerializer(forceStaticSerializerRegistry); } } diff --git a/src/MongoDB.Bson/Serialization/BsonSerializationArgs.cs b/src/MongoDB.Bson/Serialization/BsonSerializationArgs.cs index 5bfeb9ffe6a..94b5b668dc7 100644 --- a/src/MongoDB.Bson/Serialization/BsonSerializationArgs.cs +++ b/src/MongoDB.Bson/Serialization/BsonSerializationArgs.cs @@ -23,6 +23,7 @@ namespace Etherna.MongoDB.Bson.Serialization public struct BsonSerializationArgs { // private fields + private bool _forceStaticSerializerRegistry = false; private Type _nominalType; private bool _serializeAsNominalType; private bool _serializeIdFirst; @@ -45,6 +46,15 @@ public BsonSerializationArgs( } // public properties + /// + /// Gets or sets when to force using of static serialization registry + /// + public bool ForceStaticSerializerRegistry + { + get { return _forceStaticSerializerRegistry; } + set { _forceStaticSerializerRegistry = value; } + } + /// /// Gets or sets the nominal type. /// diff --git a/src/MongoDB.Bson/Serialization/BsonSerializationContext.cs b/src/MongoDB.Bson/Serialization/BsonSerializationContext.cs index 1f81629677c..07c1b614aa5 100644 --- a/src/MongoDB.Bson/Serialization/BsonSerializationContext.cs +++ b/src/MongoDB.Bson/Serialization/BsonSerializationContext.cs @@ -122,8 +122,8 @@ internal Builder(BsonSerializationContext other, IBsonWriter writer) else { _isDynamicType = t => - (BsonDefaults.DynamicArraySerializer != null && t == BsonDefaults.DynamicArraySerializer.ValueType) || - (BsonDefaults.DynamicDocumentSerializer != null && t == BsonDefaults.DynamicDocumentSerializer.ValueType); + (BsonDefaults.GetDynamicArraySerializer() != null && t == BsonDefaults.GetDynamicArraySerializer().ValueType) || + (BsonDefaults.GetDynamicDocumentSerializer() != null && t == BsonDefaults.GetDynamicDocumentSerializer().ValueType); } } diff --git a/src/MongoDB.Bson/Serialization/BsonSerializationProviderBase.cs b/src/MongoDB.Bson/Serialization/BsonSerializationProviderBase.cs index 63af9a56285..1ef761ef839 100644 --- a/src/MongoDB.Bson/Serialization/BsonSerializationProviderBase.cs +++ b/src/MongoDB.Bson/Serialization/BsonSerializationProviderBase.cs @@ -26,7 +26,7 @@ public abstract class BsonSerializationProviderBase : IRegistryAwareBsonSerializ /// public virtual IBsonSerializer GetSerializer(Type type) { - return GetSerializer(type, BsonSerializer.SerializerRegistry); + return GetSerializer(type, BsonSerializer.GetSerializerRegistry()); } /// @@ -40,7 +40,7 @@ public virtual IBsonSerializer GetSerializer(Type type) /// A serializer. protected virtual IBsonSerializer CreateGenericSerializer(Type serializerTypeDefinition, params Type[] typeArguments) { - return CreateGenericSerializer(serializerTypeDefinition, typeArguments, BsonSerializer.SerializerRegistry); + return CreateGenericSerializer(serializerTypeDefinition, typeArguments, BsonSerializer.GetSerializerRegistry()); } /// @@ -65,7 +65,7 @@ protected virtual IBsonSerializer CreateGenericSerializer(Type serializerTypeDef /// A serializer. protected virtual IBsonSerializer CreateSerializer(Type serializerType) { - return CreateSerializer(serializerType, BsonSerializer.SerializerRegistry); + return CreateSerializer(serializerType, BsonSerializer.GetSerializerRegistry()); } /// diff --git a/src/MongoDB.Bson/Serialization/BsonSerializer.cs b/src/MongoDB.Bson/Serialization/BsonSerializer.cs index a371f0faaef..1394e5e34a1 100644 --- a/src/MongoDB.Bson/Serialization/BsonSerializer.cs +++ b/src/MongoDB.Bson/Serialization/BsonSerializer.cs @@ -57,13 +57,6 @@ static BsonSerializer() } // public static properties - /// - /// Gets the serializer registry. - /// - public static IBsonSerializerRegistry SerializerRegistry => - __serializationContextAccessor?.TryGetCurrentBsonSerializerRegistry() ?? - __serializerRegistry; - /// /// Gets or sets whether to use the NullIdChecker on reference Id types that don't have an IdGenerator registered. /// @@ -269,6 +262,19 @@ public static object Deserialize(TextReader textReader, Type nominalType, Action } } + /// + /// Gets the serializer registry. + /// + public static IBsonSerializerRegistry GetSerializerRegistry( + bool forceStaticSerializerRegistry = false) + { + if (forceStaticSerializerRegistry) + return __serializerRegistry; + + return __serializationContextAccessor?.TryGetCurrentBsonSerializerRegistry() ?? + __serializerRegistry; + } + /// /// Returns whether the given type has any discriminators registered for any of its subclasses. /// @@ -485,20 +491,25 @@ public static IIdGenerator LookupIdGenerator(Type type) /// Looks up a serializer for a Type. /// /// The type. + /// Force to use static serializer registry /// A serializer for type T. - public static IBsonSerializer LookupSerializer() + public static IBsonSerializer LookupSerializer( + bool forceStaticSerializerRegistry = false) { - return (IBsonSerializer)LookupSerializer(typeof(T)); + return (IBsonSerializer)LookupSerializer(typeof(T), forceStaticSerializerRegistry); } /// /// Looks up a serializer for a Type. /// /// The Type. + /// Force to use static serializer registry /// A serializer for the Type. - public static IBsonSerializer LookupSerializer(Type type) + public static IBsonSerializer LookupSerializer( + Type type, + bool forceStaticSerializerRegistry = false) { - return SerializerRegistry.GetSerializer(type); + return GetSerializerRegistry(forceStaticSerializerRegistry).GetSerializer(type); } /// @@ -613,7 +624,7 @@ public static void Serialize( BsonSerializationArgs args = default(BsonSerializationArgs)) { args.SetOrValidateNominalType(typeof(TNominalType), ""); - var serializer = LookupSerializer(); + var serializer = LookupSerializer(args.ForceStaticSerializerRegistry); var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator); serializer.Serialize(context, args, value); } @@ -634,7 +645,7 @@ public static void Serialize( BsonSerializationArgs args = default(BsonSerializationArgs)) { args.SetOrValidateNominalType(nominalType, "nominalType"); - var serializer = LookupSerializer(nominalType); + var serializer = LookupSerializer(nominalType, args.ForceStaticSerializerRegistry); var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator); serializer.Serialize(context, args, value); } diff --git a/src/MongoDB.Bson/Serialization/IBsonSerializerExtensions.cs b/src/MongoDB.Bson/Serialization/IBsonSerializerExtensions.cs index dac3026b252..a347b573be7 100644 --- a/src/MongoDB.Bson/Serialization/IBsonSerializerExtensions.cs +++ b/src/MongoDB.Bson/Serialization/IBsonSerializerExtensions.cs @@ -55,9 +55,18 @@ public static TValue Deserialize(this IBsonSerializer serializer /// The serializer. /// The serialization context. /// The value. - public static void Serialize(this IBsonSerializer serializer, BsonSerializationContext context, object value) + /// Force to use static serializer registry + public static void Serialize( + this IBsonSerializer serializer, + BsonSerializationContext context, + object value, + bool forceStaticSerializerRegistry = false) { - var args = new BsonSerializationArgs { NominalType = serializer.ValueType }; + var args = new BsonSerializationArgs + { + ForceStaticSerializerRegistry = forceStaticSerializerRegistry, + NominalType = serializer.ValueType + }; serializer.Serialize(context, args, value); } @@ -68,9 +77,18 @@ public static void Serialize(this IBsonSerializer serializer, BsonSerializationC /// The serializer. /// The serialization context. /// The value. - public static void Serialize(this IBsonSerializer serializer, BsonSerializationContext context, TValue value) + /// Force to use static serializer registry + public static void Serialize( + this IBsonSerializer serializer, + BsonSerializationContext context, + TValue value, + bool forceStaticSerializerRegistry = false) { - var args = new BsonSerializationArgs { NominalType = serializer.ValueType }; + var args = new BsonSerializationArgs + { + ForceStaticSerializerRegistry = forceStaticSerializerRegistry, + NominalType = serializer.ValueType + }; serializer.Serialize(context, args, value); } diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs index 144ff3b3d36..c55284c1fe5 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs @@ -92,7 +92,7 @@ public override TClass Deserialize(BsonDeserializationContext context, BsonDeser return DeserializeClass(context); } - var serializer = BsonSerializer.LookupSerializer(actualType); + var serializer = BsonSerializer.LookupSerializer(actualType, args.ForceStaticSerializerRegistry); return (TClass)serializer.Deserialize(context); } @@ -388,7 +388,7 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati return; } - var serializer = BsonSerializer.LookupSerializer(actualType); + var serializer = BsonSerializer.LookupSerializer(actualType, args.ForceStaticSerializerRegistry); serializer.Serialize(context, args, value); } diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonDocumentSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonDocumentSerializer.cs index 7d55e483af0..a3c27714e54 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonDocumentSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonDocumentSerializer.cs @@ -172,7 +172,7 @@ protected override void SerializeValue(BsonSerializationContext context, BsonSer var element = value.GetElement(index); bsonWriter.WriteName(element.Name); - BsonValueSerializer.Instance.Serialize(context, element.Value); + BsonValueSerializer.Instance.Serialize(context, element.Value, args.ForceStaticSerializerRegistry); } bsonWriter.WriteEndDocument(); diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonDocumentWrapperSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonDocumentWrapperSerializer.cs index f640f2cfeac..19d355a0e40 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonDocumentWrapperSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonDocumentWrapperSerializer.cs @@ -73,7 +73,7 @@ protected override BsonDocumentWrapper DeserializeValue(BsonDeserializationConte /// The object. protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, BsonDocumentWrapper value) { - value.Serializer.Serialize(context, value.Wrapped); + value.Serializer.Serialize(context, value.Wrapped, args.ForceStaticSerializerRegistry); } } } diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonValueSerializerBase.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonValueSerializerBase.cs index 31e4e921ec6..fc412f173ec 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonValueSerializerBase.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonValueSerializerBase.cs @@ -70,8 +70,8 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati var actualType = value.GetType(); if (actualType != ValueType && !args.SerializeAsNominalType) { - var serializer = BsonSerializer.LookupSerializer(actualType); - serializer.Serialize(context, value); + var serializer = BsonSerializer.LookupSerializer(actualType, args.ForceStaticSerializerRegistry); + serializer.Serialize(context, value, args.ForceStaticSerializerRegistry); return; } diff --git a/src/MongoDB.Bson/Serialization/Serializers/ClassSerializerBase.cs b/src/MongoDB.Bson/Serialization/Serializers/ClassSerializerBase.cs index 817a15100dd..302149cf10e 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/ClassSerializerBase.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/ClassSerializerBase.cs @@ -47,7 +47,7 @@ public override TValue Deserialize(BsonDeserializationContext context, BsonDeser } else { - var serializer = BsonSerializer.LookupSerializer(actualType); + var serializer = BsonSerializer.LookupSerializer(actualType, args.ForceStaticSerializerRegistry); return (TValue)serializer.Deserialize(context, args); } } @@ -75,7 +75,7 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati } else { - var serializer = BsonSerializer.LookupSerializer(actualType); + var serializer = BsonSerializer.LookupSerializer(actualType, args.ForceStaticSerializerRegistry); serializer.Serialize(context, value); } } diff --git a/src/MongoDB.Bson/Serialization/Serializers/DictionarySerializerBase.cs b/src/MongoDB.Bson/Serialization/Serializers/DictionarySerializerBase.cs index b537d38f112..90e4c21e8e7 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/DictionarySerializerBase.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/DictionarySerializerBase.cs @@ -386,7 +386,7 @@ public DictionarySerializerBase() /// /// The dictionary representation. public DictionarySerializerBase(DictionaryRepresentation dictionaryRepresentation) - : this(dictionaryRepresentation, BsonSerializer.SerializerRegistry) + : this(dictionaryRepresentation, BsonSerializer.GetSerializerRegistry()) { } diff --git a/src/MongoDB.Bson/Serialization/Serializers/DiscriminatedInterfaceSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/DiscriminatedInterfaceSerializer.cs index f4bd84afbe0..2ce84657541 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/DiscriminatedInterfaceSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/DiscriminatedInterfaceSerializer.cs @@ -146,7 +146,7 @@ public override TInterface Deserialize(BsonDeserializationContext context, BsonD throw new FormatException(message); } - var serializer = BsonSerializer.LookupSerializer(actualType); + var serializer = BsonSerializer.LookupSerializer(actualType, args.ForceStaticSerializerRegistry); return (TInterface)serializer.Deserialize(context, args); } } diff --git a/src/MongoDB.Bson/Serialization/Serializers/DiscriminatedWrapperSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/DiscriminatedWrapperSerializer.cs index ac133573999..ecd3299c3de 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/DiscriminatedWrapperSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/DiscriminatedWrapperSerializer.cs @@ -74,7 +74,7 @@ public override TValue Deserialize(BsonDeserializationContext context, BsonDeser var bsonReader = context.Reader; var nominalType = args.NominalType; var actualType = _discriminatorConvention.GetActualType(bsonReader, nominalType); - var serializer = BsonSerializer.LookupSerializer(actualType); + var serializer = BsonSerializer.LookupSerializer(actualType, args.ForceStaticSerializerRegistry); TValue value = default(TValue); _helper.DeserializeMembers(context, (elementName, flag) => diff --git a/src/MongoDB.Bson/Serialization/Serializers/EnumerableSerializerBase.cs b/src/MongoDB.Bson/Serialization/Serializers/EnumerableSerializerBase.cs index d56118d9849..664cd88ca5b 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/EnumerableSerializerBase.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/EnumerableSerializerBase.cs @@ -35,7 +35,7 @@ public abstract class EnumerableSerializerBase : SerializerBase, /// Initializes a new instance of the class. /// protected EnumerableSerializerBase() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } @@ -230,7 +230,7 @@ public abstract class EnumerableSerializerBase : SerializerBase class. /// protected EnumerableSerializerBase() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } diff --git a/src/MongoDB.Bson/Serialization/Serializers/IEnumerableDeserializingAsCollectionSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/IEnumerableDeserializingAsCollectionSerializer.cs index 642b2c87102..4db46d5c23a 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/IEnumerableDeserializingAsCollectionSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/IEnumerableDeserializingAsCollectionSerializer.cs @@ -50,7 +50,7 @@ private static void EnsureTIEnumerableIsAnInterface() /// Initializes a new instance of the IEnumerableDeserializingAsCollectionSerializer class. /// public IEnumerableDeserializingAsCollectionSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } diff --git a/src/MongoDB.Bson/Serialization/Serializers/ImpliedImplementationInterfaceSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/ImpliedImplementationInterfaceSerializer.cs index 2269e86e2c5..56cefcc0239 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/ImpliedImplementationInterfaceSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/ImpliedImplementationInterfaceSerializer.cs @@ -52,7 +52,7 @@ public class ImpliedImplementationInterfaceSerializer class. /// public ImpliedImplementationInterfaceSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } @@ -279,7 +279,7 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati } else { - var serializer = BsonSerializer.LookupSerializer(actualType); + var serializer = BsonSerializer.LookupSerializer(actualType, args.ForceStaticSerializerRegistry); serializer.Serialize(context, value); } } diff --git a/src/MongoDB.Bson/Serialization/Serializers/KeyValuePairSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/KeyValuePairSerializer.cs index b3ef9691662..6e01a538af8 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/KeyValuePairSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/KeyValuePairSerializer.cs @@ -66,7 +66,7 @@ public KeyValuePairSerializer() /// /// The representation. public KeyValuePairSerializer(BsonType representation) - : this(representation, BsonSerializer.SerializerRegistry) + : this(representation, BsonSerializer.GetSerializerRegistry()) { } diff --git a/src/MongoDB.Bson/Serialization/Serializers/NullableSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/NullableSerializer.cs index 4956c060952..72e115445a5 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/NullableSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/NullableSerializer.cs @@ -64,7 +64,7 @@ public class NullableSerializer : /// Initializes a new instance of the class. /// public NullableSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } diff --git a/src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs index f4a639adcb2..d52ff4265b4 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/ObjectSerializer.cs @@ -403,7 +403,7 @@ private object DeserializeDiscriminatedValue(BsonDeserializationContext context, } else { - var serializer = BsonSerializer.LookupSerializer(actualType); + var serializer = BsonSerializer.LookupSerializer(actualType, args.ForceStaticSerializerRegistry); var polymorphicSerializer = serializer as IBsonPolymorphicSerializer; if (polymorphicSerializer != null && polymorphicSerializer.IsDiscriminatorCompatibleWithObjectSerializer) { @@ -452,7 +452,7 @@ private void SerializeDiscriminatedValue(BsonSerializationContext context, BsonS throw new BsonSerializationException($"Type {actualType.FullName} is not configured as a type that is allowed to be serialized for this instance of ObjectSerializer."); } - var serializer = BsonSerializer.LookupSerializer(actualType); + var serializer = BsonSerializer.LookupSerializer(actualType, args.ForceStaticSerializerRegistry); var polymorphicSerializer = serializer as IBsonPolymorphicSerializer; if (polymorphicSerializer != null && polymorphicSerializer.IsDiscriminatorCompatibleWithObjectSerializer) diff --git a/src/MongoDB.Bson/Serialization/Serializers/SerializeAsNominalTypeSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/SerializeAsNominalTypeSerializer.cs index 8837426ba7d..6ecec675753 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/SerializeAsNominalTypeSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/SerializeAsNominalTypeSerializer.cs @@ -32,7 +32,7 @@ public class SerializeAsNominalTypeSerializer : Seria /// Initializes a new instance of the class. /// public SerializeAsNominalTypeSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } diff --git a/src/MongoDB.Bson/Serialization/Serializers/ThreeDimensionalArraySerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/ThreeDimensionalArraySerializer.cs index af191a3bf14..3ae3ef4da97 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/ThreeDimensionalArraySerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/ThreeDimensionalArraySerializer.cs @@ -34,7 +34,7 @@ public class ThreeDimensionalArraySerializer : /// Initializes a new instance of the class. /// public ThreeDimensionalArraySerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } diff --git a/src/MongoDB.Bson/Serialization/Serializers/TupleSerializers.cs b/src/MongoDB.Bson/Serialization/Serializers/TupleSerializers.cs index 8e52d61b32c..0fb3c2ed1a6 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/TupleSerializers.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/TupleSerializers.cs @@ -114,7 +114,7 @@ public class TupleSerializer : SealedClassSerializerBase>, IBsonTu /// Initializes a new instance of the class. /// public TupleSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } @@ -208,7 +208,7 @@ public class TupleSerializer : SealedClassSerializerBase>, /// Initializes a new instance of the class. /// public TupleSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } @@ -318,7 +318,7 @@ public class TupleSerializer : SealedClassSerializerBase class. /// public TupleSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } @@ -443,7 +443,7 @@ public class TupleSerializer : SealedClassSerializerBase class. /// public TupleSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } @@ -585,7 +585,7 @@ public class TupleSerializer : SealedClassSerializerBase class. /// public TupleSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } @@ -743,7 +743,7 @@ public class TupleSerializer : SealedClassSerializerBase /// Initializes a new instance of the class. /// public TupleSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } @@ -917,7 +917,7 @@ public class TupleSerializer : SealedClassSerializer /// Initializes a new instance of the class. /// public TupleSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } @@ -1107,7 +1107,7 @@ public class TupleSerializer : SealedClassSer /// Initializes a new instance of the class. /// public TupleSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } diff --git a/src/MongoDB.Bson/Serialization/Serializers/TwoDimensionalArraySerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/TwoDimensionalArraySerializer.cs index 3b19540e3df..d7b378878fb 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/TwoDimensionalArraySerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/TwoDimensionalArraySerializer.cs @@ -34,7 +34,7 @@ public class TwoDimensionalArraySerializer : /// Initializes a new instance of the class. /// public TwoDimensionalArraySerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } diff --git a/src/MongoDB.Bson/Serialization/Serializers/UndiscriminatedActualTypeSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/UndiscriminatedActualTypeSerializer.cs index f35afc54521..25cdb7e8d59 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/UndiscriminatedActualTypeSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/UndiscriminatedActualTypeSerializer.cs @@ -62,7 +62,7 @@ public override void Serialize(BsonSerializationContext context, BsonSerializati else { var actualType = value.GetType(); - var serializer = BsonSerializer.LookupSerializer(actualType); + var serializer = BsonSerializer.LookupSerializer(actualType, args.ForceStaticSerializerRegistry); args.NominalType = actualType; serializer.Serialize(context, args, value); } diff --git a/src/MongoDB.Bson/Serialization/Serializers/ValueTupleSerializers.cs b/src/MongoDB.Bson/Serialization/Serializers/ValueTupleSerializers.cs index 5bbed23d9c0..a0784567d51 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/ValueTupleSerializers.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/ValueTupleSerializers.cs @@ -75,7 +75,7 @@ public sealed class ValueTupleSerializer : StructSerializerBase class. /// public ValueTupleSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } @@ -193,7 +193,7 @@ public sealed class ValueTupleSerializer : StructSerializerBase class. /// public ValueTupleSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } @@ -329,7 +329,7 @@ public sealed class ValueTupleSerializer : StructSerializerBase class. /// public ValueTupleSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } @@ -483,7 +483,7 @@ public sealed class ValueTupleSerializer : StructSerializerBase< /// Initializes a new instance of the class. /// public ValueTupleSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } @@ -655,7 +655,7 @@ public sealed class ValueTupleSerializer : StructSerializerB /// Initializes a new instance of the class. /// public ValueTupleSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } @@ -845,7 +845,7 @@ public sealed class ValueTupleSerializer : StructSeriali /// Initializes a new instance of the class. /// public ValueTupleSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } @@ -1053,7 +1053,7 @@ public sealed class ValueTupleSerializer : StructSer /// Initializes a new instance of the class. /// public ValueTupleSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } @@ -1280,7 +1280,7 @@ public sealed class ValueTupleSerializer : St /// Initializes a new instance of the class. /// public ValueTupleSerializer() - : this(BsonSerializer.SerializerRegistry) + : this(BsonSerializer.GetSerializerRegistry()) { } diff --git a/src/MongoDB.Driver.Core/Core/Connections/BinaryConnection.cs b/src/MongoDB.Driver.Core/Core/Connections/BinaryConnection.cs index df586fb5a2e..6a9f6dde73d 100644 --- a/src/MongoDB.Driver.Core/Core/Connections/BinaryConnection.cs +++ b/src/MongoDB.Driver.Core/Core/Connections/BinaryConnection.cs @@ -936,7 +936,7 @@ public ResponseMessage DecodeMessage(IByteBuffer buffer, IMessageEncoderSelector else { var encoder = encoderSelector.GetEncoder(encoderFactory); - message = (ResponseMessage)encoder.ReadMessage(); + message = (ResponseMessage)encoder.ReadMessage(true); } } _stopwatch.Stop(); @@ -1018,7 +1018,7 @@ public IByteBuffer EncodeMessages(CancellationToken cancellationToken, out List< if (message.ShouldBeSent == null || message.ShouldBeSent()) { var encoder = message.GetEncoder(encoderFactory); - encoder.WriteMessage(message); + encoder.WriteMessage(message, true); message.WasSent = true; sentMessages.Add(message); } diff --git a/src/MongoDB.Driver.Core/Core/WireProtocol/CommandUsingQueryMessageWireProtocol.cs b/src/MongoDB.Driver.Core/Core/WireProtocol/CommandUsingQueryMessageWireProtocol.cs index 671005e6ca7..0e520e25e5c 100644 --- a/src/MongoDB.Driver.Core/Core/WireProtocol/CommandUsingQueryMessageWireProtocol.cs +++ b/src/MongoDB.Driver.Core/Core/WireProtocol/CommandUsingQueryMessageWireProtocol.cs @@ -263,22 +263,22 @@ private TCommandResult ProcessReply(ConnectionId connectionId, ReplyMessage)encoderFactory.GetReplyMessageEncoder(_resultSerializer); using (var reader = encoder.CreateBinaryReader()) { - var context = BsonDeserializationContext.CreateRoot(reader); + var context = BsonDeserializationContext.CreateRoot(reader, forceStaticSerializerRegistry: true); return _resultSerializer.Deserialize(context); } } diff --git a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/CommandMessageBinaryEncoder.cs b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/CommandMessageBinaryEncoder.cs index c6082c10fa9..28ee8b586a4 100644 --- a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/CommandMessageBinaryEncoder.cs +++ b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/CommandMessageBinaryEncoder.cs @@ -101,12 +101,15 @@ public void WriteMessage(CommandMessage message) } // explicit interface implementations - MongoDBMessage IMessageEncoder.ReadMessage() + MongoDBMessage IMessageEncoder.ReadMessage( + bool forceStaticSerializerRegistry) { return ReadMessage(); } - void IMessageEncoder.WriteMessage(MongoDBMessage message) + void IMessageEncoder.WriteMessage( + MongoDBMessage message, + bool forceStaticSerializerRegistry) { WriteMessage((CommandMessage)message); } diff --git a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/CommandRequestMessageBinaryEncoder.cs b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/CommandRequestMessageBinaryEncoder.cs index efae7294d34..11222ab3aaa 100644 --- a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/CommandRequestMessageBinaryEncoder.cs +++ b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/CommandRequestMessageBinaryEncoder.cs @@ -60,12 +60,15 @@ public void WriteMessage(CommandRequestMessage message) } // explicit interface implementations - MongoDBMessage IMessageEncoder.ReadMessage() + MongoDBMessage IMessageEncoder.ReadMessage( + bool forceStaticSerializerRegistry) { return ReadMessage(); } - void IMessageEncoder.WriteMessage(MongoDBMessage message) + void IMessageEncoder.WriteMessage( + MongoDBMessage message, + bool forceStaticSerializerRegistry) { WriteMessage((CommandRequestMessage)message); } diff --git a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/CommandResponseMessageBinaryEncoder.cs b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/CommandResponseMessageBinaryEncoder.cs index 5a5192a813a..488af2806fb 100644 --- a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/CommandResponseMessageBinaryEncoder.cs +++ b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/CommandResponseMessageBinaryEncoder.cs @@ -58,12 +58,15 @@ public void WriteMessage(CommandResponseMessage message) } // explicit interface implementations - MongoDBMessage IMessageEncoder.ReadMessage() + MongoDBMessage IMessageEncoder.ReadMessage( + bool forceStaticSerializerRegistry) { return ReadMessage(); } - void IMessageEncoder.WriteMessage(MongoDBMessage message) + void IMessageEncoder.WriteMessage( + MongoDBMessage message, + bool forceStaticSerializerRegistry) { WriteMessage((CommandResponseMessage)message); } diff --git a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/CompressedMessageBinaryEncoder.cs b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/CompressedMessageBinaryEncoder.cs index 0732e941cd3..ad909bfa951 100644 --- a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/CompressedMessageBinaryEncoder.cs +++ b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/CompressedMessageBinaryEncoder.cs @@ -122,12 +122,15 @@ public void WriteMessage(CompressedMessage message) stream.BackpatchSize(messageStartPosition); } - MongoDBMessage IMessageEncoder.ReadMessage() + MongoDBMessage IMessageEncoder.ReadMessage( + bool forceStaticSerializerRegistry) { return ReadMessage(); } - void IMessageEncoder.WriteMessage(MongoDBMessage message) + void IMessageEncoder.WriteMessage( + MongoDBMessage message, + bool forceStaticSerializerRegistry) { WriteMessage((CompressedMessage)message); } diff --git a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/QueryMessageBinaryEncoder.cs b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/QueryMessageBinaryEncoder.cs index 0eabf61b7af..fb2b4a19fb1 100644 --- a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/QueryMessageBinaryEncoder.cs +++ b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/QueryMessageBinaryEncoder.cs @@ -136,7 +136,10 @@ internal QueryMessage ReadMessage(IBsonSerializer serializ /// Writes the message. /// /// The message. - public void WriteMessage(QueryMessage message) + /// Force to use static serializer registry + public void WriteMessage( + QueryMessage message, + bool forceStaticSerializerRegistry = false) { Ensure.IsNotNull(message, nameof(message)); @@ -152,7 +155,7 @@ public void WriteMessage(QueryMessage message) stream.WriteCString(message.CollectionNamespace.FullName); stream.WriteInt32(message.Skip); stream.WriteInt32(message.BatchSize); - WriteQuery(binaryWriter, message.Query, message.QueryValidator); + WriteQuery(binaryWriter, message.Query, message.QueryValidator, forceStaticSerializerRegistry); WriteOptionalFields(binaryWriter, message.Fields); stream.BackpatchSize(messageStartPosition); @@ -177,7 +180,11 @@ private void WriteOptionalFields(BsonBinaryWriter binaryWriter, BsonDocument fie } } - private void WriteQuery(BsonBinaryWriter binaryWriter, BsonDocument query, IElementNameValidator queryValidator) + private void WriteQuery( + BsonBinaryWriter binaryWriter, + BsonDocument query, + IElementNameValidator queryValidator, + bool forceStaticSerializerRegistry = false) { var maxWireDocumentSize = MaxWireDocumentSize ?? MaxDocumentSize ?? binaryWriter.Settings.MaxDocumentSize; @@ -186,7 +193,7 @@ private void WriteQuery(BsonBinaryWriter binaryWriter, BsonDocument query, IElem try { var context = BsonSerializationContext.CreateRoot(binaryWriter); - BsonDocumentSerializer.Instance.Serialize(context, query ?? new BsonDocument()); + BsonDocumentSerializer.Instance.Serialize(context, query ?? new BsonDocument(), forceStaticSerializerRegistry); } finally { @@ -196,14 +203,17 @@ private void WriteQuery(BsonBinaryWriter binaryWriter, BsonDocument query, IElem } // explicit interface implementations - MongoDBMessage IMessageEncoder.ReadMessage() + MongoDBMessage IMessageEncoder.ReadMessage( + bool forceStaticSerializerRegistry) { return ReadMessage(); } - void IMessageEncoder.WriteMessage(MongoDBMessage message) + void IMessageEncoder.WriteMessage( + MongoDBMessage message, + bool forceStaticSerializerRegistry) { - WriteMessage((QueryMessage)message); + WriteMessage((QueryMessage)message, forceStaticSerializerRegistry); } // nested types diff --git a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/ReplyMessageBinaryEncoder.cs b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/ReplyMessageBinaryEncoder.cs index 35b43611eaa..55a4de97b76 100644 --- a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/ReplyMessageBinaryEncoder.cs +++ b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/BinaryEncoders/ReplyMessageBinaryEncoder.cs @@ -51,7 +51,8 @@ public ReplyMessageBinaryEncoder(Stream stream, MessageEncoderSettings encoderSe /// Reads the message. /// /// A message. - public ReplyMessage ReadMessage() + public ReplyMessage ReadMessage( + bool forceStaticSerializerRegistry = false) { var binaryReader = CreateBinaryReader(); var stream = binaryReader.BsonStream; @@ -84,9 +85,10 @@ public ReplyMessage ReadMessage() { var allowDuplicateElementNames = typeof(TDocument) == typeof(BsonDocument); var context = BsonDeserializationContext.CreateRoot(binaryReader, builder => - { - builder.AllowDuplicateElementNames = allowDuplicateElementNames; - }); + { + builder.AllowDuplicateElementNames = allowDuplicateElementNames; + }, + forceStaticSerializerRegistry); documents.Add(_serializer.Deserialize(context)); } } @@ -166,12 +168,15 @@ private void EnsureOpcodeIsValid(Opcode opcode) } // explicit interface implementations - MongoDBMessage IMessageEncoder.ReadMessage() + MongoDBMessage IMessageEncoder.ReadMessage( + bool forceStaticSerializerRegistry) { - return ReadMessage(); + return ReadMessage(forceStaticSerializerRegistry); } - void IMessageEncoder.WriteMessage(MongoDBMessage message) + void IMessageEncoder.WriteMessage( + MongoDBMessage message, + bool forceStaticSerializerRegistry) { WriteMessage((ReplyMessage)message); } diff --git a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/IMessageEncoder.cs b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/IMessageEncoder.cs index f9ad4d704cb..6b9919b791c 100644 --- a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/IMessageEncoder.cs +++ b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/IMessageEncoder.cs @@ -29,13 +29,18 @@ public interface IMessageEncoder /// /// Reads the message. /// + /// Force to use static serializer registry /// A message. - MongoDBMessage ReadMessage(); + MongoDBMessage ReadMessage( + bool forceStaticSerializerRegistry = false); /// /// Writes the message. /// /// The message. - void WriteMessage(MongoDBMessage message); + /// Force to use static serializer registry + void WriteMessage( + MongoDBMessage message, + bool forceStaticSerializerRegistry = false); } } diff --git a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/CommandMessageJsonEncoder.cs b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/CommandMessageJsonEncoder.cs index 5a347566d1f..4353f6f54ab 100644 --- a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/CommandMessageJsonEncoder.cs +++ b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/CommandMessageJsonEncoder.cs @@ -99,12 +99,15 @@ public void WriteMessage(CommandMessage message) } // explicit interface implementations - MongoDBMessage IMessageEncoder.ReadMessage() + MongoDBMessage IMessageEncoder.ReadMessage( + bool forceStaticSerializerRegistry) { return ReadMessage(); } - void IMessageEncoder.WriteMessage(MongoDBMessage message) + void IMessageEncoder.WriteMessage( + MongoDBMessage message, + bool forceStaticSerializerRegistry) { WriteMessage((CommandMessage)message); } diff --git a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/CommandRequestMessageJsonEncoder.cs b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/CommandRequestMessageJsonEncoder.cs index 2ee94fa94ac..6e06cf87553 100644 --- a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/CommandRequestMessageJsonEncoder.cs +++ b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/CommandRequestMessageJsonEncoder.cs @@ -58,12 +58,15 @@ public void WriteMessage(CommandRequestMessage message) } // explicit interface implementations - MongoDBMessage IMessageEncoder.ReadMessage() + MongoDBMessage IMessageEncoder.ReadMessage( + bool forceStaticSerializerRegistry) { return ReadMessage(); } - void IMessageEncoder.WriteMessage(MongoDBMessage message) + void IMessageEncoder.WriteMessage( + MongoDBMessage message, + bool forceStaticSerializerRegistry) { WriteMessage((CommandRequestMessage)message); } diff --git a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/CommandResponseMessageJsonEncoder.cs b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/CommandResponseMessageJsonEncoder.cs index 78b4f90a957..d62dc5e9b5c 100644 --- a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/CommandResponseMessageJsonEncoder.cs +++ b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/CommandResponseMessageJsonEncoder.cs @@ -58,12 +58,15 @@ public void WriteMessage(CommandResponseMessage message) } // explicit interface implementations - MongoDBMessage IMessageEncoder.ReadMessage() + MongoDBMessage IMessageEncoder.ReadMessage( + bool forceStaticSerializerRegistry) { return ReadMessage(); } - void IMessageEncoder.WriteMessage(MongoDBMessage message) + void IMessageEncoder.WriteMessage( + MongoDBMessage message, + bool forceStaticSerializerRegistry) { WriteMessage((CommandResponseMessage)message); } diff --git a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/CompressedMessageJsonEncoder.cs b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/CompressedMessageJsonEncoder.cs index 065bccb0a25..e311327c552 100644 --- a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/CompressedMessageJsonEncoder.cs +++ b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/CompressedMessageJsonEncoder.cs @@ -104,13 +104,16 @@ public void WriteMessage(CompressedMessage message) } /// - MongoDBMessage IMessageEncoder.ReadMessage() + MongoDBMessage IMessageEncoder.ReadMessage( + bool forceStaticSerializerRegistry) { return ReadMessage(); } /// - void IMessageEncoder.WriteMessage(MongoDBMessage message) + void IMessageEncoder.WriteMessage( + MongoDBMessage message, + bool forceStaticSerializerRegistry) { WriteMessage((CompressedMessage)message); } diff --git a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/QueryMessageJsonEncoder.cs b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/QueryMessageJsonEncoder.cs index ac5c1216b53..e42ca7a4aee 100644 --- a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/QueryMessageJsonEncoder.cs +++ b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/QueryMessageJsonEncoder.cs @@ -123,12 +123,15 @@ public void WriteMessage(QueryMessage message) } // explicit interface implementations - MongoDBMessage IMessageEncoder.ReadMessage() + MongoDBMessage IMessageEncoder.ReadMessage( + bool forceStaticSerializerRegistry) { return ReadMessage(); } - void IMessageEncoder.WriteMessage(MongoDBMessage message) + void IMessageEncoder.WriteMessage( + MongoDBMessage message, + bool forceStaticSerializerRegistry) { WriteMessage((QueryMessage)message); } diff --git a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/ReplyMessageJsonEncoder.cs b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/ReplyMessageJsonEncoder.cs index aea9d04ce28..56a78768490 100644 --- a/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/ReplyMessageJsonEncoder.cs +++ b/src/MongoDB.Driver.Core/Core/WireProtocol/Messages/Encoders/JsonEncoders/ReplyMessageJsonEncoder.cs @@ -147,12 +147,15 @@ public void WriteMessage(ReplyMessage message) } // explicit interface implementations - MongoDBMessage IMessageEncoder.ReadMessage() + MongoDBMessage IMessageEncoder.ReadMessage( + bool forceStaticSerializerRegistry) { return ReadMessage(); } - void IMessageEncoder.WriteMessage(MongoDBMessage message) + void IMessageEncoder.WriteMessage( + MongoDBMessage message, + bool forceStaticSerializerRegistry) { WriteMessage((ReplyMessage)message); } diff --git a/src/MongoDB.Driver.GridFS/GridFSBucketCompat.cs b/src/MongoDB.Driver.GridFS/GridFSBucketCompat.cs index a186203360e..2286f95ea99 100644 --- a/src/MongoDB.Driver.GridFS/GridFSBucketCompat.cs +++ b/src/MongoDB.Driver.GridFS/GridFSBucketCompat.cs @@ -242,7 +242,7 @@ private IEnumerable TransformFileInfos(IEnumerable> WrapFilter(FilterDefinition filter) { var linqProvider = Database.Client.Settings.LinqProvider; - var renderedFilter = filter.Render(GridFSFileInfoSerializer.Instance, BsonSerializer.SerializerRegistry, linqProvider); + var renderedFilter = filter.Render(GridFSFileInfoSerializer.Instance, BsonSerializer.GetSerializerRegistry(), linqProvider); return new BsonDocumentFilterDefinition>(renderedFilter); } @@ -251,7 +251,7 @@ private GridFSFindOptions WrapFindOptions(GridFSFindOptions options) if (options != null) { var linqProvider = Database.Client.Settings.LinqProvider; - var renderedSort = options.Sort == null ? null : options.Sort.Render(GridFSFileInfoSerializer.Instance, BsonSerializer.SerializerRegistry, linqProvider); + var renderedSort = options.Sort == null ? null : options.Sort.Render(GridFSFileInfoSerializer.Instance, BsonSerializer.GetSerializerRegistry(), linqProvider); var wrappedSort = renderedSort == null ? null : new BsonDocumentSortDefinition>(renderedSort); return new GridFSFindOptions { diff --git a/src/MongoDB.Driver.GridFS/GridFSBucketOptions.cs b/src/MongoDB.Driver.GridFS/GridFSBucketOptions.cs index 86e3b83d06c..1416dead0da 100644 --- a/src/MongoDB.Driver.GridFS/GridFSBucketOptions.cs +++ b/src/MongoDB.Driver.GridFS/GridFSBucketOptions.cs @@ -272,7 +272,7 @@ public ReadPreference ReadPreference /// public IBsonSerializerRegistry SerializerRegistry { - get { return BsonSerializer.SerializerRegistry; } + get { return BsonSerializer.GetSerializerRegistry(); } } /// diff --git a/src/MongoDB.Driver/ChangeStreamHelper.cs b/src/MongoDB.Driver/ChangeStreamHelper.cs index 846c48513c4..bce62023dba 100644 --- a/src/MongoDB.Driver/ChangeStreamHelper.cs +++ b/src/MongoDB.Driver/ChangeStreamHelper.cs @@ -136,7 +136,7 @@ private static RenderedPipelineDefinition RenderPipeline(documentSerializer); - var serializerRegistry = BsonSerializer.SerializerRegistry; + var serializerRegistry = BsonSerializer.GetSerializerRegistry(); return pipeline.Render(changeStreamDocumentSerializer, serializerRegistry, linqProvider); } diff --git a/src/MongoDB.Driver/Encryption/ExplicitEncryptionLibMongoCryptController.cs b/src/MongoDB.Driver/Encryption/ExplicitEncryptionLibMongoCryptController.cs index 6557eeba35c..b50706f40d1 100644 --- a/src/MongoDB.Driver/Encryption/ExplicitEncryptionLibMongoCryptController.cs +++ b/src/MongoDB.Driver/Encryption/ExplicitEncryptionLibMongoCryptController.cs @@ -551,7 +551,7 @@ private byte[] GetWrappedValueBytes(BsonValue value) private BsonValue RenderFilter(FilterDefinition filter) { - var registry = BsonSerializer.SerializerRegistry; + var registry = BsonSerializer.GetSerializerRegistry(); var serializer = registry.GetSerializer(); return filter.Render(serializer, registry); } diff --git a/src/MongoDB.Driver/FieldValueSerializerHelper.cs b/src/MongoDB.Driver/FieldValueSerializerHelper.cs index ebfc5673e09..793ed83d3ab 100644 --- a/src/MongoDB.Driver/FieldValueSerializerHelper.cs +++ b/src/MongoDB.Driver/FieldValueSerializerHelper.cs @@ -52,7 +52,7 @@ public static IBsonSerializer GetSerializerForValueType(IBsonSerializer fieldSer // serialize numeric values without converting them if (fieldType.IsNumeric() && valueType.IsNumeric()) { - var valueSerializer = BsonSerializer.SerializerRegistry.GetSerializer(valueType); + var valueSerializer = BsonSerializer.GetSerializerRegistry().GetSerializer(valueType); if (HasStringRepresentation(fieldSerializer)) { valueSerializer = WithStringRepresentation(valueSerializer); diff --git a/src/MongoDB.Driver/Linq/Linq2Implementation/Expressions/ISerializationExpression.cs b/src/MongoDB.Driver/Linq/Linq2Implementation/Expressions/ISerializationExpression.cs index adb5c9e7083..1789751d938 100644 --- a/src/MongoDB.Driver/Linq/Linq2Implementation/Expressions/ISerializationExpression.cs +++ b/src/MongoDB.Driver/Linq/Linq2Implementation/Expressions/ISerializationExpression.cs @@ -49,7 +49,7 @@ public static BsonValue SerializeValue(this ISerializationExpression field, Type { Ensure.IsNotNull(field, nameof(field)); - var valueSerializer = FieldValueSerializerHelper.GetSerializerForValueType(field.Serializer, BsonSerializer.SerializerRegistry, valueType, value); + var valueSerializer = FieldValueSerializerHelper.GetSerializerForValueType(field.Serializer, BsonSerializer.GetSerializerRegistry(), valueType, value); var tempDocument = new BsonDocument(); using (var bsonWriter = new BsonDocumentWriter(tempDocument)) @@ -69,7 +69,7 @@ public static BsonArray SerializeValues(this ISerializationExpression field, Typ Ensure.IsNotNull(itemType, nameof(itemType)); Ensure.IsNotNull(values, nameof(values)); - var itemSerializer = FieldValueSerializerHelper.GetSerializerForValueType(field.Serializer, BsonSerializer.SerializerRegistry, itemType); + var itemSerializer = FieldValueSerializerHelper.GetSerializerForValueType(field.Serializer, BsonSerializer.GetSerializerRegistry(), itemType); var tempDocument = new BsonDocument(); using (var bsonWriter = new BsonDocumentWriter(tempDocument)) diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/PickMethodToAggregationExpressionTranslator.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/PickMethodToAggregationExpressionTranslator.cs index f3aa23c0811..af3e2cf9c80 100644 --- a/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/PickMethodToAggregationExpressionTranslator.cs +++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToAggregationExpressionTranslators/MethodTranslators/PickMethodToAggregationExpressionTranslator.cs @@ -290,7 +290,7 @@ private static AstSortFields TranslateSortByDefinition(Expression expression, Ex private static AstSortFields TranslateSortByDefinitionGeneric(Expression expression, Expression sortByExpression, SortDefinition sortByDefinition, IBsonSerializer documentSerializer) { - var serializerRegistry = BsonSerializer.SerializerRegistry; + var serializerRegistry = BsonSerializer.GetSerializerRegistry(); var sortDocument = sortByDefinition.Render(documentSerializer, serializerRegistry); var fields = new List(); foreach (var element in sortDocument) diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/MethodTranslators/InjectMethodToFilterTranslator.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/MethodTranslators/InjectMethodToFilterTranslator.cs index f36621e64d4..2f1868a61e2 100644 --- a/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/MethodTranslators/InjectMethodToFilterTranslator.cs +++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToFilterTranslators/MethodTranslators/InjectMethodToFilterTranslator.cs @@ -38,7 +38,7 @@ public static AstFilter Translate(TranslationContext context, MethodCallExpressi var filterDefinition = filterExpression.GetConstantValue(expression); var filterDefinitionType = filterDefinition.GetType(); // we KNOW it's a FilterDefinition because of the Inject method signature var documentType = filterDefinitionType.GetGenericArguments()[0]; - var serializerRegistry = BsonSerializer.SerializerRegistry; + var serializerRegistry = BsonSerializer.GetSerializerRegistry(); var documentSerializer = serializerRegistry.GetSerializer(documentType); // TODO: is this the right serializer? var renderMethodArgumentTypes = new[] { diff --git a/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToPipelineTranslators/AppendStageMethodToPipelineTranslator.cs b/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToPipelineTranslators/AppendStageMethodToPipelineTranslator.cs index fd67cbe07c2..ba071e85cbe 100644 --- a/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToPipelineTranslators/AppendStageMethodToPipelineTranslator.cs +++ b/src/MongoDB.Driver/Linq/Linq3Implementation/Translators/ExpressionToPipelineTranslators/AppendStageMethodToPipelineTranslator.cs @@ -38,7 +38,7 @@ public static AstPipeline Translate(TranslationContext context, MethodCallExpres var sourceSerializer = pipeline.OutputSerializer; var stageExpression = arguments[1]; - var renderedStage = TranslateStage(expression, stageExpression, sourceSerializer, BsonSerializer.SerializerRegistry); + var renderedStage = TranslateStage(expression, stageExpression, sourceSerializer, BsonSerializer.GetSerializerRegistry()); var stage = AstStage.Universal(renderedStage.Document); var resultSerializerExpression = arguments[2]; diff --git a/src/MongoDB.Driver/MongoClient.cs b/src/MongoDB.Driver/MongoClient.cs index 35e77c7b986..eba2d2cd479 100644 --- a/src/MongoDB.Driver/MongoClient.cs +++ b/src/MongoDB.Driver/MongoClient.cs @@ -434,7 +434,7 @@ private ListDatabasesOperation CreateListDatabaseOperation( { AuthorizedDatabases = options.AuthorizedDatabases, Comment = options.Comment, - Filter = options.Filter?.Render(BsonDocumentSerializer.Instance, BsonSerializer.SerializerRegistry, _linqProvider), + Filter = options.Filter?.Render(BsonDocumentSerializer.Instance, BsonSerializer.GetSerializerRegistry(), _linqProvider), NameOnly = options.NameOnly, RetryRequested = _settings.RetryReads }; diff --git a/src/MongoDB.Driver/MongoDatabaseImpl.cs b/src/MongoDB.Driver/MongoDatabaseImpl.cs index 45daa440177..152f219ee71 100644 --- a/src/MongoDB.Driver/MongoDatabaseImpl.cs +++ b/src/MongoDB.Driver/MongoDatabaseImpl.cs @@ -660,7 +660,7 @@ private Task CreateCollectionHelperAsync(IClientSessionHandle session private IWriteOperation CreateCreateCollectionOperation(string name, CreateCollectionOptions options) { - var serializerRegistry = options.SerializerRegistry ?? BsonSerializer.SerializerRegistry; + var serializerRegistry = options.SerializerRegistry ?? BsonSerializer.GetSerializerRegistry(); var documentSerializer = options.DocumentSerializer ?? serializerRegistry.GetSerializer(); var clusteredIndex = options.ClusteredIndex?.Render(documentSerializer, serializerRegistry); @@ -701,7 +701,7 @@ private IWriteOperation CreateCreateCollectionOperation private CreateViewOperation CreateCreateViewOperation(string viewName, string viewOn, PipelineDefinition pipeline, CreateViewOptions options) { - var serializerRegistry = options.SerializerRegistry ?? BsonSerializer.SerializerRegistry; + var serializerRegistry = options.SerializerRegistry ?? BsonSerializer.GetSerializerRegistry(); var documentSerializer = options.DocumentSerializer ?? serializerRegistry.GetSerializer(); var pipelineDocuments = pipeline.Render(documentSerializer, serializerRegistry, _linqProvider).Documents; return new CreateViewOperation(_databaseNamespace, viewName, viewOn, pipelineDocuments, GetMessageEncoderSettings()) diff --git a/src/MongoDB.Driver/MongoDatabaseSettings.cs b/src/MongoDB.Driver/MongoDatabaseSettings.cs index fc3f9e17bc0..abde09a0601 100644 --- a/src/MongoDB.Driver/MongoDatabaseSettings.cs +++ b/src/MongoDB.Driver/MongoDatabaseSettings.cs @@ -133,7 +133,7 @@ public IBsonSerializerRegistry SerializerRegistry { get { return _serializerRegistry.HasBeenSet ? _serializerRegistry.Value : - BsonSerializer.SerializerRegistry; } + BsonSerializer.GetSerializerRegistry(); } set { if (_isFrozen) { throw new InvalidOperationException("MongoCollectionSettings is frozen."); } diff --git a/src/MongoDB.Driver/PipelineDefinition.cs b/src/MongoDB.Driver/PipelineDefinition.cs index 13bb640dfc9..fd530d63547 100644 --- a/src/MongoDB.Driver/PipelineDefinition.cs +++ b/src/MongoDB.Driver/PipelineDefinition.cs @@ -113,7 +113,7 @@ public override string ToString() /// public string ToString(LinqProvider linqProvider) { - var serializerRegistry = BsonSerializer.SerializerRegistry; + var serializerRegistry = BsonSerializer.GetSerializerRegistry(); var inputSerializer = serializerRegistry.GetSerializer(); return ToString(inputSerializer, serializerRegistry, linqProvider); } diff --git a/src/MongoDB.Driver/PipelineStageDefinition.cs b/src/MongoDB.Driver/PipelineStageDefinition.cs index 5f371038791..7930705356a 100644 --- a/src/MongoDB.Driver/PipelineStageDefinition.cs +++ b/src/MongoDB.Driver/PipelineStageDefinition.cs @@ -227,7 +227,7 @@ public virtual RenderedPipelineStageDefinition Render(IBsonSerializer public override string ToString() { - var serializerRegistry = BsonSerializer.SerializerRegistry; + var serializerRegistry = BsonSerializer.GetSerializerRegistry(); var inputSerializer = serializerRegistry.GetSerializer(); return ToString(inputSerializer, serializerRegistry); } diff --git a/src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs b/src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs index c29c9bb5ff1..2360072909e 100644 --- a/src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs +++ b/src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs @@ -599,7 +599,7 @@ public static PipelineStageDefinition Facet f.Name), - facets.Select(f => f.OutputSerializer ?? BsonSerializer.SerializerRegistry.GetSerializer(f.OutputType))); + facets.Select(f => f.OutputSerializer ?? BsonSerializer.GetSerializerRegistry().GetSerializer(f.OutputType))); var options = new AggregateFacetOptions { OutputSerializer = outputSerializer }; return Facet(facets, options); } diff --git a/src/MongoDB.Driver/UpdateDefinition.cs b/src/MongoDB.Driver/UpdateDefinition.cs index df6f9fa9d8a..aea33774d11 100644 --- a/src/MongoDB.Driver/UpdateDefinition.cs +++ b/src/MongoDB.Driver/UpdateDefinition.cs @@ -136,7 +136,7 @@ public override BsonValue Render(IBsonSerializer documentSerializer, /// public override string ToString() { - var serializerRegistry = BsonSerializer.SerializerRegistry; + var serializerRegistry = BsonSerializer.GetSerializerRegistry(); var inputSerializer = serializerRegistry.GetSerializer(); return ToString(inputSerializer, serializerRegistry); }