MtomMessageEncodingBindingElement.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.Collections.Generic;
  7. using System.ServiceModel.Description;
  8. using System.Runtime.Serialization;
  9. using System.ServiceModel.Channels;
  10. using System.ServiceModel;
  11. using System.Text;
  12. using System.Xml;
  13. using System.ComponentModel;
  14. public sealed class MtomMessageEncodingBindingElement : MessageEncodingBindingElement, IWsdlExportExtension, IPolicyExportExtension
  15. {
  16. int maxReadPoolSize;
  17. int maxWritePoolSize;
  18. XmlDictionaryReaderQuotas readerQuotas;
  19. int maxBufferSize;
  20. Encoding writeEncoding;
  21. MessageVersion messageVersion;
  22. public MtomMessageEncodingBindingElement()
  23. : this(MessageVersion.Default, TextEncoderDefaults.Encoding)
  24. {
  25. }
  26. public MtomMessageEncodingBindingElement(MessageVersion messageVersion, Encoding writeEncoding)
  27. {
  28. if (messageVersion == null)
  29. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("messageVersion");
  30. if (messageVersion == MessageVersion.None)
  31. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.MtomEncoderBadMessageVersion, messageVersion.ToString()), "messageVersion"));
  32. if (writeEncoding == null)
  33. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writeEncoding");
  34. TextEncoderDefaults.ValidateEncoding(writeEncoding);
  35. this.maxReadPoolSize = EncoderDefaults.MaxReadPoolSize;
  36. this.maxWritePoolSize = EncoderDefaults.MaxWritePoolSize;
  37. this.readerQuotas = new XmlDictionaryReaderQuotas();
  38. EncoderDefaults.ReaderQuotas.CopyTo(this.readerQuotas);
  39. this.maxBufferSize = MtomEncoderDefaults.MaxBufferSize;
  40. this.messageVersion = messageVersion;
  41. this.writeEncoding = writeEncoding;
  42. }
  43. MtomMessageEncodingBindingElement(MtomMessageEncodingBindingElement elementToBeCloned)
  44. : base(elementToBeCloned)
  45. {
  46. this.maxReadPoolSize = elementToBeCloned.maxReadPoolSize;
  47. this.maxWritePoolSize = elementToBeCloned.maxWritePoolSize;
  48. this.readerQuotas = new XmlDictionaryReaderQuotas();
  49. elementToBeCloned.readerQuotas.CopyTo(this.readerQuotas);
  50. this.maxBufferSize = elementToBeCloned.maxBufferSize;
  51. this.writeEncoding = elementToBeCloned.writeEncoding;
  52. this.messageVersion = elementToBeCloned.messageVersion;
  53. }
  54. [DefaultValue(EncoderDefaults.MaxReadPoolSize)]
  55. public int MaxReadPoolSize
  56. {
  57. get
  58. {
  59. return this.maxReadPoolSize;
  60. }
  61. set
  62. {
  63. if (value <= 0)
  64. {
  65. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
  66. SR.GetString(SR.ValueMustBePositive)));
  67. }
  68. this.maxReadPoolSize = value;
  69. }
  70. }
  71. [DefaultValue(EncoderDefaults.MaxWritePoolSize)]
  72. public int MaxWritePoolSize
  73. {
  74. get
  75. {
  76. return this.maxWritePoolSize;
  77. }
  78. set
  79. {
  80. if (value <= 0)
  81. {
  82. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
  83. SR.GetString(SR.ValueMustBePositive)));
  84. }
  85. this.maxWritePoolSize = value;
  86. }
  87. }
  88. public XmlDictionaryReaderQuotas ReaderQuotas
  89. {
  90. get
  91. {
  92. return this.readerQuotas;
  93. }
  94. }
  95. [DefaultValue(MtomEncoderDefaults.MaxBufferSize)]
  96. public int MaxBufferSize
  97. {
  98. get
  99. {
  100. return this.maxBufferSize;
  101. }
  102. set
  103. {
  104. if (value <= 0)
  105. {
  106. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
  107. SR.GetString(SR.ValueMustBePositive)));
  108. }
  109. this.maxBufferSize = value;
  110. }
  111. }
  112. [TypeConverter(typeof(System.ServiceModel.Configuration.EncodingConverter))]
  113. public Encoding WriteEncoding
  114. {
  115. get
  116. {
  117. return this.writeEncoding;
  118. }
  119. set
  120. {
  121. if (value == null)
  122. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  123. TextEncoderDefaults.ValidateEncoding(value);
  124. this.writeEncoding = value;
  125. }
  126. }
  127. public override MessageVersion MessageVersion
  128. {
  129. get
  130. {
  131. return this.messageVersion;
  132. }
  133. set
  134. {
  135. if (value == null)
  136. {
  137. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  138. }
  139. if (value == MessageVersion.None)
  140. {
  141. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.MtomEncoderBadMessageVersion, value.ToString()), "value"));
  142. }
  143. this.messageVersion = value;
  144. }
  145. }
  146. public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
  147. {
  148. return InternalBuildChannelFactory<TChannel>(context);
  149. }
  150. public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
  151. {
  152. return InternalCanBuildChannelFactory<TChannel>(context);
  153. }
  154. public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context)
  155. {
  156. return InternalBuildChannelListener<TChannel>(context);
  157. }
  158. public override bool CanBuildChannelListener<TChannel>(BindingContext context)
  159. {
  160. return InternalCanBuildChannelListener<TChannel>(context);
  161. }
  162. public override BindingElement Clone()
  163. {
  164. return new MtomMessageEncodingBindingElement(this);
  165. }
  166. public override MessageEncoderFactory CreateMessageEncoderFactory()
  167. {
  168. return new MtomMessageEncoderFactory(MessageVersion, WriteEncoding, this.MaxReadPoolSize, this.MaxWritePoolSize, this.MaxBufferSize, this.ReaderQuotas);
  169. }
  170. public override T GetProperty<T>(BindingContext context)
  171. {
  172. if (context == null)
  173. {
  174. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  175. }
  176. if (typeof(T) == typeof(XmlDictionaryReaderQuotas))
  177. {
  178. return (T)(object)this.readerQuotas;
  179. }
  180. else
  181. {
  182. return base.GetProperty<T>(context);
  183. }
  184. }
  185. void IPolicyExportExtension.ExportPolicy(MetadataExporter exporter, PolicyConversionContext policyContext)
  186. {
  187. if (policyContext == null)
  188. {
  189. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("policyContext");
  190. }
  191. XmlDocument document = new XmlDocument();
  192. policyContext.GetBindingAssertions().Add(document.CreateElement(
  193. MessageEncodingPolicyConstants.OptimizedMimeSerializationPrefix,
  194. MessageEncodingPolicyConstants.MtomEncodingName,
  195. MessageEncodingPolicyConstants.OptimizedMimeSerializationNamespace));
  196. }
  197. void IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext context) { }
  198. void IWsdlExportExtension.ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context)
  199. {
  200. if (context == null)
  201. {
  202. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("context");
  203. }
  204. SoapHelper.SetSoapVersion(context, exporter, this.messageVersion.Envelope);
  205. }
  206. internal override bool CheckEncodingVersion(EnvelopeVersion version)
  207. {
  208. return messageVersion.Envelope == version;
  209. }
  210. internal override bool IsMatch(BindingElement b)
  211. {
  212. if (!base.IsMatch(b))
  213. return false;
  214. MtomMessageEncodingBindingElement mtom = b as MtomMessageEncodingBindingElement;
  215. if (mtom == null)
  216. return false;
  217. if (this.maxReadPoolSize != mtom.MaxReadPoolSize)
  218. return false;
  219. if (this.maxWritePoolSize != mtom.MaxWritePoolSize)
  220. return false;
  221. // compare XmlDictionaryReaderQuotas
  222. if (this.readerQuotas.MaxStringContentLength != mtom.ReaderQuotas.MaxStringContentLength)
  223. return false;
  224. if (this.readerQuotas.MaxArrayLength != mtom.ReaderQuotas.MaxArrayLength)
  225. return false;
  226. if (this.readerQuotas.MaxBytesPerRead != mtom.ReaderQuotas.MaxBytesPerRead)
  227. return false;
  228. if (this.readerQuotas.MaxDepth != mtom.ReaderQuotas.MaxDepth)
  229. return false;
  230. if (this.readerQuotas.MaxNameTableCharCount != mtom.ReaderQuotas.MaxNameTableCharCount)
  231. return false;
  232. if (this.maxBufferSize != mtom.MaxBufferSize)
  233. return false;
  234. if (this.WriteEncoding.EncodingName != mtom.WriteEncoding.EncodingName)
  235. return false;
  236. if (!this.MessageVersion.IsMatch(mtom.MessageVersion))
  237. return false;
  238. return true;
  239. }
  240. [EditorBrowsable(EditorBrowsableState.Never)]
  241. public bool ShouldSerializeMessageVersion()
  242. {
  243. return (!this.messageVersion.IsMatch(MessageVersion.Default));
  244. }
  245. [EditorBrowsable(EditorBrowsableState.Never)]
  246. public bool ShouldSerializeReaderQuotas()
  247. {
  248. return (!EncoderDefaults.IsDefaultReaderQuotas(this.ReaderQuotas));
  249. }
  250. [EditorBrowsable(EditorBrowsableState.Never)]
  251. public bool ShouldSerializeWriteEncoding()
  252. {
  253. return (this.WriteEncoding != TextEncoderDefaults.Encoding);
  254. }
  255. }
  256. }