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