Context
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 execution.
using System;
using System.Collections.Generic;
namespace Polly
{
public class Context : Dictionary<string, object>
{
private static readonly IDictionary<string, object> emptyDictionary = new Dictionary<string, object>();
internal static readonly Context None = new Context(emptyDictionary);
private Guid? _executionGuid;
public string PolicyWrapKey { get; set; }
public string PolicyKey { get; set; }
public string ExecutionKey { get; }
public Guid ExecutionGuid {
get {
if (!_executionGuid.HasValue)
_executionGuid = Guid.NewGuid();
return _executionGuid.Value;
}
}
public Context(string executionKey)
: this(executionKey, emptyDictionary)
{
}
public Context(string executionKey, IDictionary<string, object> contextData)
: this(contextData)
{
ExecutionKey = executionKey;
}
internal Context()
: this(emptyDictionary)
{
}
internal Context(IDictionary<string, object> contextData)
: base(contextData)
{
if (contextData == null)
throw new ArgumentNullException("contextData");
}
}
}