File: CustomTextMessageBindingElement.cs
Web Access
Project: src\src\System.Private.ServiceModel\tests\Scenarios\Extensibility\MessageEncoder\Extensibility.MessageEncoder.IntegrationTests.csproj (Extensibility.MessageEncoder.IntegrationTests)
// 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;
using System.ServiceModel.Channels;
using System.Xml;
 
namespace Extensibility.MessageEncoder.Tests
{
    public class CustomTextMessageBindingElement : MessageEncodingBindingElement
    {
        private MessageVersion _msgVersion;
        private string _mediaType;
        private string _encoding;
        private XmlDictionaryReaderQuotas _readerQuotas;
 
        private CustomTextMessageBindingElement(CustomTextMessageBindingElement binding)
            : this(binding.Encoding, binding.MediaType, binding.MessageVersion)
        {
            _readerQuotas = new XmlDictionaryReaderQuotas();
            binding.ReaderQuotas.CopyTo(_readerQuotas);
        }
 
        public CustomTextMessageBindingElement(string encoding, string mediaType, MessageVersion msgVersion)
        {
            if (encoding == null)
                throw new ArgumentNullException(nameof(encoding));
 
            if (mediaType == null)
                throw new ArgumentNullException(nameof(mediaType));
 
            if (msgVersion == null)
                throw new ArgumentNullException(nameof(msgVersion));
 
            _msgVersion = msgVersion;
            _mediaType = mediaType;
            _encoding = encoding;
            _readerQuotas = new XmlDictionaryReaderQuotas();
        }
 
        public CustomTextMessageBindingElement(string encoding, string mediaType)
    : this(encoding, mediaType, System.ServiceModel.Channels.MessageVersion.Soap12WSAddressing10)
        {
        }
 
        public CustomTextMessageBindingElement(string encoding)
            : this(encoding, "text/xml")
        {
        }
 
        public override MessageVersion MessageVersion
        {
            get
            {
                return _msgVersion;
            }
 
            set
            {
                if (value == null)
                    throw new ArgumentNullException(nameof(value));
                _msgVersion = value;
            }
        }
 
        public string MediaType
        {
            get
            {
                return _mediaType;
            }
 
            set
            {
                if (value == null)
                    throw new ArgumentNullException(nameof(value));
                _mediaType = value;
            }
        }
 
        public string Encoding
        {
            get
            {
                return _encoding;
            }
 
            set
            {
                if (value == null)
                    throw new ArgumentNullException(nameof(value));
                _encoding = value;
            }
        }
 
        // This encoder does not enforces any quotas for the unsecure messages. The 
        // quotas are enforced for the secure portions of messages when this encoder
        // is used in a binding that is configured with security. 
        public XmlDictionaryReaderQuotas ReaderQuotas
        {
            get
            {
                return _readerQuotas;
            }
        }
 
        public override MessageEncoderFactory CreateMessageEncoderFactory()
        {
            return new CustomTextMessageEncoderFactory(MediaType,
                Encoding, MessageVersion);
        }
 
        public override BindingElement Clone()
        {
            return new CustomTextMessageBindingElement(this);
        }
 
        public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
        {
            if (context == null)
                throw new ArgumentNullException(nameof(context));
 
            context.BindingParameters.Add(this);
            return context.BuildInnerChannelFactory<TChannel>();
        }
 
        public override bool CanBuildChannelFactory<TChannel>(BindingContext context)
        {
            if (context == null)
                throw new ArgumentNullException(nameof(context));
 
            return context.CanBuildInnerChannelFactory<TChannel>();
        }
 
        public override T GetProperty<T>(BindingContext context)
        {
            if (typeof(T) == typeof(XmlDictionaryReaderQuotas))
            {
                return (T)(object)_readerQuotas;
            }
 
            return base.GetProperty<T>(context);
        }
    }
}