FaultConverter.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // FaultConverter.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc. http://www.novell.com
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.ServiceModel;
  31. using System.ServiceModel.Description;
  32. namespace System.ServiceModel.Channels
  33. {
  34. public abstract class FaultConverter
  35. {
  36. public static FaultConverter GetDefaultFaultConverter (MessageVersion version)
  37. {
  38. if (version == null)
  39. throw new ArgumentNullException ("version");
  40. return new SimpleFaultConverter (version);
  41. }
  42. protected FaultConverter ()
  43. {
  44. }
  45. [MonoTODO]
  46. protected abstract bool OnTryCreateException (
  47. Message message, MessageFault fault, out Exception error);
  48. [MonoTODO]
  49. protected abstract bool OnTryCreateFaultMessage (
  50. Exception error, out Message message);
  51. public bool TryCreateException (Message message, MessageFault fault, out Exception error)
  52. {
  53. return OnTryCreateException (message, fault, out error);
  54. }
  55. public bool TryCreateFaultMessage (Exception error, out Message message)
  56. {
  57. return OnTryCreateFaultMessage (error, out message);
  58. }
  59. }
  60. class SimpleFaultConverter : FaultConverter
  61. {
  62. static readonly Dictionary<Type,string> map;
  63. static SimpleFaultConverter ()
  64. {
  65. map = new Dictionary<Type,string> ();
  66. map [typeof (EndpointNotFoundException)] = "DestinationUnreachable";
  67. map [typeof (ActionNotSupportedException)] = "ActionNotSupported";
  68. }
  69. MessageVersion version;
  70. public SimpleFaultConverter (MessageVersion version)
  71. {
  72. this.version = version;
  73. }
  74. protected override bool OnTryCreateException (
  75. Message message, MessageFault fault, out Exception error)
  76. {
  77. if (message == null)
  78. throw new ArgumentNullException ("message");
  79. if (fault == null)
  80. throw new ArgumentNullException ("fault");
  81. error = null;
  82. FaultCode fc;
  83. if (version.Envelope.Equals (EnvelopeVersion.Soap11))
  84. fc = fault.Code;
  85. else
  86. fc = fault.Code.SubCode;
  87. if (fc == null)
  88. return false;
  89. string msg = fault.Reason.GetMatchingTranslation ().Text;
  90. if (fc.Namespace == message.Version.Addressing.Namespace) {
  91. switch (fc.Name) {
  92. case "ActionNotSupported":
  93. error = new ActionNotSupportedException (msg);
  94. return true;
  95. case "DestinationUnreachable":
  96. error = new EndpointNotFoundException (msg);
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. protected override bool OnTryCreateFaultMessage (Exception error, out Message message)
  103. {
  104. if (version.Envelope.Equals (EnvelopeVersion.None)) {
  105. message = null;
  106. return false;
  107. }
  108. string action;
  109. if (!map.TryGetValue (error.GetType (), out action)) {
  110. message = null;
  111. return false;
  112. }
  113. FaultCode fc;
  114. if (version.Envelope.Equals (EnvelopeVersion.Soap12))
  115. fc = new FaultCode ("Sender", version.Envelope.Namespace, new FaultCode (action, version.Addressing.Namespace));
  116. else
  117. fc = new FaultCode (action, version.Addressing.Namespace);
  118. OperationContext ctx = OperationContext.Current;
  119. // FIXME: support more fault code depending on the exception type.
  120. #if !MOBILE && !XAMMAC_4_5
  121. // FIXME: set correct fault reason.
  122. if (ctx != null && ctx.EndpointDispatcher.ChannelDispatcher.IncludeExceptionDetailInFaults) {
  123. ExceptionDetail detail = new ExceptionDetail (error);
  124. message = Message.CreateMessage (version, fc,
  125. error.Message, detail, version.Addressing.FaultNamespace);
  126. }
  127. else
  128. #endif
  129. message = Message.CreateMessage (version, fc, error.Message, version.Addressing.FaultNamespace);
  130. return true;
  131. }
  132. }
  133. }