TextMessageEncodingBindingElement.cs 8.8 KB

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