MessageVersion.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.ComponentModel;
  7. using System.Globalization;
  8. using System.Runtime;
  9. using System.ServiceModel.Configuration;
  10. [TypeConverter(typeof(MessageVersionConverter))]
  11. public sealed class MessageVersion
  12. {
  13. EnvelopeVersion envelope;
  14. AddressingVersion addressing;
  15. static MessageVersion none;
  16. static MessageVersion soap11;
  17. static MessageVersion soap12;
  18. static MessageVersion soap11Addressing10;
  19. static MessageVersion soap12Addressing10;
  20. static MessageVersion soap11Addressing200408;
  21. static MessageVersion soap12Addressing200408;
  22. static MessageVersion()
  23. {
  24. none = new MessageVersion(EnvelopeVersion.None, AddressingVersion.None);
  25. soap11 = new MessageVersion(EnvelopeVersion.Soap11, AddressingVersion.None);
  26. soap12 = new MessageVersion(EnvelopeVersion.Soap12, AddressingVersion.None);
  27. soap11Addressing10 = new MessageVersion(EnvelopeVersion.Soap11, AddressingVersion.WSAddressing10);
  28. soap12Addressing10 = new MessageVersion(EnvelopeVersion.Soap12, AddressingVersion.WSAddressing10);
  29. soap11Addressing200408 = new MessageVersion(EnvelopeVersion.Soap11, AddressingVersion.WSAddressingAugust2004);
  30. soap12Addressing200408 = new MessageVersion(EnvelopeVersion.Soap12, AddressingVersion.WSAddressingAugust2004);
  31. }
  32. MessageVersion(EnvelopeVersion envelopeVersion, AddressingVersion addressingVersion)
  33. {
  34. this.envelope = envelopeVersion;
  35. this.addressing = addressingVersion;
  36. }
  37. public static MessageVersion CreateVersion(EnvelopeVersion envelopeVersion)
  38. {
  39. return CreateVersion(envelopeVersion, AddressingVersion.WSAddressing10);
  40. }
  41. public static MessageVersion CreateVersion(EnvelopeVersion envelopeVersion, AddressingVersion addressingVersion)
  42. {
  43. if (envelopeVersion == null)
  44. {
  45. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("envelopeVersion");
  46. }
  47. if (addressingVersion == null)
  48. {
  49. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("addressingVersion");
  50. }
  51. if (envelopeVersion == EnvelopeVersion.Soap12)
  52. {
  53. if (addressingVersion == AddressingVersion.WSAddressing10)
  54. {
  55. return soap12Addressing10;
  56. }
  57. else if (addressingVersion == AddressingVersion.WSAddressingAugust2004)
  58. {
  59. return soap12Addressing200408;
  60. }
  61. else if (addressingVersion == AddressingVersion.None)
  62. {
  63. return soap12;
  64. }
  65. else
  66. {
  67. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("addressingVersion",
  68. SR.GetString(SR.AddressingVersionNotSupported, addressingVersion));
  69. }
  70. }
  71. else if (envelopeVersion == EnvelopeVersion.Soap11)
  72. {
  73. if (addressingVersion == AddressingVersion.WSAddressing10)
  74. {
  75. return soap11Addressing10;
  76. }
  77. else if (addressingVersion == AddressingVersion.WSAddressingAugust2004)
  78. {
  79. return soap11Addressing200408;
  80. }
  81. else if (addressingVersion == AddressingVersion.None)
  82. {
  83. return soap11;
  84. }
  85. else
  86. {
  87. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("addressingVersion",
  88. SR.GetString(SR.AddressingVersionNotSupported, addressingVersion));
  89. }
  90. }
  91. else if (envelopeVersion == EnvelopeVersion.None)
  92. {
  93. if (addressingVersion == AddressingVersion.None)
  94. {
  95. return none;
  96. }
  97. else
  98. {
  99. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("addressingVersion",
  100. SR.GetString(SR.AddressingVersionNotSupported, addressingVersion));
  101. }
  102. }
  103. else
  104. {
  105. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("envelopeVersion",
  106. SR.GetString(SR.EnvelopeVersionNotSupported, envelopeVersion));
  107. }
  108. }
  109. public AddressingVersion Addressing
  110. {
  111. get { return addressing; }
  112. }
  113. public static MessageVersion Default
  114. {
  115. get { return soap12Addressing10; }
  116. }
  117. public EnvelopeVersion Envelope
  118. {
  119. get { return envelope; }
  120. }
  121. public override bool Equals(object obj)
  122. {
  123. return this == obj;
  124. }
  125. public override int GetHashCode()
  126. {
  127. int code = 0;
  128. if (this.Envelope == EnvelopeVersion.Soap11)
  129. code += 1;
  130. if (this.Addressing == AddressingVersion.WSAddressingAugust2004)
  131. code += 2;
  132. return code;
  133. }
  134. public static MessageVersion None
  135. {
  136. get { return none; }
  137. }
  138. public static MessageVersion Soap12WSAddressing10
  139. {
  140. get { return soap12Addressing10; }
  141. }
  142. public static MessageVersion Soap11WSAddressing10
  143. {
  144. get { return soap11Addressing10; }
  145. }
  146. public static MessageVersion Soap12WSAddressingAugust2004
  147. {
  148. get { return soap12Addressing200408; }
  149. }
  150. public static MessageVersion Soap11WSAddressingAugust2004
  151. {
  152. get { return soap11Addressing200408; }
  153. }
  154. public static MessageVersion Soap11
  155. {
  156. get { return soap11; }
  157. }
  158. public static MessageVersion Soap12
  159. {
  160. get { return soap12; }
  161. }
  162. public override string ToString()
  163. {
  164. return SR.GetString(SR.MessageVersionToStringFormat, envelope.ToString(), addressing.ToString());
  165. }
  166. internal bool IsMatch(MessageVersion messageVersion)
  167. {
  168. if (messageVersion == null)
  169. {
  170. Fx.Assert("Invalid (null) messageVersion value");
  171. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("messageVersion");
  172. }
  173. if (addressing == null)
  174. {
  175. Fx.Assert("Invalid (null) addressing value");
  176. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "MessageVersion.Addressing cannot be null")));
  177. }
  178. if (envelope != messageVersion.Envelope)
  179. return false;
  180. if (addressing.Namespace != messageVersion.Addressing.Namespace)
  181. return false;
  182. return true;
  183. }
  184. }
  185. }