< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.BinaryMessageEncodingBindingElement
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/BinaryMessageEncodingBindingElement.cs
Line coverage
61%
Covered lines: 79
Uncovered lines: 49
Coverable lines: 128
Total lines: 341
Line coverage: 61.7%
Branch coverage
32%
Covered branches: 18
Total branches: 56
Branch coverage: 32.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/BinaryMessageEncodingBindingElement.cs

#LineLine coverage
 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
 4using System;
 5using System.ComponentModel;
 6using System.Xml;
 7using CoreWCF.Configuration;
 8using CoreWCF.Description;
 9using CoreWCF.Runtime;
 10
 11namespace CoreWCF.Channels
 12{
 13    public sealed class BinaryMessageEncodingBindingElement : MessageEncodingBindingElement, IWsdlExportExtension, IPoli
 14    {
 15        private int _maxReadPoolSize;
 16        private int _maxWritePoolSize;
 17        private readonly XmlDictionaryReaderQuotas _readerQuotas;
 18        private int _maxSessionSize;
 19        private BinaryVersion _binaryVersion;
 20        private MessageVersion _messageVersion;
 21        private long _maxReceivedMessageSize;
 22
 44023        public BinaryMessageEncodingBindingElement()
 24        {
 44025            _maxReadPoolSize = EncoderDefaults.MaxReadPoolSize;
 44026            _maxWritePoolSize = EncoderDefaults.MaxWritePoolSize;
 44027            _readerQuotas = new XmlDictionaryReaderQuotas();
 44028            EncoderDefaults.ReaderQuotas.CopyTo(_readerQuotas);
 44029            _maxSessionSize = BinaryEncoderDefaults.MaxSessionSize;
 44030            _binaryVersion = BinaryEncoderDefaults.BinaryVersion;
 44031            _messageVersion = MessageVersion.CreateVersion(BinaryEncoderDefaults.EnvelopeVersion);
 44032            CompressionFormat = EncoderDefaults.DefaultCompressionFormat;
 44033        }
 34
 35        private BinaryMessageEncodingBindingElement(BinaryMessageEncodingBindingElement elementToBeCloned)
 217536            : base(elementToBeCloned)
 37        {
 217538            _maxReadPoolSize = elementToBeCloned._maxReadPoolSize;
 217539            _maxWritePoolSize = elementToBeCloned._maxWritePoolSize;
 217540            _readerQuotas = new XmlDictionaryReaderQuotas();
 217541            elementToBeCloned._readerQuotas.CopyTo(_readerQuotas);
 217542            MaxSessionSize = elementToBeCloned.MaxSessionSize;
 217543            BinaryVersion = elementToBeCloned.BinaryVersion;
 217544            _messageVersion = elementToBeCloned._messageVersion;
 217545            CompressionFormat = elementToBeCloned.CompressionFormat;
 217546            _maxReceivedMessageSize = elementToBeCloned._maxReceivedMessageSize;
 217547        }
 48
 49        [DefaultValue(EncoderDefaults.DefaultCompressionFormat)]
 534550        public CompressionFormat CompressionFormat { get; set; }
 51
 52        /* public */
 53        private BinaryVersion BinaryVersion
 54        {
 55            get
 56            {
 253057                return _binaryVersion;
 58            }
 59            set
 60            {
 217561                _binaryVersion = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullExce
 217562            }
 63        }
 64
 65        public override MessageVersion MessageVersion
 66        {
 95067            get { return _messageVersion; }
 68            set
 69            {
 5170                if (value == null)
 71                {
 072                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value));
 73                }
 5174                if (value.Envelope != BinaryEncoderDefaults.EnvelopeVersion)
 75                {
 076                    string errorMsg = SR.Format(SR.UnsupportedEnvelopeVersion, GetType().FullName, BinaryEncoderDefaults
 077                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(errorMsg));
 78                }
 79
 5180                _messageVersion = MessageVersion.CreateVersion(BinaryEncoderDefaults.EnvelopeVersion, value.Addressing);
 5181            }
 82        }
 83
 84        [DefaultValue(EncoderDefaults.MaxReadPoolSize)]
 85        public int MaxReadPoolSize
 86        {
 87            get
 88            {
 35789                return _maxReadPoolSize;
 90            }
 91            set
 92            {
 293                if (value <= 0)
 94                {
 095                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 096                                                    SRCommon.ValueMustBePositive));
 97                }
 298                _maxReadPoolSize = value;
 299            }
 100        }
 101
 102        [DefaultValue(EncoderDefaults.MaxWritePoolSize)]
 103        public int MaxWritePoolSize
 104        {
 105            get
 106            {
 357107                return _maxWritePoolSize;
 108            }
 109            set
 110            {
 2111                if (value <= 0)
 112                {
 0113                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 0114                                                    SRCommon.ValueMustBePositive));
 115                }
 2116                _maxWritePoolSize = value;
 2117            }
 118        }
 119
 120        public XmlDictionaryReaderQuotas ReaderQuotas
 121        {
 122            get
 123            {
 370124                return _readerQuotas;
 125            }
 126            set
 127            {
 0128                if (value == null)
 129                {
 0130                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value));
 131                }
 132
 0133                value.CopyTo(_readerQuotas);
 0134            }
 135        }
 136
 137        [DefaultValue(BinaryEncoderDefaults.MaxSessionSize)]
 138        public int MaxSessionSize
 139        {
 140            get
 141            {
 2532142                return _maxSessionSize;
 143            }
 144            set
 145            {
 2177146                if (value < 0)
 147                {
 0148                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 0149                                                    SRCommon.ValueMustBeNonNegative));
 150                }
 151
 2177152                _maxSessionSize = value;
 2177153            }
 154        }
 155
 156        private void VerifyCompression(BindingContext context)
 157        {
 127158            if (CompressionFormat != CompressionFormat.None)
 159            {
 18160                ITransportCompressionSupport compressionSupport = context.GetInnerProperty<ITransportCompressionSupport>
 18161                if (compressionSupport == null || !compressionSupport.IsCompressionFormatSupported(CompressionFormat))
 162                {
 0163                    throw Fx.Exception.AsError(new NotSupportedException(SR.Format(
 0164                        SR.TransportDoesNotSupportCompression, CompressionFormat.ToString(),
 0165                        GetType().Name,
 0166                        CompressionFormat.None.ToString())));
 167                }
 168            }
 127169        }
 170
 171        private void SetMaxReceivedMessageSizeFromTransport(BindingContext context)
 172        {
 127173            TransportBindingElement transport = context.Binding.Elements.Find<TransportBindingElement>();
 127174            if (transport != null)
 175            {
 176                // We are guaranteed that a transport exists when building a binding;
 177                // Allow the regular flow/checks to happen rather than throw here
 178                // (InternalBuildChannelListener will call into the BindingContext. Validation happens there and it will
 127179                _maxReceivedMessageSize = transport.MaxReceivedMessageSize;
 180            }
 127181        }
 182
 183        public override IServiceDispatcher BuildServiceDispatcher<TChannel>(BindingContext context, IServiceDispatcher i
 184        {
 127185            if (context == null)
 186            {
 0187                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(context));
 188            }
 189
 127190            if (innerDispatcher == null)
 191            {
 0192                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(innerDispatcher));
 193            }
 194
 127195            VerifyCompression(context);
 127196            SetMaxReceivedMessageSizeFromTransport(context);
 127197            return context.BuildNextServiceDispatcher<TChannel>(innerDispatcher);
 198        }
 199
 200        public override BindingElement Clone()
 201        {
 2175202            return new BinaryMessageEncodingBindingElement(this);
 203        }
 204
 205        public override MessageEncoderFactory CreateMessageEncoderFactory()
 206        {
 355207            return new BinaryMessageEncoderFactory(
 355208                MessageVersion,
 355209                MaxReadPoolSize,
 355210                MaxWritePoolSize,
 355211                MaxSessionSize,
 355212                ReaderQuotas,
 355213                _maxReceivedMessageSize,
 355214                BinaryVersion,
 355215                CompressionFormat);
 216        }
 217
 218        public override T GetProperty<T>(BindingContext context)
 219        {
 959220            if (context == null)
 221            {
 0222                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(context));
 223            }
 959224            if (typeof(T) == typeof(XmlDictionaryReaderQuotas))
 225            {
 0226                return (T)(object)_readerQuotas;
 227            }
 228            else
 229            {
 959230                return base.GetProperty<T>(context);
 231            }
 232        }
 233
 234        void IPolicyExportExtension.ExportPolicy(MetadataExporter exporter, PolicyConversionContext policyContext)
 235        {
 1236            if (policyContext == null)
 237            {
 0238                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(policyContext));
 239            }
 1240            XmlDocument document = new XmlDocument();
 1241            policyContext.GetBindingAssertions().Add(document.CreateElement(
 1242                MessageEncodingPolicyConstants.BinaryEncodingPrefix,
 1243                MessageEncodingPolicyConstants.BinaryEncodingName,
 1244                MessageEncodingPolicyConstants.BinaryEncodingNamespace));
 1245        }
 246
 0247        void IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext context) { }
 248        void IWsdlExportExtension.ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context)
 249        {
 1250            if (context == null)
 251            {
 0252                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(context));
 253            }
 254
 1255            SoapHelper.SetSoapVersion(context, exporter, MessageVersion.Soap12WSAddressing10.Envelope);
 1256        }
 257
 258        protected override bool IsMatch(BindingElement b)
 259        {
 0260            if (!base.IsMatch(b))
 261            {
 0262                return false;
 263            }
 264
 0265            if (!(b is BinaryMessageEncodingBindingElement binary))
 266            {
 0267                return false;
 268            }
 269
 0270            if (_maxReadPoolSize != binary.MaxReadPoolSize)
 271            {
 0272                return false;
 273            }
 274
 0275            if (_maxWritePoolSize != binary.MaxWritePoolSize)
 276            {
 0277                return false;
 278            }
 279
 280            // compare XmlDictionaryReaderQuotas
 0281            if (_readerQuotas.MaxStringContentLength != binary.ReaderQuotas.MaxStringContentLength)
 282            {
 0283                return false;
 284            }
 285
 0286            if (_readerQuotas.MaxArrayLength != binary.ReaderQuotas.MaxArrayLength)
 287            {
 0288                return false;
 289            }
 290
 0291            if (_readerQuotas.MaxBytesPerRead != binary.ReaderQuotas.MaxBytesPerRead)
 292            {
 0293                return false;
 294            }
 295
 0296            if (_readerQuotas.MaxDepth != binary.ReaderQuotas.MaxDepth)
 297            {
 0298                return false;
 299            }
 300
 0301            if (_readerQuotas.MaxNameTableCharCount != binary.ReaderQuotas.MaxNameTableCharCount)
 302            {
 0303                return false;
 304            }
 305
 0306            if (MaxSessionSize != binary.MaxSessionSize)
 307            {
 0308                return false;
 309            }
 310
 0311            if (CompressionFormat != binary.CompressionFormat)
 312            {
 0313                return false;
 314            }
 315
 0316            return true;
 317        }
 318
 319        [EditorBrowsable(EditorBrowsableState.Never)]
 320        public bool ShouldSerializeReaderQuotas()
 321        {
 0322            return !EncoderDefaults.IsDefaultReaderQuotas(ReaderQuotas);
 323        }
 324
 325        [EditorBrowsable(EditorBrowsableState.Never)]
 326        public bool ShouldSerializeMessageVersion()
 327        {
 0328            return !_messageVersion.IsMatch(MessageVersion.Default);
 329        }
 330
 331        private static class MessageEncodingPolicyConstants
 332        {
 333            public const string BinaryEncodingName = "BinaryEncoding";
 334            public const string BinaryEncodingNamespace = "http://schemas.microsoft.com/ws/06/2004/mspolicy/netbinary1";
 335            public const string BinaryEncodingPrefix = "msb";
 336            public const string OptimizedMimeSerializationNamespace = "http://schemas.xmlsoap.org/ws/2004/09/policy/opti
 337            public const string OptimizedMimeSerializationPrefix = "wsoma";
 338            public const string MtomEncodingName = "OptimizedMimeSerialization";
 339        }
 340    }
 341}