<PackageReference Include="Polly" Version="8.5.0" />

Context

public class Context : IDictionary<string, object>, ICollection<KeyValuePair<string, object>>, IEnumerable<KeyValuePair<string, object>>, IEnumerable, IDictionary, ICollection, IReadOnlyDictionary<string, object>, IReadOnlyCollection<KeyValuePair<string, object>>
Context that carries with a single execution through a Policy. Commonly-used properties are directly on the class. Backed by a dictionary of string key / object value pairs, to which user-defined values may be added. Do not re-use an instance of Context across more than one call through .Execute(...) or .ExecuteAsync(...).
using System; using System.Collections; using System.Collections.Generic; namespace Polly { public class Context : IDictionary<string, object>, ICollection<KeyValuePair<string, object>>, IEnumerable<KeyValuePair<string, object>>, IEnumerable, IDictionary, ICollection, IReadOnlyDictionary<string, object>, IReadOnlyCollection<KeyValuePair<string, object>> { private Guid? _correlationId; private Dictionary<string, object> _wrappedDictionary; public string PolicyWrapKey { get; set; } public string PolicyKey { get; set; } public string OperationKey { get; } public Guid CorrelationId { get { Guid valueOrDefault = _correlationId.GetValueOrDefault(); if (!_correlationId.HasValue) { valueOrDefault = Guid.NewGuid(); _correlationId = valueOrDefault; } return _correlationId.Value; } } private Dictionary<string, object> WrappedDictionary => _wrappedDictionary ?? (_wrappedDictionary = new Dictionary<string, object>()); public ICollection<string> Keys => WrappedDictionary.Keys; public ICollection<object> Values => WrappedDictionary.Values; public int Count => WrappedDictionary.Count; bool ICollection<KeyValuePair<string, object>>.IsReadOnly { get { return ((ICollection<KeyValuePair<string, object>>)WrappedDictionary).IsReadOnly; } } public object this[string key] { get { return WrappedDictionary[key]; } set { WrappedDictionary[key] = value; } } IEnumerable<string> IReadOnlyDictionary<string, object>.Keys { get { return ((IReadOnlyDictionary<string, object>)WrappedDictionary).Keys; } } IEnumerable<object> IReadOnlyDictionary<string, object>.Values { get { return ((IReadOnlyDictionary<string, object>)WrappedDictionary).Values; } } bool IDictionary.IsFixedSize { get { return ((IDictionary)WrappedDictionary).IsFixedSize; } } bool IDictionary.IsReadOnly { get { return ((IDictionary)WrappedDictionary).IsReadOnly; } } ICollection IDictionary.Keys { get { return ((IDictionary)WrappedDictionary).Keys; } } ICollection IDictionary.Values { get { return ((IDictionary)WrappedDictionary).Values; } } bool ICollection.IsSynchronized { get { return ((ICollection)WrappedDictionary).IsSynchronized; } } object ICollection.SyncRoot { get { return ((ICollection)WrappedDictionary).SyncRoot; } } object IDictionary.this[object key] { get { return ((IDictionary)WrappedDictionary)[key]; } set { ((IDictionary)WrappedDictionary)[key] = value; } } internal static Context None() { return new Context(); } public Context(string operationKey) { OperationKey = operationKey; } public Context() { } public Context(string operationKey, IDictionary<string, object> contextData) : this(contextData) { OperationKey = operationKey; } internal Context(IDictionary<string, object> contextData) : this() { if (contextData == null) throw new ArgumentNullException("contextData"); _wrappedDictionary = new Dictionary<string, object>(contextData); } public void Add(string key, object value) { WrappedDictionary.Add(key, value); } public bool ContainsKey(string key) { return WrappedDictionary.ContainsKey(key); } public bool Remove(string key) { return WrappedDictionary.Remove(key); } public bool TryGetValue(string key, out object value) { return WrappedDictionary.TryGetValue(key, out value); } void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item) { ((ICollection<KeyValuePair<string, object>>)WrappedDictionary).Add(item); } public void Clear() { WrappedDictionary.Clear(); } bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item) { return ((ICollection<KeyValuePair<string, object>>)WrappedDictionary).Contains(item); } void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex) { ((ICollection<KeyValuePair<string, object>>)WrappedDictionary).CopyTo(array, arrayIndex); } bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item) { return ((ICollection<KeyValuePair<string, object>>)WrappedDictionary).Remove(item); } public IEnumerator<KeyValuePair<string, object>> GetEnumerator() { return WrappedDictionary.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return WrappedDictionary.GetEnumerator(); } public void Add(object key, object value) { ((IDictionary)WrappedDictionary).Add(key, value); } public bool Contains(object key) { return ((IDictionary)WrappedDictionary).Contains(key); } IDictionaryEnumerator IDictionary.GetEnumerator() { return ((IDictionary)WrappedDictionary).GetEnumerator(); } public void Remove(object key) { ((IDictionary)WrappedDictionary).Remove(key); } public void CopyTo(Array array, int index) { ((ICollection)WrappedDictionary).CopyTo(array, index); } } }