FaultConverter.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. error = null;
  78. return false;
  79. }
  80. protected override bool OnTryCreateFaultMessage (Exception error, out Message message)
  81. {
  82. if (version.Addressing.Equals (AddressingVersion.None)) {
  83. message = null;
  84. return false;
  85. }
  86. string action;
  87. if (!map.TryGetValue (error.GetType (), out action)) {
  88. message = null;
  89. return false;
  90. }
  91. FaultCode fc;
  92. if (version.Envelope.Equals (EnvelopeVersion.Soap12))
  93. fc = new FaultCode ("Sender", version.Envelope.Namespace, new FaultCode (action, version.Addressing.Namespace));
  94. else
  95. fc = new FaultCode (action, version.Addressing.Namespace);
  96. OperationContext ctx = OperationContext.Current;
  97. // FIXME: support more fault code depending on the exception type.
  98. #if !NET_2_1
  99. // FIXME: set correct fault reason.
  100. if (ctx != null && ctx.EndpointDispatcher.ChannelDispatcher.IncludeExceptionDetailInFaults) {
  101. ExceptionDetail detail = new ExceptionDetail (error);
  102. message = Message.CreateMessage (version, fc,
  103. error.Message, detail, version.Addressing.FaultNamespace);
  104. }
  105. #endif
  106. else
  107. message = Message.CreateMessage (version, fc, error.Message, version.Addressing.FaultNamespace);
  108. return true;
  109. }
  110. }
  111. }