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