CallbackContextMessageProperty.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Diagnostics;
  10. using System.Diagnostics.CodeAnalysis;
  11. using System.IO;
  12. using System.Net;
  13. using System.Runtime.Serialization;
  14. using System.Threading;
  15. using System.Xml;
  16. using System.Runtime;
  17. [Serializable]
  18. public class CallbackContextMessageProperty : IMessageProperty
  19. {
  20. const string PropertyName = "CallbackContextMessageProperty";
  21. // these hold init data from ctors
  22. [NonSerializedAttribute]
  23. readonly EndpointAddress listenAddress;
  24. readonly IDictionary<string, string> context;
  25. // used to cache "assembled" EndpointAddress that contains context property
  26. [NonSerializedAttribute]
  27. EndpointAddress callbackAddress;
  28. // if this constructor is used, the listen address will have to be provided later by setting it in the ContextBindingElement
  29. // CallbackContextMessageProperty will flow on the wire only if listenaddress is set.
  30. public CallbackContextMessageProperty(IDictionary<string, string> context)
  31. : this((EndpointAddress)null, context)
  32. {
  33. }
  34. public CallbackContextMessageProperty(string listenAddress, IDictionary<string, string> context)
  35. : this(new Uri(listenAddress), context)
  36. {
  37. }
  38. public CallbackContextMessageProperty(Uri listenAddress, IDictionary<string, string> context)
  39. : this(new EndpointAddress(listenAddress), context)
  40. {
  41. }
  42. public CallbackContextMessageProperty(EndpointAddress listenAddress, IDictionary<string, string> context)
  43. {
  44. if (listenAddress != null && listenAddress.Headers.FindHeader(ContextMessageHeader.ContextHeaderName, ContextMessageHeader.ContextHeaderNamespace) != null)
  45. {
  46. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.GetString(SR.ListenAddressAlreadyContainsContext));
  47. }
  48. this.listenAddress = listenAddress;
  49. this.context = context;
  50. }
  51. public CallbackContextMessageProperty(EndpointAddress callbackAddress)
  52. {
  53. if (callbackAddress == null)
  54. {
  55. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("callbackAddress");
  56. }
  57. this.callbackAddress = callbackAddress;
  58. }
  59. public static string Name
  60. {
  61. get
  62. {
  63. return PropertyName;
  64. }
  65. }
  66. public EndpointAddress CallbackAddress
  67. {
  68. get
  69. {
  70. if (this.callbackAddress == null && this.listenAddress != null)
  71. {
  72. this.callbackAddress = CreateCallbackAddress(this.listenAddress, this.context);
  73. }
  74. return this.callbackAddress;
  75. }
  76. }
  77. public IDictionary<string, string> Context
  78. {
  79. get
  80. {
  81. return this.context;
  82. }
  83. }
  84. public EndpointAddress CreateCallbackAddress(Uri listenAddress)
  85. {
  86. if (listenAddress == null)
  87. {
  88. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("listenAddress");
  89. }
  90. return CreateCallbackAddress(new EndpointAddress(listenAddress), this.context);
  91. }
  92. static EndpointAddress CreateCallbackAddress(EndpointAddress listenAddress, IDictionary<string, string> context)
  93. {
  94. if (listenAddress == null)
  95. {
  96. return null;
  97. }
  98. EndpointAddressBuilder builder = new EndpointAddressBuilder(listenAddress);
  99. if (context != null)
  100. {
  101. builder.Headers.Add(new ContextAddressHeader(context));
  102. }
  103. return builder.ToEndpointAddress();
  104. }
  105. public static bool TryGet(Message message, out CallbackContextMessageProperty contextMessageProperty)
  106. {
  107. if (message == null)
  108. {
  109. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
  110. }
  111. return TryGet(message.Properties, out contextMessageProperty);
  112. }
  113. public static bool TryGet(MessageProperties properties, out CallbackContextMessageProperty contextMessageProperty)
  114. {
  115. if (properties == null)
  116. {
  117. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("properties");
  118. }
  119. object value = null;
  120. if (properties.TryGetValue(PropertyName, out value))
  121. {
  122. contextMessageProperty = value as CallbackContextMessageProperty;
  123. }
  124. else
  125. {
  126. contextMessageProperty = null;
  127. }
  128. return contextMessageProperty != null;
  129. }
  130. public void AddOrReplaceInMessage(Message message)
  131. {
  132. if (message == null)
  133. {
  134. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
  135. }
  136. this.AddOrReplaceInMessageProperties(message.Properties);
  137. }
  138. public void AddOrReplaceInMessageProperties(MessageProperties properties)
  139. {
  140. if (properties == null)
  141. {
  142. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("properties");
  143. }
  144. properties[PropertyName] = this;
  145. }
  146. public IMessageProperty CreateCopy()
  147. {
  148. if (this.callbackAddress != null)
  149. {
  150. return new CallbackContextMessageProperty(this.callbackAddress);
  151. }
  152. else
  153. {
  154. return new CallbackContextMessageProperty(this.listenAddress, this.context);
  155. }
  156. }
  157. [SuppressMessage(FxCop.Category.Design, FxCop.Rule.AvoidOutParameters,
  158. Justification = "The method needs to return two objects with one parsing")]
  159. public void GetListenAddressAndContext(out EndpointAddress listenAddress, out IDictionary<string, string> context)
  160. {
  161. // we expect the callback address to be already set when this is called
  162. if (this.CallbackAddress == null)
  163. {
  164. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("callbackaddress");
  165. }
  166. EndpointAddressBuilder builder = new EndpointAddressBuilder(this.CallbackAddress);
  167. AddressHeader contextHeader = null;
  168. int contextHeaderIndex = -1;
  169. for (int i = 0; i < builder.Headers.Count; ++i)
  170. {
  171. if (builder.Headers[i].Name == ContextMessageHeader.ContextHeaderName && builder.Headers[i].Namespace == ContextMessageHeader.ContextHeaderNamespace)
  172. {
  173. if (contextHeader != null)
  174. {
  175. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(SR.GetString(SR.MultipleContextHeadersFoundInCallbackAddress)));
  176. }
  177. contextHeader = builder.Headers[i];
  178. contextHeaderIndex = i;
  179. }
  180. }
  181. if (contextHeader != null)
  182. {
  183. builder.Headers.RemoveAt(contextHeaderIndex);
  184. }
  185. context = (contextHeader != null) ? ContextMessageHeader.ParseContextHeader(contextHeader.GetAddressHeaderReader()).Context : null;
  186. listenAddress = builder.ToEndpointAddress();
  187. }
  188. }
  189. }