< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Dispatcher.ChannelDispatcher
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/ChannelDispatcher.cs
Line coverage
55%
Covered lines: 101
Uncovered lines: 80
Coverable lines: 181
Total lines: 526
Line coverage: 55.8%
Branch coverage
34%
Covered branches: 15
Total branches: 44
Branch coverage: 34%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
.ctor(...)100%11100%
Initialize(...)100%11100%
HandleError(...)100%110%
HandleError(...)50%4462.5%
InitializeChannel(...)25%4444.44%
Init()100%22100%
NewBehaviorCollection()100%11100%
OnAddEndpoint(...)50%2283.33%
OnRemoveEndpoint(...)0%220%
OnAbort()100%110%
OnCloseAsync(...)100%110%
OnOpenAsync(...)100%11100%
ProvideFault(...)50%2271.42%
Attach(...)50%4466.66%
Detach(...)0%440%
.ctor(...)100%11100%
ClearItems()0%220%
InsertItem(...)50%2280%
RemoveItem(...)100%110%
SetItem(...)100%110%
.ctor(...)100%11100%
ClearItems()100%110%
InsertItem(...)0%220%
RemoveItem(...)100%110%
SetItem(...)0%220%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Dispatcher/ChannelDispatcher.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.Threading;
 8using System.Threading.Tasks;
 9using CoreWCF.Channels;
 10using CoreWCF.Collections.Generic;
 11using CoreWCF.Runtime;
 12
 13namespace CoreWCF.Dispatcher
 14{
 15    // This class is now only used as an OM for configuring the service.
 16    // This class has been kept to enable using existing behaviors.
 17    public class ChannelDispatcher : ChannelDispatcherBase
 18    {
 19        private EndpointDispatcherCollection _endpointDispatchers;
 20        private ServiceHostBase _host;
 21
 22        //bool isTransactedReceive;
 23        //bool asynchronousTransactedAcceptEnabled;
 24        //int maxTransactedBatchSize;
 25        private MessageVersion _messageVersion;
 26        private bool _receiveSynchronously;
 27        private bool _sendAsynchronously;
 28        private int _maxPendingReceives;
 29        private bool _includeExceptionDetailInFaults;
 30
 31        //ServiceThrottle serviceThrottle;
 32        // TODO: _session is needed when implementing clean shutdown codepath. See issue #282
 33        // private readonly bool _session;
 34        private SharedRuntimeState _shared;
 35        private readonly IDefaultCommunicationTimeouts _timeouts;
 36
 37        //IsolationLevel transactionIsolationLevel = ServiceBehaviorAttribute.DefaultIsolationLevel;
 38        //bool transactionIsolationLevelSet;
 39        //private TimeSpan _transactionTimeout;
 40        // TODO: Implement shutdown service cleanly, see Issue #282
 41        //private readonly bool _performDefaultCloseInput;
 42
 43        //EventTraceActivity eventTraceActivity;
 44        private ErrorBehavior _errorBehavior;
 45
 046        internal ChannelDispatcher(SharedRuntimeState shared)
 47        {
 048            Initialize(shared);
 049        }
 50
 66351        internal ChannelDispatcher(Uri listenUri, Binding binding, string bindingName, IDefaultCommunicationTimeouts tim
 52        {
 66353            BindingName = bindingName;
 66354            Binding = binding;
 66355            ListenUri = listenUri;
 66356            SupportedChannelTypes = supportedChannelTypes;
 66357            _timeouts = new ImmutableCommunicationTimeouts(timeouts);
 66358            Initialize(new SharedRuntimeState(true));
 66359        }
 60
 61        private void Initialize(SharedRuntimeState shared)
 62        {
 66363            _shared = shared;
 66364            _endpointDispatchers = new EndpointDispatcherCollection(this);
 66365            ChannelInitializers = NewBehaviorCollection<IChannelInitializer>();
 66366            Channels = new CommunicationObjectManager<IChannel>(ThisLock);
 66367            PendingChannels = new SynchronizedChannelCollection<IChannel>(ThisLock);
 66368            ErrorHandlers = new Collection<IErrorHandler>();
 69            //this.isTransactedReceive = false;
 70            //this.asynchronousTransactedAcceptEnabled = false;
 66371            _receiveSynchronously = false;
 66372            _sendAsynchronously = true;
 73            //this.serviceThrottle = null;
 74            //transactionTimeout = TimeSpan.Zero;
 66375            _maxPendingReceives = 1;
 66376        }
 77
 078        public string BindingName { get; }
 79
 80        // TODO: As the channel concept is changing, does it make sense to support IChannelInitializer's?
 117981        public SynchronizedCollection<IChannelInitializer> ChannelInitializers { get; private set; }
 82
 83        protected override TimeSpan DefaultCloseTimeout
 84        {
 85            get
 86            {
 087                if (_timeouts != null)
 88                {
 089                    return _timeouts.CloseTimeout;
 90                }
 91                else
 92                {
 093                    return ServiceDefaults.CloseTimeout;
 94                }
 95            }
 96        }
 97
 98        protected override TimeSpan DefaultOpenTimeout
 99        {
 100            get
 101            {
 1176102                if (_timeouts != null)
 103                {
 1176104                    return _timeouts.OpenTimeout;
 105                }
 106                else
 107                {
 0108                    return ServiceDefaults.OpenTimeout;
 109                }
 110            }
 111        }
 112
 3842113        internal EndpointDispatcherTable EndpointDispatcherTable { get; private set; }
 114
 3122115        internal CommunicationObjectManager<IChannel> Channels { get; private set; }
 116
 117        public SynchronizedCollection<EndpointDispatcher> Endpoints
 118        {
 5776119            get { return _endpointDispatchers; }
 120        }
 121
 3348122        public Collection<IErrorHandler> ErrorHandlers { get; private set; }
 123
 124        public MessageVersion MessageVersion
 125        {
 1324126            get { return _messageVersion; }
 127            set
 128            {
 664129                _messageVersion = value;
 664130                ThrowIfDisposedOrImmutable();
 664131            }
 132        }
 133
 134        public override ServiceHostBase Host
 135        {
 2387136            get { return _host; }
 137        }
 138
 139        internal bool EnableFaults
 140        {
 680141            get { return _shared.EnableFaults; }
 142            set
 143            {
 641144                ThrowIfDisposedOrImmutable();
 641145                _shared.EnableFaults = value;
 641146            }
 147        }
 148
 149        //public ServiceThrottle ServiceThrottle
 150        //{
 151        //    get
 152        //    {
 153        //        return this.serviceThrottle;
 154        //    }
 155        //    set
 156        //    {
 157        //        this.ThrowIfDisposedOrImmutable();
 158        //        this.serviceThrottle = value;
 159        //    }
 160        //}
 161
 162        public bool ManualAddressing
 163        {
 2980164            get { return _shared.ManualAddressing; }
 165            set
 166            {
 664167                ThrowIfDisposedOrImmutable();
 664168                _shared.ManualAddressing = value;
 664169            }
 170        }
 171
 663172        internal SynchronizedChannelCollection<IChannel> PendingChannels { get; private set; }
 173
 174        public bool ReceiveSynchronously
 175        {
 176            get
 177            {
 0178                return _receiveSynchronously;
 179            }
 180            set
 181            {
 641182                ThrowIfDisposedOrImmutable();
 641183                if (value != false)
 184                {
 0185                    throw new ArgumentException("Only false supported", nameof(ReceiveSynchronously));
 186                }
 187
 641188                _receiveSynchronously = value;
 641189            }
 190        }
 191
 192        public bool SendAsynchronously
 193        {
 194            get
 195            {
 0196                return _sendAsynchronously;
 197            }
 198            set
 199            {
 0200                ThrowIfDisposedOrImmutable();
 0201                if (value != true)
 202                {
 0203                    throw new ArgumentException("Only true supported", nameof(ReceiveSynchronously));
 204                }
 205
 0206                _sendAsynchronously = value;
 0207            }
 208        }
 209
 210        // TODO: Do we need to worry about this?
 211        public int MaxPendingReceives
 212        {
 213            get
 214            {
 0215                return _maxPendingReceives;
 216            }
 217            set
 218            {
 0219                ThrowIfDisposedOrImmutable();
 0220                _maxPendingReceives = value;
 0221            }
 222        }
 223
 224        public bool IncludeExceptionDetailInFaults
 225        {
 1361226            get { return _includeExceptionDetailInFaults; }
 227            set
 228            {
 660229                lock (ThisLock)
 230                {
 660231                    ThrowIfDisposedOrImmutable();
 660232                    _includeExceptionDetailInFaults = value;
 660233                }
 660234            }
 235        }
 236
 9988237        public Uri ListenUri { get; }
 238
 459239        internal List<Type> SupportedChannelTypes { get; }
 240
 8599241        internal Binding Binding { get; }
 242
 243        internal bool HandleError(Exception error)
 244        {
 0245            ErrorHandlerFaultInfo dummy = new ErrorHandlerFaultInfo();
 0246            return HandleError(error, ref dummy);
 247        }
 248
 249        internal bool HandleError(Exception error, ref ErrorHandlerFaultInfo faultInfo)
 250        {
 251            ErrorBehavior behavior;
 252
 4253            lock (ThisLock)
 254            {
 4255                if (_errorBehavior != null)
 256                {
 4257                    behavior = _errorBehavior;
 258                }
 259                else
 260                {
 0261                    behavior = new ErrorBehavior(this);
 262                }
 0263            }
 264
 4265            if (behavior != null)
 266            {
 4267                return behavior.HandleError(error, ref faultInfo);
 268            }
 269            else
 270            {
 0271                return false;
 272            }
 273        }
 274
 275        internal void InitializeChannel(IClientChannel channel)
 276        {
 516277            ThrowIfDisposedOrNotOpen();
 278            try
 279            {
 1032280                for (int i = 0; i < ChannelInitializers.Count; ++i)
 281                {
 0282                    ChannelInitializers[i].Initialize(channel);
 283                }
 516284            }
 0285            catch (Exception e)
 286            {
 0287                if (Fx.IsFatal(e))
 288                {
 0289                    throw;
 290                }
 0291                throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e);
 292            }
 516293        }
 294
 295        internal void Init()
 296        {
 663297            _errorBehavior = new ErrorBehavior(this);
 298
 663299            EndpointDispatcherTable = new EndpointDispatcherTable(ThisLock);
 2648300            for (int i = 0; i < _endpointDispatchers.Count; i++)
 301            {
 664302                EndpointDispatcher endpoint = _endpointDispatchers[i];
 303
 304                // Force a build of the runtime to catch any unexpected errors before we are done opening.
 664305                endpoint.DispatchRuntime.GetRuntime();
 306                // Lock down the DispatchRuntime.
 661307                endpoint.DispatchRuntime.LockDownProperties();
 308
 661309                EndpointDispatcherTable.AddEndpoint(endpoint);
 310
 311                //if (DiagnosticUtility.ShouldTraceInformation)
 312                //{
 313                //    this.TraceEndpointLifetime(endpoint, TraceCode.EndpointListenerOpen, SR.Format(SR.TraceCodeEndpoin
 314                //}
 315            }
 660316        }
 317
 318        internal SynchronizedCollection<T> NewBehaviorCollection<T>()
 319        {
 663320            return new ChannelDispatcherBehaviorCollection<T>(this);
 321        }
 322
 323        internal bool HasApplicationEndpoints
 324        {
 325            get
 326            {
 1920327                foreach (EndpointDispatcher endpointDispatcher in Endpoints)
 328                {
 640329                    if (!endpointDispatcher.IsSystemEndpoint)
 330                    {
 640331                        return true;
 332                    }
 333                }
 0334                return false;
 640335            }
 336        }
 337
 338        private void OnAddEndpoint(EndpointDispatcher endpoint)
 339        {
 664340            lock (ThisLock)
 341            {
 664342                endpoint.Attach(this);
 343
 664344                if (State == CommunicationState.Opened)
 345                {
 0346                    EndpointDispatcherTable.AddEndpoint(endpoint);
 347                }
 664348            }
 664349        }
 350
 351        private void OnRemoveEndpoint(EndpointDispatcher endpoint)
 352        {
 0353            lock (ThisLock)
 354            {
 0355                if (State == CommunicationState.Opened)
 356                {
 0357                    EndpointDispatcherTable.RemoveEndpoint(endpoint);
 358                }
 359
 0360                endpoint.Detach(this);
 0361            }
 0362        }
 363
 364        protected override void OnAbort()
 365        {
 0366            throw new PlatformNotSupportedException();
 367        }
 368
 369        protected override Task OnCloseAsync(CancellationToken token)
 370        {
 0371            throw new PlatformNotSupportedException();
 372        }
 373
 374        protected override Task OnOpenAsync(CancellationToken token)
 375        {
 660376            return Task.CompletedTask;
 377        }
 378
 379        // TODO: Move this functionality somewhere else as this class is now OM only
 380        internal void ProvideFault(Exception e, FaultConverter faultConverter, ref ErrorHandlerFaultInfo faultInfo)
 381        {
 382            ErrorBehavior behavior;
 383
 19384            lock (ThisLock)
 385            {
 19386                if (_errorBehavior != null)
 387                {
 19388                    behavior = _errorBehavior;
 389                }
 390                else
 391                {
 0392                    behavior = new ErrorBehavior(this);
 393                }
 0394            }
 395
 19396            behavior.ProvideFault(e, faultConverter, ref faultInfo);
 19397        }
 398
 399        protected override void Attach(ServiceHostBase host)
 400        {
 640401            if (host == null)
 402            {
 0403                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(host));
 404            }
 405
 640406            ServiceHostBase serviceHost = host;
 407
 640408            ThrowIfDisposedOrImmutable();
 409
 640410            if (_host != null)
 411            {
 0412                Exception error = new InvalidOperationException(SR.SFxChannelDispatcherMultipleHost0);
 0413                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error);
 414            }
 415
 640416            _host = serviceHost;
 640417        }
 418
 419        protected override void Detach(ServiceHostBase host)
 420        {
 0421            if (host == null)
 422            {
 0423                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(host));
 424            }
 425
 0426            if (_host != host)
 427            {
 0428                Exception error = new InvalidOperationException(SR.SFxChannelDispatcherDifferentHost0);
 0429                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error);
 430            }
 431
 0432            ThrowIfDisposedOrImmutable();
 433
 0434            _host = null;
 0435        }
 436
 437        private class EndpointDispatcherCollection : SynchronizedCollection<EndpointDispatcher>
 438        {
 439            private readonly ChannelDispatcher _owner;
 440
 441            internal EndpointDispatcherCollection(ChannelDispatcher owner)
 663442                : base(owner.ThisLock)
 443            {
 663444                _owner = owner;
 663445            }
 446
 447            protected override void ClearItems()
 448            {
 0449                foreach (EndpointDispatcher item in Items)
 450                {
 0451                    _owner.OnRemoveEndpoint(item);
 452                }
 0453                base.ClearItems();
 0454            }
 455
 456            protected override void InsertItem(int index, EndpointDispatcher item)
 457            {
 664458                if (item == null)
 459                {
 0460                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(item));
 461                }
 462
 664463                _owner.OnAddEndpoint(item);
 664464                base.InsertItem(index, item);
 664465            }
 466
 467            protected override void RemoveItem(int index)
 468            {
 0469                EndpointDispatcher item = Items[index];
 0470                base.RemoveItem(index);
 0471                _owner.OnRemoveEndpoint(item);
 0472            }
 473
 474            protected override void SetItem(int index, EndpointDispatcher item)
 475            {
 0476                Exception error = new InvalidOperationException(SR.SFxCollectionDoesNotSupportSet0);
 0477                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error);
 478            }
 479        }
 480
 481        private class ChannelDispatcherBehaviorCollection<T> : SynchronizedCollection<T>
 482        {
 483            private readonly ChannelDispatcher _outer;
 484
 485            internal ChannelDispatcherBehaviorCollection(ChannelDispatcher outer)
 663486                : base(outer.ThisLock)
 487            {
 663488                _outer = outer;
 663489            }
 490
 491            protected override void ClearItems()
 492            {
 0493                _outer.ThrowIfDisposedOrImmutable();
 0494                base.ClearItems();
 0495            }
 496
 497            protected override void InsertItem(int index, T item)
 498            {
 0499                if (item == null)
 500                {
 0501                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(item));
 502                }
 503
 0504                _outer.ThrowIfDisposedOrImmutable();
 0505                base.InsertItem(index, item);
 0506            }
 507
 508            protected override void RemoveItem(int index)
 509            {
 0510                _outer.ThrowIfDisposedOrImmutable();
 0511                base.RemoveItem(index);
 0512            }
 513
 514            protected override void SetItem(int index, T item)
 515            {
 0516                if (item == null)
 517                {
 0518                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(item));
 519                }
 520
 0521                _outer.ThrowIfDisposedOrImmutable();
 0522                base.SetItem(index, item);
 0523            }
 524        }
 525    }
 526}

Methods/Properties

.ctor(CoreWCF.Dispatcher.SharedRuntimeState)
.ctor(System.Uri,CoreWCF.Channels.Binding,System.String,CoreWCF.IDefaultCommunicationTimeouts,System.Collections.Generic.List`1<System.Type>)
Initialize(CoreWCF.Dispatcher.SharedRuntimeState)
BindingName()
ChannelInitializers()
DefaultCloseTimeout()
DefaultOpenTimeout()
EndpointDispatcherTable()
Channels()
Endpoints()
ErrorHandlers()
MessageVersion()
MessageVersion(CoreWCF.Channels.MessageVersion)
Host()
EnableFaults()
EnableFaults(System.Boolean)
ManualAddressing()
ManualAddressing(System.Boolean)
PendingChannels()
ReceiveSynchronously()
ReceiveSynchronously(System.Boolean)
SendAsynchronously()
SendAsynchronously(System.Boolean)
MaxPendingReceives()
MaxPendingReceives(System.Int32)
IncludeExceptionDetailInFaults()
IncludeExceptionDetailInFaults(System.Boolean)
ListenUri()
SupportedChannelTypes()
Binding()
HandleError(System.Exception)
HandleError(System.Exception,CoreWCF.Dispatcher.ErrorHandlerFaultInfo&)
InitializeChannel(CoreWCF.IClientChannel)
Init()
NewBehaviorCollection()
HasApplicationEndpoints()
OnAddEndpoint(CoreWCF.Dispatcher.EndpointDispatcher)
OnRemoveEndpoint(CoreWCF.Dispatcher.EndpointDispatcher)
OnAbort()
OnCloseAsync(System.Threading.CancellationToken)
OnOpenAsync(System.Threading.CancellationToken)
ProvideFault(System.Exception,CoreWCF.Channels.FaultConverter,CoreWCF.Dispatcher.ErrorHandlerFaultInfo&)
Attach(CoreWCF.ServiceHostBase)
Detach(CoreWCF.ServiceHostBase)
.ctor(CoreWCF.Dispatcher.ChannelDispatcher)
ClearItems()
InsertItem(System.Int32,CoreWCF.Dispatcher.EndpointDispatcher)
RemoveItem(System.Int32)
SetItem(System.Int32,CoreWCF.Dispatcher.EndpointDispatcher)
.ctor(CoreWCF.Dispatcher.ChannelDispatcher)
ClearItems()
InsertItem(System.Int32,T)
RemoveItem(System.Int32)
SetItem(System.Int32,T)