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