ExceptionMapper.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------------------------
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Globalization;
  8. using System.IdentityModel.Protocols.WSTrust;
  9. using System.IdentityModel.Tokens;
  10. using System.Reflection;
  11. using System.Runtime;
  12. using System.ServiceModel;
  13. using System.ServiceModel.Diagnostics;
  14. namespace System.ServiceModel
  15. {
  16. /// <summary>
  17. /// Defines the mapping to be used for translating exceptions to faults.
  18. /// </summary>
  19. public class ExceptionMapper
  20. {
  21. internal const string SoapSenderFaultCode = "Sender";
  22. /// <summary>
  23. /// ExceptionMapper constructor.
  24. /// </summary>
  25. public ExceptionMapper()
  26. {
  27. }
  28. /// <summary>
  29. /// Translates the input exception to a fault using the mapping defined in ExceptionMap.
  30. /// </summary>
  31. /// <param name="ex">The exception to be mapped to a fault.</param>
  32. /// <returns>The fault corresponding to the input exception.</returns>
  33. public virtual FaultException FromException(Exception ex)
  34. {
  35. return FromException(ex, String.Empty, String.Empty);
  36. }
  37. /// <summary>
  38. /// Translates the input exception to a fault using the mapping defined in ExceptionMap.
  39. /// </summary>
  40. /// <param name="ex">The exception to be mapped to a fault.</param>
  41. /// <param name="soapNamespace">The SOAP Namespace to be used when generating the mapped fault.</param>
  42. /// <param name="trustNamespace">The WS-Trust Namespace to be used when generating the mapped fault.</param>
  43. /// <returns>The fault corresponding to the input exception.</returns>
  44. public virtual FaultException FromException(Exception ex, string soapNamespace, string trustNamespace)
  45. {
  46. return null;
  47. }
  48. /// <summary>
  49. /// Determines whether an exception that occurred during the processing of a security token
  50. /// should be handled using the defined ExceptionMap.
  51. /// </summary>
  52. /// <param name="ex">The input exception.</param>
  53. /// <returns>A boolean value indicating whether the exception should be handled using the defined ExceptionMap.</returns>
  54. public virtual bool HandleSecurityTokenProcessingException(Exception ex)
  55. {
  56. if (Fx.IsFatal(ex))
  57. {
  58. return false;
  59. }
  60. if (ex is FaultException)
  61. {
  62. // Just throw the original exception.
  63. return false;
  64. }
  65. else
  66. {
  67. FaultException faultException = FromException(ex);
  68. if (faultException != null)
  69. {
  70. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(faultException);
  71. }
  72. // The exception is not one of the recognized exceptions. Just throw the original exception.
  73. return false;
  74. }
  75. }
  76. }
  77. }