diff options
Diffstat (limited to 'ThirdParty/SmartThreadPool/CallerThreadContext.cs')
-rw-r--r-- | ThirdParty/SmartThreadPool/CallerThreadContext.cs | 276 |
1 files changed, 138 insertions, 138 deletions
diff --git a/ThirdParty/SmartThreadPool/CallerThreadContext.cs b/ThirdParty/SmartThreadPool/CallerThreadContext.cs index 2177241..e63add5 100644 --- a/ThirdParty/SmartThreadPool/CallerThreadContext.cs +++ b/ThirdParty/SmartThreadPool/CallerThreadContext.cs | |||
@@ -1,138 +1,138 @@ | |||
1 | 1 | ||
2 | #if !(_WINDOWS_CE) && !(_SILVERLIGHT) && !(WINDOWS_PHONE) | 2 | #if !(_WINDOWS_CE) && !(_SILVERLIGHT) && !(WINDOWS_PHONE) |
3 | 3 | ||
4 | using System; | 4 | using System; |
5 | using System.Diagnostics; | 5 | using System.Diagnostics; |
6 | using System.Threading; | 6 | using System.Threading; |
7 | using System.Reflection; | 7 | using System.Reflection; |
8 | using System.Web; | 8 | using System.Web; |
9 | using System.Runtime.Remoting.Messaging; | 9 | using System.Runtime.Remoting.Messaging; |
10 | 10 | ||
11 | 11 | ||
12 | namespace Amib.Threading.Internal | 12 | namespace Amib.Threading.Internal |
13 | { | 13 | { |
14 | #region CallerThreadContext class | 14 | #region CallerThreadContext class |
15 | 15 | ||
16 | /// <summary> | 16 | /// <summary> |
17 | /// This class stores the caller call context in order to restore | 17 | /// This class stores the caller call context in order to restore |
18 | /// it when the work item is executed in the thread pool environment. | 18 | /// it when the work item is executed in the thread pool environment. |
19 | /// </summary> | 19 | /// </summary> |
20 | internal class CallerThreadContext | 20 | internal class CallerThreadContext |
21 | { | 21 | { |
22 | #region Prepare reflection information | 22 | #region Prepare reflection information |
23 | 23 | ||
24 | // Cached type information. | 24 | // Cached type information. |
25 | private static readonly MethodInfo getLogicalCallContextMethodInfo = | 25 | private static readonly MethodInfo getLogicalCallContextMethodInfo = |
26 | typeof(Thread).GetMethod("GetLogicalCallContext", BindingFlags.Instance | BindingFlags.NonPublic); | 26 | typeof(Thread).GetMethod("GetLogicalCallContext", BindingFlags.Instance | BindingFlags.NonPublic); |
27 | 27 | ||
28 | private static readonly MethodInfo setLogicalCallContextMethodInfo = | 28 | private static readonly MethodInfo setLogicalCallContextMethodInfo = |
29 | typeof(Thread).GetMethod("SetLogicalCallContext", BindingFlags.Instance | BindingFlags.NonPublic); | 29 | typeof(Thread).GetMethod("SetLogicalCallContext", BindingFlags.Instance | BindingFlags.NonPublic); |
30 | 30 | ||
31 | private static string HttpContextSlotName = GetHttpContextSlotName(); | 31 | private static string HttpContextSlotName = GetHttpContextSlotName(); |
32 | 32 | ||
33 | private static string GetHttpContextSlotName() | 33 | private static string GetHttpContextSlotName() |
34 | { | 34 | { |
35 | FieldInfo fi = typeof(HttpContext).GetField("CallContextSlotName", BindingFlags.Static | BindingFlags.NonPublic); | 35 | FieldInfo fi = typeof(HttpContext).GetField("CallContextSlotName", BindingFlags.Static | BindingFlags.NonPublic); |
36 | 36 | ||
37 | if (fi != null) | 37 | if (fi != null) |
38 | { | 38 | { |
39 | return (string) fi.GetValue(null); | 39 | return (string) fi.GetValue(null); |
40 | } | 40 | } |
41 | 41 | ||
42 | return "HttpContext"; | 42 | return "HttpContext"; |
43 | } | 43 | } |
44 | 44 | ||
45 | #endregion | 45 | #endregion |
46 | 46 | ||
47 | #region Private fields | 47 | #region Private fields |
48 | 48 | ||
49 | private HttpContext _httpContext; | 49 | private HttpContext _httpContext; |
50 | private LogicalCallContext _callContext; | 50 | private LogicalCallContext _callContext; |
51 | 51 | ||
52 | #endregion | 52 | #endregion |
53 | 53 | ||
54 | /// <summary> | 54 | /// <summary> |
55 | /// Constructor | 55 | /// Constructor |
56 | /// </summary> | 56 | /// </summary> |
57 | private CallerThreadContext() | 57 | private CallerThreadContext() |
58 | { | 58 | { |
59 | } | 59 | } |
60 | 60 | ||
61 | public bool CapturedCallContext | 61 | public bool CapturedCallContext |
62 | { | 62 | { |
63 | get | 63 | get |
64 | { | 64 | { |
65 | return (null != _callContext); | 65 | return (null != _callContext); |
66 | } | 66 | } |
67 | } | 67 | } |
68 | 68 | ||
69 | public bool CapturedHttpContext | 69 | public bool CapturedHttpContext |
70 | { | 70 | { |
71 | get | 71 | get |
72 | { | 72 | { |
73 | return (null != _httpContext); | 73 | return (null != _httpContext); |
74 | } | 74 | } |
75 | } | 75 | } |
76 | 76 | ||
77 | /// <summary> | 77 | /// <summary> |
78 | /// Captures the current thread context | 78 | /// Captures the current thread context |
79 | /// </summary> | 79 | /// </summary> |
80 | /// <returns></returns> | 80 | /// <returns></returns> |
81 | public static CallerThreadContext Capture( | 81 | public static CallerThreadContext Capture( |
82 | bool captureCallContext, | 82 | bool captureCallContext, |
83 | bool captureHttpContext) | 83 | bool captureHttpContext) |
84 | { | 84 | { |
85 | Debug.Assert(captureCallContext || captureHttpContext); | 85 | Debug.Assert(captureCallContext || captureHttpContext); |
86 | 86 | ||
87 | CallerThreadContext callerThreadContext = new CallerThreadContext(); | 87 | CallerThreadContext callerThreadContext = new CallerThreadContext(); |
88 | 88 | ||
89 | // TODO: In NET 2.0, redo using the new feature of ExecutionContext class - Capture() | 89 | // TODO: In NET 2.0, redo using the new feature of ExecutionContext class - Capture() |
90 | // Capture Call Context | 90 | // Capture Call Context |
91 | if(captureCallContext && (getLogicalCallContextMethodInfo != null)) | 91 | if(captureCallContext && (getLogicalCallContextMethodInfo != null)) |
92 | { | 92 | { |
93 | callerThreadContext._callContext = (LogicalCallContext)getLogicalCallContextMethodInfo.Invoke(Thread.CurrentThread, null); | 93 | callerThreadContext._callContext = (LogicalCallContext)getLogicalCallContextMethodInfo.Invoke(Thread.CurrentThread, null); |
94 | if (callerThreadContext._callContext != null) | 94 | if (callerThreadContext._callContext != null) |
95 | { | 95 | { |
96 | callerThreadContext._callContext = (LogicalCallContext)callerThreadContext._callContext.Clone(); | 96 | callerThreadContext._callContext = (LogicalCallContext)callerThreadContext._callContext.Clone(); |
97 | } | 97 | } |
98 | } | 98 | } |
99 | 99 | ||
100 | // Capture httpContext | 100 | // Capture httpContext |
101 | if (captureHttpContext && (null != HttpContext.Current)) | 101 | if (captureHttpContext && (null != HttpContext.Current)) |
102 | { | 102 | { |
103 | callerThreadContext._httpContext = HttpContext.Current; | 103 | callerThreadContext._httpContext = HttpContext.Current; |
104 | } | 104 | } |
105 | 105 | ||
106 | return callerThreadContext; | 106 | return callerThreadContext; |
107 | } | 107 | } |
108 | 108 | ||
109 | /// <summary> | 109 | /// <summary> |
110 | /// Applies the thread context stored earlier | 110 | /// Applies the thread context stored earlier |
111 | /// </summary> | 111 | /// </summary> |
112 | /// <param name="callerThreadContext"></param> | 112 | /// <param name="callerThreadContext"></param> |
113 | public static void Apply(CallerThreadContext callerThreadContext) | 113 | public static void Apply(CallerThreadContext callerThreadContext) |
114 | { | 114 | { |
115 | if (null == callerThreadContext) | 115 | if (null == callerThreadContext) |
116 | { | 116 | { |
117 | throw new ArgumentNullException("callerThreadContext"); | 117 | throw new ArgumentNullException("callerThreadContext"); |
118 | } | 118 | } |
119 | 119 | ||
120 | // Todo: In NET 2.0, redo using the new feature of ExecutionContext class - Run() | 120 | // Todo: In NET 2.0, redo using the new feature of ExecutionContext class - Run() |
121 | // Restore call context | 121 | // Restore call context |
122 | if ((callerThreadContext._callContext != null) && (setLogicalCallContextMethodInfo != null)) | 122 | if ((callerThreadContext._callContext != null) && (setLogicalCallContextMethodInfo != null)) |
123 | { | 123 | { |
124 | setLogicalCallContextMethodInfo.Invoke(Thread.CurrentThread, new object[] { callerThreadContext._callContext }); | 124 | setLogicalCallContextMethodInfo.Invoke(Thread.CurrentThread, new object[] { callerThreadContext._callContext }); |
125 | } | 125 | } |
126 | 126 | ||
127 | // Restore HttpContext | 127 | // Restore HttpContext |
128 | if (callerThreadContext._httpContext != null) | 128 | if (callerThreadContext._httpContext != null) |
129 | { | 129 | { |
130 | HttpContext.Current = callerThreadContext._httpContext; | 130 | HttpContext.Current = callerThreadContext._httpContext; |
131 | //CallContext.SetData(HttpContextSlotName, callerThreadContext._httpContext); | 131 | //CallContext.SetData(HttpContextSlotName, callerThreadContext._httpContext); |
132 | } | 132 | } |
133 | } | 133 | } |
134 | } | 134 | } |
135 | 135 | ||
136 | #endregion | 136 | #endregion |
137 | } | 137 | } |
138 | #endif | 138 | #endif |