|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Text;
using Microsoft.Xml;
using System.ComponentModel;
namespace System.ServiceModel.Channels
{
public sealed class TextMessageEncodingBindingElement : MessageEncodingBindingElement
{
private int _maxReadPoolSize;
private int _maxWritePoolSize;
private XmlDictionaryReaderQuotas _readerQuotas;
private MessageVersion _messageVersion;
private Encoding _writeEncoding;
public TextMessageEncodingBindingElement()
: this(MessageVersion.Default, TextEncoderDefaults.Encoding)
{
}
public TextMessageEncodingBindingElement(MessageVersion messageVersion, Encoding writeEncoding)
{
if (messageVersion == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("messageVersion");
if (writeEncoding == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writeEncoding");
TextEncoderDefaults.ValidateEncoding(writeEncoding);
_maxReadPoolSize = EncoderDefaults.MaxReadPoolSize;
_maxWritePoolSize = EncoderDefaults.MaxWritePoolSize;
_readerQuotas = new XmlDictionaryReaderQuotas();
EncoderDefaults.ReaderQuotas.CopyTo(_readerQuotas);
_messageVersion = messageVersion;
_writeEncoding = writeEncoding;
}
private TextMessageEncodingBindingElement(TextMessageEncodingBindingElement elementToBeCloned)
: base(elementToBeCloned)
{
_maxReadPoolSize = elementToBeCloned._maxReadPoolSize;
_maxWritePoolSize = elementToBeCloned._maxWritePoolSize;
_readerQuotas = new XmlDictionaryReaderQuotas();
elementToBeCloned._readerQuotas.CopyTo(_readerQuotas);
_writeEncoding = elementToBeCloned._writeEncoding;
_messageVersion = elementToBeCloned._messageVersion;
}
[DefaultValue(EncoderDefaults.MaxReadPoolSize)]
public int MaxReadPoolSize
{
get
{
return _maxReadPoolSize;
}
set
{
if (value <= 0)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
SRServiceModel.ValueMustBePositive));
}
_maxReadPoolSize = value;
}
}
[DefaultValue(EncoderDefaults.MaxWritePoolSize)]
public int MaxWritePoolSize
{
get
{
return _maxWritePoolSize;
}
set
{
if (value <= 0)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("value", value,
SRServiceModel.ValueMustBePositive));
}
_maxWritePoolSize = value;
}
}
public XmlDictionaryReaderQuotas ReaderQuotas
{
get
{
return _readerQuotas;
}
set
{
if (value == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
value.CopyTo(_readerQuotas);
}
}
public override MessageVersion MessageVersion
{
get
{
return _messageVersion;
}
set
{
if (value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
}
_messageVersion = value;
}
}
public Encoding WriteEncoding
{
get
{
return _writeEncoding;
}
set
{
if (value == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
}
TextEncoderDefaults.ValidateEncoding(value);
_writeEncoding = value;
}
}
public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
{
return InternalBuildChannelFactory<TChannel>(context);
}
public override BindingElement Clone()
{
return new TextMessageEncodingBindingElement(this);
}
public override MessageEncoderFactory CreateMessageEncoderFactory()
{
return new TextMessageEncoderFactory(MessageVersion, WriteEncoding, this.MaxReadPoolSize, this.MaxWritePoolSize, 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)_readerQuotas;
}
else
{
return base.GetProperty<T>(context);
}
}
internal override bool CheckEncodingVersion(EnvelopeVersion version)
{
return _messageVersion.Envelope == version;
}
internal override bool IsMatch(BindingElement b)
{
if (!base.IsMatch(b))
return false;
TextMessageEncodingBindingElement text = b as TextMessageEncodingBindingElement;
if (text == null)
return false;
if (_maxReadPoolSize != text.MaxReadPoolSize)
return false;
if (_maxWritePoolSize != text.MaxWritePoolSize)
return false;
// compare XmlDictionaryReaderQuotas
if (_readerQuotas.MaxStringContentLength != text.ReaderQuotas.MaxStringContentLength)
return false;
if (_readerQuotas.MaxArrayLength != text.ReaderQuotas.MaxArrayLength)
return false;
if (_readerQuotas.MaxBytesPerRead != text.ReaderQuotas.MaxBytesPerRead)
return false;
if (_readerQuotas.MaxDepth != text.ReaderQuotas.MaxDepth)
return false;
if (_readerQuotas.MaxNameTableCharCount != text.ReaderQuotas.MaxNameTableCharCount)
return false;
if (this.WriteEncoding.WebName != text.WriteEncoding.WebName)
return false;
if (!this.MessageVersion.IsMatch(text.MessageVersion))
return false;
return true;
}
}
}
|