diff --git a/src/Elastic.Clients.Elasticsearch/_Shared/Serialization/DefaultRequestResponseSerializer.cs b/src/Elastic.Clients.Elasticsearch/_Shared/Serialization/DefaultRequestResponseSerializer.cs index 7669ee7dd1..915c0552dc 100644 --- a/src/Elastic.Clients.Elasticsearch/_Shared/Serialization/DefaultRequestResponseSerializer.cs +++ b/src/Elastic.Clients.Elasticsearch/_Shared/Serialization/DefaultRequestResponseSerializer.cs @@ -24,6 +24,10 @@ internal sealed class DefaultRequestResponseSerializer : SystemTextJsonSerialize { private readonly IElasticsearchClientSettings _settings; +#if !NET8_0_OR_GREATER + private readonly object _lock = new(); +#endif + public DefaultRequestResponseSerializer(IElasticsearchClientSettings settings) : base(new DefaultRequestResponseSerializerOptionsProvider(settings)) { @@ -62,15 +66,23 @@ private void LinkSettings(IElasticsearchClientSettings settings) var options = GetJsonSerializerOptions(SerializationFormatting.None); var indentedOptions = GetJsonSerializerOptions(SerializationFormatting.Indented); - if (!ElasticsearchClient.SettingsTable.TryGetValue(options, out _)) - { - ElasticsearchClient.SettingsTable.Add(options, settings); - } - - if (!ElasticsearchClient.SettingsTable.TryGetValue(indentedOptions, out _)) +#if NET8_0_OR_GREATER + ElasticsearchClient.SettingsTable.TryAdd(options, settings); + ElasticsearchClient.SettingsTable.TryAdd(indentedOptions, settings); +#else + lock (_lock) { - ElasticsearchClient.SettingsTable.Add(indentedOptions, settings); + if (!ElasticsearchClient.SettingsTable.TryGetValue(options, out _)) + { + ElasticsearchClient.SettingsTable.Add(options, settings); + } + + if (!ElasticsearchClient.SettingsTable.TryGetValue(indentedOptions, out _)) + { + ElasticsearchClient.SettingsTable.Add(indentedOptions, settings); + } } +#endif } } diff --git a/src/Elastic.Clients.Elasticsearch/_Shared/Serialization/DefaultSourceSerializer.cs b/src/Elastic.Clients.Elasticsearch/_Shared/Serialization/DefaultSourceSerializer.cs index de2bc568f4..2b986d4fa0 100644 --- a/src/Elastic.Clients.Elasticsearch/_Shared/Serialization/DefaultSourceSerializer.cs +++ b/src/Elastic.Clients.Elasticsearch/_Shared/Serialization/DefaultSourceSerializer.cs @@ -21,6 +21,10 @@ namespace Elastic.Clients.Elasticsearch.Serialization; public class DefaultSourceSerializer : SystemTextJsonSerializer { +#if !NET8_0_OR_GREATER + private readonly object _lock = new(); +#endif + /// /// Constructs a new instance that accepts an that can /// be provided to customize the default . @@ -43,15 +47,23 @@ private void LinkSettings(IElasticsearchClientSettings settings) var options = GetJsonSerializerOptions(SerializationFormatting.None); var indentedOptions = GetJsonSerializerOptions(SerializationFormatting.Indented); - if (!ElasticsearchClient.SettingsTable.TryGetValue(options, out _)) +#if NET8_0_OR_GREATER + ElasticsearchClient.SettingsTable.TryAdd(options, settings); + ElasticsearchClient.SettingsTable.TryAdd(indentedOptions, settings); +#else + lock (_lock) { - ElasticsearchClient.SettingsTable.Add(options, settings); - } + if (!ElasticsearchClient.SettingsTable.TryGetValue(options, out _)) + { + ElasticsearchClient.SettingsTable.Add(options, settings); + } - if (!ElasticsearchClient.SettingsTable.TryGetValue(indentedOptions, out _)) - { - ElasticsearchClient.SettingsTable.Add(indentedOptions, settings); + if (!ElasticsearchClient.SettingsTable.TryGetValue(indentedOptions, out _)) + { + ElasticsearchClient.SettingsTable.Add(indentedOptions, settings); + } } +#endif } }