diff --git a/src/Kampute.HttpClient.DataContract/Kampute.HttpClient.DataContract.csproj b/src/Kampute.HttpClient.DataContract/Kampute.HttpClient.DataContract.csproj index c9097b6..16d3d9b 100644 --- a/src/Kampute.HttpClient.DataContract/Kampute.HttpClient.DataContract.csproj +++ b/src/Kampute.HttpClient.DataContract/Kampute.HttpClient.DataContract.csproj @@ -5,7 +5,7 @@ Kampute.HttpClient.DataContract This package is an extension package for Kampute.HttpClient, enhancing it to manage application/xml content types, using DataContractSerializer for serialization and deserialization of XML responses and payloads. Kambiz Khojasteh - 2.2.0 + 2.2.1 Kampute Copyright (c) 2024 Kampute latest diff --git a/src/Kampute.HttpClient.Json/Kampute.HttpClient.Json.csproj b/src/Kampute.HttpClient.Json/Kampute.HttpClient.Json.csproj index 1a87263..64846bf 100644 --- a/src/Kampute.HttpClient.Json/Kampute.HttpClient.Json.csproj +++ b/src/Kampute.HttpClient.Json/Kampute.HttpClient.Json.csproj @@ -5,7 +5,7 @@ Kampute.HttpClient.Json This package is an extension package for Kampute.HttpClient, enhancing it to manage application/json content types, using System.Text.Json library for serialization and deserialization of JSON responses and payloads. Kambiz Khojasteh - 2.2.0 + 2.2.1 Kampute Copyright (c) 2024 Kampute latest diff --git a/src/Kampute.HttpClient.NewtonsoftJson/Kampute.HttpClient.NewtonsoftJson.csproj b/src/Kampute.HttpClient.NewtonsoftJson/Kampute.HttpClient.NewtonsoftJson.csproj index faba952..b712b47 100644 --- a/src/Kampute.HttpClient.NewtonsoftJson/Kampute.HttpClient.NewtonsoftJson.csproj +++ b/src/Kampute.HttpClient.NewtonsoftJson/Kampute.HttpClient.NewtonsoftJson.csproj @@ -5,7 +5,7 @@ Kampute.HttpClient.NewtonsoftJson This package is an extension package for Kampute.HttpClient, enhancing it to manage application/json content types, using Newtonsoft.Json library for serialization and deserialization of JSON responses and payloads. Kambiz Khojasteh - 2.2.0 + 2.2.1 Kampute Copyright (c) 2024 Kampute latest diff --git a/src/Kampute.HttpClient.Xml/Kampute.HttpClient.Xml.csproj b/src/Kampute.HttpClient.Xml/Kampute.HttpClient.Xml.csproj index b328848..f612d4e 100644 --- a/src/Kampute.HttpClient.Xml/Kampute.HttpClient.Xml.csproj +++ b/src/Kampute.HttpClient.Xml/Kampute.HttpClient.Xml.csproj @@ -5,7 +5,7 @@ Kampute.HttpClient.Xml This package is an extension package for Kampute.HttpClient, enhancing it to manage application/xml content types, using XmlSerializer for serialization and deserialization of XML responses and payloads. Kambiz Khojasteh - 2.2.0 + 2.2.1 Kampute Copyright (c) 2024 Kampute latest diff --git a/src/Kampute.HttpClient/HttpRestClient.cs b/src/Kampute.HttpClient/HttpRestClient.cs index 10c4dfd..39b8639 100644 --- a/src/Kampute.HttpClient/HttpRestClient.cs +++ b/src/Kampute.HttpClient/HttpRestClient.cs @@ -57,8 +57,8 @@ private static HttpRequestHeaders CreateRequestHeaders() private readonly HttpClient _httpClient; private readonly IDisposable? _disposable; - private readonly Lazy>> _scopedHeaders = new(LazyThreadSafetyMode.ExecutionAndPublication); - private readonly Lazy>> _scopedProperties = new(LazyThreadSafetyMode.ExecutionAndPublication); + private readonly ScopedCollection> _scopedHeaders = new(); + private readonly ScopedCollection> _scopedProperties = new(); private IHttpBackoffProvider _backoffStrategy = BackoffStrategies.None; private Uri? _baseAddress; @@ -261,7 +261,7 @@ public virtual IDisposable BeginPropertyScope(IEnumerable @@ -290,7 +290,7 @@ public virtual IDisposable BeginHeaderScope(IEnumerable @@ -661,9 +661,9 @@ void AddRequestHeaders() foreach (var header in DefaultRequestHeaders) request.Headers.TryAddWithoutValidation(header.Key, header.Value); - if (_scopedHeaders.IsValueCreated) + if (_scopedHeaders.HasActiveScope) { - foreach (var header in _scopedHeaders.Value) + foreach (var header in _scopedHeaders) { request.Headers.Remove(header.Key); if (header.Value is not null) @@ -683,9 +683,9 @@ void AddRequestProperties() request.Properties[HttpRequestMessagePropertyKeys.TransactionId] = Guid.NewGuid(); request.Properties[HttpRequestMessagePropertyKeys.ResponseObjectType] = responseObjectType; - if (_scopedProperties.IsValueCreated) + if (_scopedProperties.HasActiveScope) { - foreach (var property in _scopedProperties.Value) + foreach (var property in _scopedProperties) { if (property.Value is not null) request.Properties[property.Key] = property.Value; diff --git a/src/Kampute.HttpClient/Kampute.HttpClient.csproj b/src/Kampute.HttpClient/Kampute.HttpClient.csproj index 5769e7a..34d0f8e 100644 --- a/src/Kampute.HttpClient/Kampute.HttpClient.csproj +++ b/src/Kampute.HttpClient/Kampute.HttpClient.csproj @@ -5,7 +5,7 @@ Kampute.HttpClient Kampute.HttpClient is a versatile and lightweight .NET library that simplifies RESTful API communication. Its core HttpRestClient class provides a streamlined approach to HTTP interactions, offering advanced features such as flexible serialization/deserialization, robust error handling, configurable backoff strategies, and detailed request-response processing. Striking a balance between simplicity and extensibility, Kampute.HttpClient empowers developers with a powerful yet easy-to-use client for seamless API integration across a wide range of .NET applications. Kambiz Khojasteh - 2.2.0 + 2.2.1 Kampute Copyright (c) 2024 Kampute latest diff --git a/src/Kampute.HttpClient/Utilities/FlyweightCache.cs b/src/Kampute.HttpClient/Utilities/FlyweightCache.cs index 0873697..501af10 100644 --- a/src/Kampute.HttpClient/Utilities/FlyweightCache.cs +++ b/src/Kampute.HttpClient/Utilities/FlyweightCache.cs @@ -25,8 +25,9 @@ public sealed class FlyweightCache where TKey : notnull /// A delegate that defines the method to create values if the key does not exist in the cache. /// Thrown if the provided is null. public FlyweightCache(Func valueFactory) - : this(valueFactory, null) { + _valueFactory = valueFactory ?? throw new ArgumentNullException(nameof(valueFactory)); + _store = new ConcurrentDictionary(); } /// @@ -34,11 +35,11 @@ public FlyweightCache(Func valueFactory) /// /// A delegate that defines the method to create values if the key does not exist in the cache. /// The equality comparison implementation to use when comparing keys. - /// Thrown if the provided is null. - public FlyweightCache(Func valueFactory, IEqualityComparer? keyComparer) + /// Thrown if the provided or is null. + public FlyweightCache(Func valueFactory, IEqualityComparer keyComparer) { _valueFactory = valueFactory ?? throw new ArgumentNullException(nameof(valueFactory)); - _store = new ConcurrentDictionary(keyComparer); + _store = new ConcurrentDictionary(keyComparer ?? throw new ArgumentNullException(nameof(keyComparer))); } /// diff --git a/src/Kampute.HttpClient/Utilities/ScopedCollection.cs b/src/Kampute.HttpClient/Utilities/ScopedCollection.cs index f910ce2..9b9f717 100644 --- a/src/Kampute.HttpClient/Utilities/ScopedCollection.cs +++ b/src/Kampute.HttpClient/Utilities/ScopedCollection.cs @@ -28,6 +28,14 @@ public class ScopedCollection : IEnumerable { private readonly AsyncLocal _activeScope = new(); + /// + /// Gets a value indicating whether the current context has an active scope. + /// + /// + /// true if an active scope is present; otherwise, false. + /// + public bool HasActiveScope => _activeScope.Value is not null; + /// /// Initiates a new scope within the current context, incorporating the specified items. ///