ExceptionHandler.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Dispatcher
  5. {
  6. using System.Diagnostics;
  7. using System.Runtime;
  8. using System.Runtime.ConstrainedExecution;
  9. using System.Security;
  10. using System.Security.Permissions;
  11. using System.ServiceModel;
  12. public abstract class ExceptionHandler
  13. {
  14. static readonly ExceptionHandler alwaysHandle = new AlwaysHandleExceptionHandler();
  15. static ExceptionHandler transportExceptionHandler = alwaysHandle;
  16. public static ExceptionHandler AlwaysHandle
  17. {
  18. get
  19. {
  20. return alwaysHandle;
  21. }
  22. }
  23. public static ExceptionHandler AsynchronousThreadExceptionHandler
  24. {
  25. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  26. get
  27. {
  28. //
  29. HandlerWrapper wrapper = (HandlerWrapper)Fx.AsynchronousThreadExceptionHandler;
  30. return wrapper == null ? null : wrapper.Handler;
  31. }
  32. [Fx.Tag.SecurityNote(Critical = "Calls a LinkDemanded method (Fx setter) and critical method (HandlerWrapper ctor)",
  33. Safe = "protected with LinkDemand")]
  34. [SecuritySafeCritical]
  35. [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
  36. set
  37. {
  38. Fx.AsynchronousThreadExceptionHandler = value == null ? null : new HandlerWrapper(value);
  39. }
  40. }
  41. public static ExceptionHandler TransportExceptionHandler
  42. {
  43. get
  44. {
  45. return transportExceptionHandler;
  46. }
  47. set
  48. {
  49. transportExceptionHandler = value;
  50. }
  51. }
  52. // Returns true if the exception has been handled. If it returns false or
  53. // throws a different exception, the original exception will be rethrown.
  54. public abstract bool HandleException(Exception exception);
  55. class AlwaysHandleExceptionHandler : ExceptionHandler
  56. {
  57. [Fx.Tag.SecurityNote(Miscellaneous = "this function can be called from within a CER, must not call into PT code")]
  58. public override bool HandleException(Exception exception)
  59. {
  60. return true;
  61. }
  62. }
  63. internal static bool HandleTransportExceptionHelper(Exception exception)
  64. {
  65. if (exception == null)
  66. {
  67. throw Fx.AssertAndThrow("Null exception passed to HandleTransportExceptionHelper.");
  68. }
  69. ExceptionHandler handler = TransportExceptionHandler;
  70. if (handler == null)
  71. {
  72. return false;
  73. }
  74. try
  75. {
  76. if (!handler.HandleException(exception))
  77. {
  78. return false;
  79. }
  80. }
  81. catch (Exception thrownException)
  82. {
  83. if (Fx.IsFatal(thrownException))
  84. {
  85. throw;
  86. }
  87. DiagnosticUtility.TraceHandledException(thrownException, TraceEventType.Error);
  88. return false;
  89. }
  90. DiagnosticUtility.TraceHandledException(exception, TraceEventType.Error);
  91. return true;
  92. }
  93. class HandlerWrapper : Fx.ExceptionHandler
  94. {
  95. [Fx.Tag.SecurityNote(Critical = "Cannot let PT code alter the handler wrapped by this class.")]
  96. [SecurityCritical]
  97. readonly ExceptionHandler handler;
  98. [Fx.Tag.SecurityNote(Critical = "Cannot let PT code alter the handler wrapped by this class.")]
  99. [SecurityCritical]
  100. public HandlerWrapper(ExceptionHandler handler)
  101. {
  102. Fx.Assert(handler != null, "Cannot wrap a null handler.");
  103. this.handler = handler;
  104. }
  105. public ExceptionHandler Handler
  106. {
  107. [Fx.Tag.SecurityNote(Critical = "Access security-critical field.", Safe = "Ok to read field.")]
  108. [SecuritySafeCritical]
  109. [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
  110. get
  111. {
  112. return this.handler;
  113. }
  114. }
  115. [Fx.Tag.SecurityNote(Critical = "Access security-critical field.", Safe = "Ok to call handler.",
  116. Miscellaneous = "Called in a CER, must not call into PT code.")]
  117. [SecuritySafeCritical]
  118. public override bool HandleException(Exception exception)
  119. {
  120. return this.handler.HandleException(exception);
  121. }
  122. }
  123. }
  124. }