< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Description.ContractDescription
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Description/ContractDescription.cs
Line coverage
72%
Covered lines: 59
Uncovered lines: 22
Coverable lines: 81
Total lines: 203
Line coverage: 72.8%
Branch coverage
69%
Covered branches: 32
Total branches: 46
Branch coverage: 69.5%
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(...)100%44100%
GetInheritedContracts()100%66100%
GetContract(...)50%2280%
GetContract(...)50%4471.42%
EnsureInvariants()72.22%181856.52%
IsDuplex()75%4475%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Description/ContractDescription.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.Collections.ObjectModel;
 6using CoreWCF.Collections.Generic;
 7
 8namespace CoreWCF.Description
 9{
 10    public class ContractDescription
 11    {
 12        private XmlName _name;
 13        private string _ns;
 14        private SessionMode _sessionMode;
 15
 16        //ProtectionLevel protectionLevel;
 17        //bool hasProtectionLevel;
 18
 19
 20        public ContractDescription(string name)
 721            : this(name, null)
 22        {
 723        }
 24
 68525        public ContractDescription(string name, string ns)
 26        {
 27            // the property setter validates given value
 68528            Name = name;
 68529            if (!string.IsNullOrEmpty(ns))
 30            {
 67831                NamingHelper.CheckUriParameter(ns, nameof(ns));
 32            }
 33
 68534            Operations = new OperationDescriptionCollection();
 68535            _ns = ns ?? NamingHelper.DefaultNamespace; // ns can be ""
 68536        }
 37
 134038        public string ConfigurationName { get; set; }
 39
 1286540        public Type ContractType { get; set; }
 41
 140042        public Type CallbackContractType { get; set; }
 43
 44        public string Name
 45        {
 690746            get { return _name.EncodedName; }
 47            set
 48            {
 68549                if (value == null)
 50                {
 051                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value));
 52                }
 53
 68554                if (value.Length == 0)
 55                {
 056                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
 057                        new ArgumentOutOfRangeException(nameof(value), SR.SFxContractDescriptionNameCannotBeEmpty));
 58                }
 68559                _name = new XmlName(value, true /*isEncoded*/);
 68560            }
 61        }
 62
 63        public string Namespace
 64        {
 1288065            get { return _ns; }
 66            set
 67            {
 068                if (!string.IsNullOrEmpty(value))
 69                {
 070                    NamingHelper.CheckUriProperty(value, "Namespace");
 71                }
 72
 073                _ns = value;
 074            }
 75        }
 76
 8570277        public OperationDescriptionCollection Operations { get; }
 78
 4479        internal bool HasProtectionLevel => false;
 80
 81        public SessionMode SessionMode
 82        {
 209883            get { return _sessionMode; }
 84            set
 85            {
 67886                if (!SessionModeHelper.IsDefined(value))
 87                {
 088                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 89                }
 90
 67891                _sessionMode = value;
 67892            }
 93        }
 94
 95        public KeyedCollection<Type, IContractBehavior> ContractBehaviors
 96        {
 273997            get { return Behaviors; }
 98        }
 99
 7431100        internal KeyedByTypeCollection<IContractBehavior> Behaviors { get; } = new KeyedByTypeCollection<IContractBehavi
 101
 102        public Collection<ContractDescription> GetInheritedContracts()
 103        {
 1266104            Collection<ContractDescription> result = new Collection<ContractDescription>();
 13564105            for (int i = 0; i < Operations.Count; i++)
 106            {
 5516107                OperationDescription od = Operations[i];
 5516108                if (od.DeclaringContract != this)
 109                {
 301110                    ContractDescription inheritedContract = od.DeclaringContract;
 301111                    if (!result.Contains(inheritedContract))
 112                    {
 157113                        result.Add(inheritedContract);
 114                    }
 115                }
 116            }
 1266117            return result;
 118        }
 119
 120        public static ContractDescription GetContract<TService>(Type contractType) where TService : class
 121        {
 511122            if (contractType == null)
 123            {
 0124                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contractType));
 125            }
 126
 511127            var typeLoader = new TypeLoader<TService>();
 511128            ContractDescription description = typeLoader.LoadContractDescription(contractType);
 507129            return description;
 130        }
 131
 132        public static ContractDescription GetContract<TService>(Type contractType, object serviceImplementation) where T
 133        {
 89134            if (contractType == null)
 135            {
 0136                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contractType));
 137            }
 138
 89139            if (serviceImplementation == null)
 140            {
 0141                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(serviceImplementation));
 142            }
 143
 89144            var typeLoader = new TypeLoader<TService>();
 89145            ContractDescription description = typeLoader.LoadContractDescription(contractType, serviceImplementation);
 89146            return description;
 147        }
 148
 149        internal void EnsureInvariants()
 150        {
 641151            if (string.IsNullOrEmpty(Name))
 152            {
 0153                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
 0154                    SR.AChannelServiceEndpointSContractSNameIsNull0));
 155            }
 641156            if (Namespace == null)
 157            {
 0158                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
 0159                    SR.AChannelServiceEndpointSContractSNamespace0));
 160            }
 641161            if (Operations.Count == 0)
 162            {
 0163                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
 0164                    SR.Format(SR.SFxContractHasZeroOperations, Name)));
 165            }
 641166            bool thereIsAtLeastOneInitiatingOperation = false;
 7338167            for (int i = 0; i < Operations.Count; i++)
 168            {
 3028169                OperationDescription operationDescription = Operations[i];
 3028170                operationDescription.EnsureInvariants();
 3028171                if (operationDescription.IsInitiating)
 172                {
 3020173                    thereIsAtLeastOneInitiatingOperation = true;
 174                }
 175
 3028176                if ((!operationDescription.IsInitiating || operationDescription.IsTerminating)
 3028177                    && (SessionMode != SessionMode.Required))
 178                {
 0179                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
 0180                        SR.Format(SR.ContractIsNotSelfConsistentItHasOneOrMore2, Name)));
 181                }
 182            }
 641183            if (!thereIsAtLeastOneInitiatingOperation)
 184            {
 0185                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
 0186                    SR.Format(SR.SFxContractHasZeroInitiatingOperations, Name)));
 187            }
 641188        }
 189
 190        public bool IsDuplex()
 191        {
 758192            for (int i = 0; i < Operations.Count; ++i)
 193            {
 308194                if (Operations[i].IsServerInitiated())
 195                {
 0196                    return true;
 197                }
 198            }
 199
 71200            return false;
 201        }
 202    }
 203}