< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.Binding
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/Binding.cs
Line coverage
70%
Covered lines: 75
Uncovered lines: 31
Coverable lines: 106
Total lines: 286
Line coverage: 70.7%
Branch coverage
56%
Covered branches: 28
Total branches: 50
Branch coverage: 56%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
.ctor(...)66.66%6680%
ValidateSecurityCapabilities(...)0%220%
CanBuildServiceDispatcher(...)100%11100%
BuildServiceDispatcher(...)50%2280%
BuildServiceDispatcher(...)100%110%
BuildServiceDispatcher(...)50%2285.71%
GetProperty(...)100%11100%
EnsureInvariants()100%11100%
EnsureInvariants(...)57.14%141447.61%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Channels/Binding.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 CoreWCF.Configuration;
 6using CoreWCF.Description;
 7using CoreWCF.Runtime;
 8
 9namespace CoreWCF.Channels
 10{
 11    public abstract class Binding : IDefaultCommunicationTimeouts
 12    {
 1162013        private TimeSpan _closeTimeout = ServiceDefaults.CloseTimeout;
 14        private string _name;
 15        private string _namespaceIdentifier;
 1162016        private TimeSpan _openTimeout = ServiceDefaults.OpenTimeout;
 1162017        private TimeSpan _receiveTimeout = ServiceDefaults.ReceiveTimeout;
 1162018        private TimeSpan _sendTimeout = ServiceDefaults.SendTimeout;
 19        internal const string DefaultNamespace = NamingHelper.DefaultNamespace;
 20
 1156421        protected Binding()
 22        {
 1156423            _name = null;
 1156424            _namespaceIdentifier = DefaultNamespace;
 1156425        }
 26
 5627        protected Binding(string name, string ns)
 28        {
 5629            if (string.IsNullOrEmpty(name))
 30            {
 031                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(name), SR.SFXBindingNameCannotBeNull
 32            }
 5633            if (ns == null)
 34            {
 035                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(ns));
 36            }
 37
 5638            if (ns.Length > 0)
 39            {
 5640                NamingHelper.CheckUriParameter(ns, nameof(ns));
 41            }
 42
 5643            _name = name;
 5644            _namespaceIdentifier = ns;
 5645        }
 46
 47        public TimeSpan CloseTimeout
 48        {
 1299449            get { return _closeTimeout; }
 50            set
 51            {
 918352                if (value < TimeSpan.Zero)
 53                {
 054                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 55                }
 918356                if (TimeoutHelper.IsTooLarge(value))
 57                {
 058                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 59                }
 60
 918361                _closeTimeout = value;
 918362            }
 63        }
 64
 65        public string Name
 66        {
 67            get
 68            {
 991369                if (_name == null)
 70                {
 60171                    _name = GetType().Name;
 72                }
 73
 991374                return _name;
 75            }
 76            set
 77            {
 918478                if (string.IsNullOrEmpty(value))
 79                {
 080                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(value), SR.SFXBindingNameCannotB
 81                }
 82
 918483                _name = value;
 918484            }
 85        }
 86
 87        public string Namespace
 88        {
 984789            get { return _namespaceIdentifier; }
 90            set
 91            {
 916892                if (value == null)
 93                {
 094                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value));
 95                }
 96
 916897                if (value.Length > 0)
 98                {
 913099                    NamingHelper.CheckUriProperty(value, "Namespace");
 100                }
 9168101                _namespaceIdentifier = value;
 9168102            }
 103        }
 104
 105        public TimeSpan OpenTimeout
 106        {
 10338107            get { return _openTimeout; }
 108            set
 109            {
 9183110                if (value < TimeSpan.Zero)
 111                {
 0112                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 113                }
 9183114                if (TimeoutHelper.IsTooLarge(value))
 115                {
 0116                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 117                }
 118
 9183119                _openTimeout = value;
 9183120            }
 121        }
 122
 123        public TimeSpan ReceiveTimeout
 124        {
 10989125            get { return _receiveTimeout; }
 126            set
 127            {
 9209128                if (value < TimeSpan.Zero)
 129                {
 0130                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 131                }
 9209132                if (TimeoutHelper.IsTooLarge(value))
 133                {
 0134                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 135                }
 136
 9209137                _receiveTimeout = value;
 9209138            }
 139        }
 140
 141        public abstract string Scheme { get; }
 142
 143        public MessageVersion MessageVersion
 144        {
 145            get
 146            {
 4794147                return GetProperty<MessageVersion>(new BindingParameterCollection());
 148            }
 149        }
 150
 151        public TimeSpan SendTimeout
 152        {
 11337153            get { return _sendTimeout; }
 154            set
 155            {
 9183156                if (value < TimeSpan.Zero)
 157                {
 0158                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 159                }
 9183160                if (TimeoutHelper.IsTooLarge(value))
 161                {
 0162                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 163                }
 164
 9183165                _sendTimeout = value;
 9183166            }
 167        }
 168
 169        private void ValidateSecurityCapabilities(ISecurityCapabilities runtimeSecurityCapabilities, BindingParameterCol
 170        {
 0171            ISecurityCapabilities bindingSecurityCapabilities = GetProperty<ISecurityCapabilities>(parameters);
 172
 0173            if (!SecurityCapabilities.IsEqual(bindingSecurityCapabilities, runtimeSecurityCapabilities))
 174            {
 0175                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
 0176                    new InvalidOperationException(SR.Format(SR.SecurityCapabilitiesMismatched, this)));
 177            }
 0178        }
 179
 180        public virtual bool CanBuildServiceDispatcher<TChannel>(BindingParameterCollection parameters) where TChannel : 
 181        {
 1850182            BindingContext context = new BindingContext(new CustomBinding(this), parameters);
 1850183            return context.CanBuildNextServiceDispatcher<TChannel>();
 184        }
 185
 186        public virtual IServiceDispatcher BuildServiceDispatcher<TChannel>(BindingParameterCollection parameters, IServi
 187where TChannel : class, IChannel
 188        {
 189            UriBuilder listenUriBuilder;
 593190            if (dispatcher.BaseAddress == null)
 191            {
 0192                listenUriBuilder = new UriBuilder(Scheme, DnsCache.MachineName);
 193            }
 194            else
 195            {
 593196                listenUriBuilder = new UriBuilder(dispatcher.BaseAddress);
 593197                listenUriBuilder.Scheme = Scheme;
 198            }
 199
 593200            return BuildServiceDispatcher<TChannel>(listenUriBuilder.Uri, string.Empty, parameters, dispatcher);
 201        }
 202
 203        public virtual IServiceDispatcher BuildServiceDispatcher<TChannel>(Uri listenUriBaseAddress, BindingParameterCol
 204where TChannel : class, IChannel
 205        {
 0206            return BuildServiceDispatcher<TChannel>(listenUriBaseAddress, string.Empty, parameters, dispatcher);
 207        }
 208
 209        public virtual IServiceDispatcher BuildServiceDispatcher<TChannel>(Uri listenUriBaseAddress, string listenUriRel
 210            where TChannel : class, IChannel
 211        {
 593212            EnsureInvariants();
 593213            if (!(this is CustomBinding binding))
 214            {
 0215                binding = new CustomBinding(this);
 216            }
 217
 593218            BindingContext context = new BindingContext(binding, parameters, listenUriBaseAddress, listenUriRelativeAddr
 593219            IServiceDispatcher serviceDispatcher = context.BuildNextServiceDispatcher<TChannel>(dispatcher);
 593220            context.ValidateBindingElementsConsumed();
 221
 222            // TODO: Work out how to validate security capabilities
 223            //this.ValidateSecurityCapabilities(serviceDispatcher.GetProperty<ISecurityCapabilities>(), parameters);
 224
 593225            return serviceDispatcher;
 226        }
 227
 228        public abstract BindingElementCollection CreateBindingElements();
 229
 230        public T GetProperty<T>(BindingParameterCollection parameters) where T : class
 231        {
 6036232            BindingContext context = new BindingContext(new CustomBinding(this), parameters);
 6036233            return context.GetInnerProperty<T>();
 234        }
 235
 236        private void EnsureInvariants()
 237        {
 593238            EnsureInvariants(null);
 593239        }
 240
 241        internal void EnsureInvariants(string contractName)
 242        {
 1234243            BindingElementCollection elements = CreateBindingElements();
 1234244            TransportBindingElement transport = null;
 245            int index;
 5044246            for (index = 0; index < elements.Count; index++)
 247            {
 2522248                transport = elements[index] as TransportBindingElement;
 2522249                if (transport != null)
 250                {
 251                    break;
 252                }
 253            }
 254
 1234255            if (transport == null)
 256            {
 0257                if (contractName == null)
 258                {
 0259                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
 0260                        SR.Format(SR.CustomBindingRequiresTransport, Name)));
 261                }
 262                else
 263                {
 0264                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
 0265                        SR.Format(SR.SFxCustomBindingNeedsTransport1, contractName)));
 266                }
 267            }
 1234268            if (index != elements.Count - 1)
 269            {
 0270                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
 0271                    SR.Format(SR.TransportBindingElementMustBeLast, Name, transport.GetType().Name)));
 272            }
 1234273            if (string.IsNullOrEmpty(transport.Scheme))
 274            {
 0275                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
 0276                    SR.Format(SR.InvalidBindingScheme, transport.GetType().Name)));
 277            }
 278
 1234279            if (MessageVersion == null)
 280            {
 0281                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
 0282                    SR.Format(SR.MessageVersionMissingFromBinding, Name)));
 283            }
 1234284        }
 285    }
 286}