FaultConverter.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. Message CreateSimpleFaultMessage (Exception error, MessageVersion version)
  60. {
  61. Console.WriteLine (error);
  62. OperationContext ctx = OperationContext.Current;
  63. // FIXME: support more fault code depending on the exception type.
  64. FaultCode fc = null;
  65. if (error is EndpointNotFoundException)
  66. fc = new FaultCode (
  67. "DestinationUnreachable",
  68. version.Addressing.Namespace);
  69. else
  70. fc = new FaultCode (
  71. "FIXME_InternalError",
  72. version.Addressing.Namespace);
  73. // FIXME: set correct fault reason.
  74. if (ctx.EndpointDispatcher.ChannelDispatcher.IncludeExceptionDetailInFaults) {
  75. ExceptionDetail detail = new ExceptionDetail (error);
  76. return Message.CreateMessage (version, fc,
  77. "error occured", detail, ctx.IncomingMessageHeaders.Action);
  78. }
  79. return Message.CreateMessage (version, fc, "error occured", ctx.IncomingMessageHeaders.Action);
  80. }
  81. class SimpleFaultConverter : FaultConverter
  82. {
  83. MessageVersion version;
  84. public SimpleFaultConverter (MessageVersion version)
  85. {
  86. this.version = version;
  87. }
  88. protected override bool OnTryCreateException (
  89. Message message, MessageFault fault, out Exception error)
  90. {
  91. error = null;
  92. return false;
  93. }
  94. protected override bool OnTryCreateFaultMessage (
  95. Exception error, out Message message)
  96. {
  97. message = null;
  98. if (OperationContext.Current == null)
  99. return false;
  100. message = CreateSimpleFaultMessage (error, version);
  101. return true;
  102. }
  103. }
  104. }
  105. }