< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.ClientRuntime
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/ClientRuntime.cs
Line coverage
42%
Covered lines: 87
Uncovered lines: 118
Coverable lines: 205
Total lines: 554
Line coverage: 42.4%
Branch coverage
26%
Covered branches: 12
Total branches: 46
Branch coverage: 26%
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(...)50%22100%
.ctor(...)100%110%
GetArray(...)0%220%
GetRuntime()100%22100%
InvalidateRuntime()100%11100%
LockDownProperties()100%110%
NewBehaviorCollection()100%11100%
IsFault(...)50%8857.14%
IsMessageVersionNoneFault(...)0%440%
.ctor(...)100%11100%
ClearItems()100%110%
InsertItem(...)0%220%
RemoveItem(...)100%110%
SetItem(...)0%220%
.ctor(...)100%11100%
ClearItems()100%110%
GetKeyForItem(...)100%11100%
InsertItem(...)50%4471.42%
RemoveItem(...)100%110%
SetItem(...)0%440%
InternalClearItems()100%110%
InternalGetKeyForItem(...)100%110%
InternalInsertItem(...)100%110%
InternalRemoveItem(...)100%110%
InternalSetItem(...)100%110%
.ctor(...)100%110%
ClearItems()100%110%
GetKeyForItem(...)100%110%
InsertItem(...)100%110%
RemoveItem(...)100%110%
SetItem(...)100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/ClientRuntime.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.Generic;
 6using System.Collections.ObjectModel;
 7using System.ComponentModel;
 8using System.Xml;
 9using CoreWCF.Channels;
 10using CoreWCF.Collections.Generic;
 11using CoreWCF.Runtime;
 12using CoreWCF.Security;
 13
 14namespace CoreWCF.Dispatcher
 15{
 16    public sealed class ClientRuntime
 17    {
 18        //bool addTransactionFlowProperties = true;
 19        internal SynchronizedCollection<IClientMessageInspector> _messageInspectors;
 20        internal SynchronizedKeyedCollection<string, ClientOperation> _operations;
 21        private Type _callbackProxyType;
 22        private readonly ProxyBehaviorCollection<IChannelInitializer> _channelInitializers;
 23        private Type _contractProxyType;
 24        private IdentityVerifier _identityVerifier;
 25        private IClientOperationSelector _operationSelector;
 26        private ImmutableClientRuntime _runtime;
 50527        private bool _useSynchronizationContext = true;
 28        private Uri _via;
 29        private readonly SharedRuntimeState _shared;
 30        private int _maxFaultSize;
 31        private bool _messageVersionNoneFaultsEnabled;
 32
 33        internal ClientRuntime(DispatchRuntime dispatchRuntime, SharedRuntimeState shared)
 50534            : this(dispatchRuntime.EndpointDispatcher.ContractName,
 50535                   dispatchRuntime.EndpointDispatcher.ContractNamespace,
 50536                   shared)
 37        {
 50538            DispatchRuntime = dispatchRuntime ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof
 50539            _shared = shared;
 40
 41            Fx.Assert(shared.IsOnServer, "Server constructor called on client?");
 50542        }
 43
 44        internal ClientRuntime(string contractName, string contractNamespace)
 045            : this(contractName, contractNamespace, new SharedRuntimeState(false))
 46        {
 47            Fx.Assert(!_shared.IsOnServer, "Client constructor called on server?");
 048        }
 49
 50550        private ClientRuntime(string contractName, string contractNamespace, SharedRuntimeState shared)
 51        {
 50552            ContractName = contractName;
 50553            ContractNamespace = contractNamespace;
 50554            _shared = shared;
 55
 50556            OperationCollection operations = new OperationCollection(this);
 50557            _operations = operations;
 50558            _channelInitializers = new ProxyBehaviorCollection<IChannelInitializer>(this);
 50559            _messageInspectors = new ProxyBehaviorCollection<IClientMessageInspector>(this);
 60
 50561            UnhandledClientOperation = new ClientOperation(this, "*", MessageHeaders.WildcardAction, MessageHeaders.Wild
 50562            {
 50563                InternalFormatter = new MessageOperationFormatter()
 50564            };
 50565            _maxFaultSize = TransportDefaults.MaxFaultSize;
 50566        }
 67
 68        //internal bool AddTransactionFlowProperties
 69        //{
 70        //    get { return this.addTransactionFlowProperties; }
 71        //    set
 72        //    {
 73        //        lock (this.ThisLock)
 74        //        {
 75        //            this.InvalidateRuntime();
 76        //            this.addTransactionFlowProperties = value;
 77        //        }
 78        //    }
 79        //}
 80
 81        public Type CallbackClientType
 82        {
 51683            get { return _callbackProxyType; }
 84            set
 85            {
 286                lock (ThisLock)
 87                {
 288                    InvalidateRuntime();
 289                    _callbackProxyType = value;
 290                }
 291            }
 92        }
 93
 94        public SynchronizedCollection<IChannelInitializer> ChannelInitializers
 95        {
 196            get { return _channelInitializers; }
 97        }
 98
 199        public string ContractName { get; }
 100
 1101        public string ContractNamespace { get; }
 102
 103        public Type ContractClientType
 104        {
 516105            get { return _contractProxyType; }
 106            set
 107            {
 2108                lock (ThisLock)
 109                {
 2110                    InvalidateRuntime();
 2111                    _contractProxyType = value;
 2112                }
 2113            }
 114        }
 115
 116        internal IdentityVerifier IdentityVerifier
 117        {
 118            get
 119            {
 0120                if (_identityVerifier == null)
 121                {
 0122                    _identityVerifier = IdentityVerifier.CreateDefault();
 123                }
 124
 0125                return _identityVerifier;
 126            }
 127            set
 128            {
 0129                InvalidateRuntime();
 130
 0131                _identityVerifier = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(val
 0132            }
 133        }
 134
 135        public Uri Via
 136        {
 0137            get { return _via; }
 138            set
 139            {
 0140                lock (ThisLock)
 141                {
 0142                    InvalidateRuntime();
 0143                    _via = value;
 0144                }
 0145            }
 146        }
 147
 148        public bool ValidateMustUnderstand
 149        {
 1150            get { return _shared.ValidateMustUnderstand; }
 151            set
 152            {
 0153                lock (ThisLock)
 154                {
 0155                    InvalidateRuntime();
 0156                    _shared.ValidateMustUnderstand = value;
 0157                }
 0158            }
 159        }
 160
 161        public bool MessageVersionNoneFaultsEnabled
 162        {
 163            get
 164            {
 1165                return _messageVersionNoneFaultsEnabled;
 166            }
 167            set
 168            {
 0169                InvalidateRuntime();
 0170                _messageVersionNoneFaultsEnabled = value;
 0171            }
 172        }
 173
 585174        internal DispatchRuntime DispatchRuntime { get; private set; }
 175
 176        public DispatchRuntime CallbackDispatchRuntime
 177        {
 178            get
 179            {
 0180                if (DispatchRuntime == null)
 181                {
 0182                    DispatchRuntime = new DispatchRuntime(this, _shared);
 183                }
 184
 0185                return DispatchRuntime;
 186            }
 187        }
 188
 189        internal bool EnableFaults
 190        {
 191            get
 192            {
 0193                if (IsOnServer)
 194                {
 0195                    return DispatchRuntime.EnableFaults;
 196                }
 197                else
 198                {
 0199                    return _shared.EnableFaults;
 200                }
 201            }
 202            set
 203            {
 0204                lock (ThisLock)
 205                {
 0206                    if (IsOnServer)
 207                    {
 0208                        string text = SR.SFxSetEnableFaultsOnChannelDispatcher0;
 0209                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(text));
 210                    }
 211                    else
 212                    {
 0213                        InvalidateRuntime();
 0214                        _shared.EnableFaults = value;
 215                    }
 0216                }
 0217            }
 218        }
 219
 220        public int MaxFaultSize
 221        {
 222            get
 223            {
 0224                return _maxFaultSize;
 225            }
 226            set
 227            {
 0228                InvalidateRuntime();
 0229                _maxFaultSize = value;
 0230            }
 231        }
 232
 233        internal bool IsOnServer
 234        {
 1235            get { return _shared.IsOnServer; }
 236        }
 237
 238        public bool ManualAddressing
 239        {
 240            get
 241            {
 1242                if (IsOnServer)
 243                {
 1244                    return DispatchRuntime.ManualAddressing;
 245                }
 246                else
 247                {
 0248                    return _shared.ManualAddressing;
 249                }
 250            }
 251            set
 252            {
 0253                lock (ThisLock)
 254                {
 0255                    if (IsOnServer)
 256                    {
 0257                        string text = SR.SFxSetManualAddressingOnChannelDispatcher0;
 0258                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(text));
 259                    }
 260                    else
 261                    {
 0262                        InvalidateRuntime();
 0263                        _shared.ManualAddressing = value;
 264                    }
 0265                }
 0266            }
 267        }
 268
 269        internal int MaxParameterInspectors
 270        {
 271            get
 272            {
 1273                lock (ThisLock)
 274                {
 1275                    int max = 0;
 276
 4277                    for (int i = 0; i < _operations.Count; i++)
 278                    {
 1279                        max = Math.Max(max, _operations[i].ParameterInspectors.Count);
 280                    }
 281
 1282                    return max;
 283                }
 1284            }
 285        }
 286
 287        public ICollection<IClientMessageInspector> ClientMessageInspectors
 288        {
 0289            get { return MessageInspectors; }
 290        }
 291
 292        internal SynchronizedCollection<IClientMessageInspector> MessageInspectors
 293        {
 1294            get { return _messageInspectors; }
 295        }
 296
 297        public ICollection<ClientOperation> ClientOperations
 298        {
 1299            get { return Operations; }
 300        }
 301
 302        internal SynchronizedKeyedCollection<string, ClientOperation> Operations
 303        {
 8304            get { return _operations; }
 305        }
 306
 307        internal IClientOperationSelector OperationSelector
 308        {
 1309            get { return _operationSelector; }
 310            set
 311            {
 2312                lock (ThisLock)
 313                {
 2314                    InvalidateRuntime();
 2315                    _operationSelector = value;
 2316                }
 2317            }
 318        }
 319
 320        internal object ThisLock
 321        {
 2593322            get { return _shared; }
 323        }
 324
 1325        public ClientOperation UnhandledClientOperation { get; }
 326
 327        internal bool UseSynchronizationContext
 328        {
 1329            get { return _useSynchronizationContext; }
 330            set
 331            {
 0332                lock (ThisLock)
 333                {
 0334                    InvalidateRuntime();
 0335                    _useSynchronizationContext = value;
 0336                }
 0337            }
 338        }
 339
 340        internal T[] GetArray<T>(SynchronizedCollection<T> collection)
 341        {
 0342            lock (collection.SyncRoot)
 343            {
 0344                if (collection.Count == 0)
 345                {
 0346                    return Array.Empty<T>();
 347                }
 348                else
 349                {
 0350                    T[] array = new T[collection.Count];
 0351                    collection.CopyTo(array, 0);
 0352                    return array;
 353                }
 354            }
 0355        }
 356
 357        internal ImmutableClientRuntime GetRuntime()
 358        {
 1359            lock (ThisLock)
 360            {
 1361                if (_runtime == null)
 362                {
 1363                    _runtime = new ImmutableClientRuntime(this);
 364                }
 365
 1366                return _runtime;
 367            }
 1368        }
 369
 370        internal void InvalidateRuntime()
 371        {
 32372            lock (ThisLock)
 373            {
 32374                _shared.ThrowIfImmutable();
 32375                _runtime = null;
 32376            }
 32377        }
 378
 379        internal void LockDownProperties()
 380        {
 0381            _shared.LockDownProperties();
 0382        }
 383
 384        internal SynchronizedCollection<T> NewBehaviorCollection<T>()
 385        {
 1014386            return new ProxyBehaviorCollection<T>(this);
 387        }
 388
 389        internal bool IsFault(ref Message reply)
 390        {
 1391            if (reply == null)
 392            {
 0393                return false;
 394            }
 1395            if (reply.IsFault)
 396            {
 0397                return true;
 398            }
 1399            if (MessageVersionNoneFaultsEnabled && IsMessageVersionNoneFault(ref reply, MaxFaultSize))
 400            {
 0401                return true;
 402            }
 403
 1404            return false;
 405        }
 406
 407        internal static bool IsMessageVersionNoneFault(ref Message message, int maxFaultSize)
 408        {
 0409            if (message.Version != MessageVersion.None || message.IsEmpty)
 410            {
 0411                return false;
 412            }
 413            //HttpResponseMessageProperty prop = message.Properties[HttpResponseMessageProperty.Name] as HttpResponseMes
 414            //if (prop == null || prop.StatusCode != HttpStatusCode.InternalServerError)
 415            //{
 416            //    return false;
 417            //}
 0418            using (MessageBuffer buffer = message.CreateBufferedCopy(maxFaultSize))
 419            {
 0420                message.Close();
 0421                message = buffer.CreateMessage();
 0422                using (Message copy = buffer.CreateMessage())
 423                {
 0424                    using (XmlDictionaryReader reader = copy.GetReaderAtBodyContents())
 425                    {
 0426                        return reader.IsStartElement(XD.MessageDictionary.Fault, MessageVersion.None.Envelope.Dictionary
 427                    }
 428                }
 429            }
 0430        }
 431
 432        private class ProxyBehaviorCollection<T> : SynchronizedCollection<T>
 433        {
 434            private readonly ClientRuntime _outer;
 435
 436            internal ProxyBehaviorCollection(ClientRuntime outer)
 2024437                : base(outer.ThisLock)
 438            {
 2024439                _outer = outer;
 2024440            }
 441
 442            protected override void ClearItems()
 443            {
 0444                _outer.InvalidateRuntime();
 0445                base.ClearItems();
 0446            }
 447
 448            protected override void InsertItem(int index, T item)
 449            {
 0450                if (item == null)
 451                {
 0452                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(item));
 453                }
 454
 0455                _outer.InvalidateRuntime();
 0456                base.InsertItem(index, item);
 0457            }
 458
 459            protected override void RemoveItem(int index)
 460            {
 0461                _outer.InvalidateRuntime();
 0462                base.RemoveItem(index);
 0463            }
 464
 465            protected override void SetItem(int index, T item)
 466            {
 0467                if (item == null)
 468                {
 0469                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(item));
 470                }
 471
 0472                _outer.InvalidateRuntime();
 0473                base.SetItem(index, item);
 0474            }
 475        }
 476
 477        private class OperationCollection : SynchronizedKeyedCollection<string, ClientOperation>
 478        {
 479            private readonly ClientRuntime _outer;
 480
 481            internal OperationCollection(ClientRuntime outer)
 505482                : base(outer.ThisLock)
 483            {
 505484                _outer = outer;
 505485            }
 486
 487            protected override void ClearItems()
 488            {
 0489                _outer.InvalidateRuntime();
 0490                base.ClearItems();
 0491            }
 492
 493            protected override string GetKeyForItem(ClientOperation item)
 494            {
 4495                return item.Name;
 496            }
 497
 498            protected override void InsertItem(int index, ClientOperation item)
 499            {
 2500                if (item == null)
 501                {
 0502                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(item));
 503                }
 504
 2505                if (item.Parent != _outer)
 506                {
 0507                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.SFxMismatchedOperationParent);
 508                }
 509
 2510                _outer.InvalidateRuntime();
 2511                base.InsertItem(index, item);
 2512            }
 513
 514            protected override void RemoveItem(int index)
 515            {
 0516                _outer.InvalidateRuntime();
 0517                base.RemoveItem(index);
 0518            }
 519
 520            protected override void SetItem(int index, ClientOperation item)
 521            {
 0522                if (item == null)
 523                {
 0524                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(item));
 525                }
 526
 0527                if (item.Parent != _outer)
 528                {
 0529                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.SFxMismatchedOperationParent);
 530                }
 531
 0532                _outer.InvalidateRuntime();
 0533                base.SetItem(index, item);
 0534            }
 535
 0536            internal void InternalClearItems() { ClearItems(); }
 0537            internal string InternalGetKeyForItem(ClientOperation item) { return GetKeyForItem(item); }
 0538            internal void InternalInsertItem(int index, ClientOperation item) { InsertItem(index, item); }
 0539            internal void InternalRemoveItem(int index) { RemoveItem(index); }
 0540            internal void InternalSetItem(int index, ClientOperation item) { SetItem(index, item); }
 541        }
 542
 543        private class OperationCollectionWrapper : KeyedCollection<string, ClientOperation>
 544        {
 545            private readonly OperationCollection _inner;
 0546            internal OperationCollectionWrapper(OperationCollection inner) { _inner = inner; }
 0547            protected override void ClearItems() { _inner.InternalClearItems(); }
 0548            protected override string GetKeyForItem(ClientOperation item) { return _inner.InternalGetKeyForItem(item); }
 0549            protected override void InsertItem(int index, ClientOperation item) { _inner.InternalInsertItem(index, item)
 0550            protected override void RemoveItem(int index) { _inner.InternalRemoveItem(index); }
 0551            protected override void SetItem(int index, ClientOperation item) { _inner.InternalSetItem(index, item); }
 552        }
 553    }
 554}

Methods/Properties

.ctor(System.String,System.String,CoreWCF.Dispatcher.SharedRuntimeState)
.ctor(CoreWCF.Dispatcher.DispatchRuntime,CoreWCF.Dispatcher.SharedRuntimeState)
.ctor(System.String,System.String)
CallbackClientType()
CallbackClientType(System.Type)
ChannelInitializers()
ContractName()
ContractNamespace()
ContractClientType()
ContractClientType(System.Type)
IdentityVerifier()
IdentityVerifier(CoreWCF.Security.IdentityVerifier)
Via()
Via(System.Uri)
ValidateMustUnderstand()
ValidateMustUnderstand(System.Boolean)
MessageVersionNoneFaultsEnabled()
MessageVersionNoneFaultsEnabled(System.Boolean)
DispatchRuntime()
CallbackDispatchRuntime()
EnableFaults()
EnableFaults(System.Boolean)
MaxFaultSize()
MaxFaultSize(System.Int32)
IsOnServer()
ManualAddressing()
ManualAddressing(System.Boolean)
MaxParameterInspectors()
ClientMessageInspectors()
MessageInspectors()
ClientOperations()
Operations()
OperationSelector()
OperationSelector(CoreWCF.Dispatcher.IClientOperationSelector)
ThisLock()
UnhandledClientOperation()
UseSynchronizationContext()
UseSynchronizationContext(System.Boolean)
GetArray(CoreWCF.Collections.Generic.SynchronizedCollection`1<T>)
GetRuntime()
InvalidateRuntime()
LockDownProperties()
NewBehaviorCollection()
IsFault(CoreWCF.Channels.Message&)
IsMessageVersionNoneFault(CoreWCF.Channels.Message&,System.Int32)
.ctor(CoreWCF.Dispatcher.ClientRuntime)
ClearItems()
InsertItem(System.Int32,T)
RemoveItem(System.Int32)
SetItem(System.Int32,T)
.ctor(CoreWCF.Dispatcher.ClientRuntime)
ClearItems()
GetKeyForItem(CoreWCF.Dispatcher.ClientOperation)
InsertItem(System.Int32,CoreWCF.Dispatcher.ClientOperation)
RemoveItem(System.Int32)
SetItem(System.Int32,CoreWCF.Dispatcher.ClientOperation)
InternalClearItems()
InternalGetKeyForItem(CoreWCF.Dispatcher.ClientOperation)
InternalInsertItem(System.Int32,CoreWCF.Dispatcher.ClientOperation)
InternalRemoveItem(System.Int32)
InternalSetItem(System.Int32,CoreWCF.Dispatcher.ClientOperation)
.ctor(CoreWCF.Dispatcher.ClientRuntime/OperationCollection)
ClearItems()
GetKeyForItem(CoreWCF.Dispatcher.ClientOperation)
InsertItem(System.Int32,CoreWCF.Dispatcher.ClientOperation)
RemoveItem(System.Int32)
SetItem(System.Int32,CoreWCF.Dispatcher.ClientOperation)