forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowData.cs
More file actions
52 lines (49 loc) · 1.58 KB
/
FlowData.cs
File metadata and controls
52 lines (49 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#if NET45
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
#else
using System.Threading;
#endif
namespace StackExchange.Profiling.Internal
{
/// <summary>
/// Internal MiniProfiler extensions, not meant for consumption.
/// This can and probably will break without warning. Don't use the .Internal namespace directly.
///
/// Shim class covering for AsyncLocal{T} in pre-.NET 4.6 which didn't have it.
/// </summary>
/// <typeparam name="T">The type of data to store.</typeparam>
public class FlowData<T>
{
#if NET45
// Key specific to this type.
#pragma warning disable RCS1158 // Avoid static members in generic types.
private static readonly string _key = typeof(FlowData<T>).FullName;
#pragma warning restore RCS1158 // Avoid static members in generic types.
/// <summary>
/// Gets or sets the value of the ambient data.
/// </summary>
public T Value
{
get
{
var handle = CallContext.LogicalGetData(_key) as ObjectHandle;
return handle != null
? (T)handle.Unwrap()
: default(T);
}
set { CallContext.LogicalSetData(_key, new ObjectHandle(value)); }
}
#else
private readonly AsyncLocal<T> _backing = new AsyncLocal<T>();
/// <summary>
/// Gets or sets the value of the ambient data.
/// </summary>
public T Value
{
get => _backing.Value;
set => _backing.Value = value;
}
#endif
}
}