1
0

CallerThreadContext.cs 4.2 KB

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