< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.ServiceHostBase
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/ServiceHostBase.cs
Line coverage
58%
Covered lines: 101
Uncovered lines: 71
Coverable lines: 172
Total lines: 545
Line coverage: 58.7%
Branch coverage
50%
Covered branches: 46
Total branches: 92
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%110%
.ctor()100%11100%
AddBaseAddress(...)50%2260%
MakeAbsoluteUri(...)100%11100%
MakeAbsoluteUri(...)66.66%6675%
ApplyConfiguration()50%2275%
EnsureAuthenticationAuthorizationDebug(...)100%11100%
BindInstance(...)100%11100%
System.IDisposable.Dispose()100%11100%
InitializeRuntime()100%110%
EnsureAuthorization(...)50%2275%
EnsureDebug(...)100%22100%
GetBaseAddressSchemes(...)0%440%
GetVia(...)75%8890.9%
GetVia(...)100%88100%
GetUri(...)21.42%141430%
GetUri(...)78.57%141490.9%
InitializeDescription(...)100%22100%
OnAddChannelDispatcher(...)100%11100%
OnRemoveChannelDispatcher(...)100%110%
OnChannelDispatcherFaulted(...)100%110%
UnbindInstance(...)100%11100%
.ctor(...)100%110%
ResolveContract(...)0%440%
.ctor(...)100%110%
ResolveContract(...)0%440%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/ServiceHostBase.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.Globalization;
 8using System.Text;
 9using CoreWCF.Channels;
 10using CoreWCF.Description;
 11using CoreWCF.Dispatcher;
 12using CoreWCF.Runtime;
 13
 14namespace CoreWCF
 15{
 16    public abstract class ServiceHostBase : CommunicationObject, IExtensibleObject<ServiceHostBase>, IDisposable
 17    {
 018        internal static readonly Uri s_emptyUri = new Uri(string.Empty, UriKind.RelativeOrAbsolute);
 19        private bool _initializeDescriptionHasFinished;
 59120        private TimeSpan _closeTimeout = ServiceDefaults.ServiceHostCloseTimeout;
 21        private readonly ExtensionCollection<ServiceHostBase> _extensions;
 22        private ReadOnlyCollection<Uri> _externalBaseAddresses;
 23        private IDictionary<string, ContractDescription> _implementedContracts;
 24        private readonly IInstanceContextManager _instances;
 59125        private TimeSpan _openTimeout = ServiceDefaults.OpenTimeout;
 26
 27        //ServiceAuthenticationBehavior readOnlyAuthentication;
 28        //EventTraceActivity eventTraceActivity;
 29
 30#pragma warning disable CS0067 // The event is never used - see issue #288
 31        public event EventHandler<UnknownMessageReceivedEventArgs> UnknownMessageReceived;
 32#pragma warning restore CS0067 // The event is never used
 33
 59134        protected ServiceHostBase()
 35        {
 59136            InternalBaseAddresses = new UriSchemeKeyedCollection(ThisLock);
 59137            ChannelDispatchers = new ChannelDispatcherCollection(this, ThisLock);
 59138            _extensions = new ExtensionCollection<ServiceHostBase>(this, ThisLock);
 59139            _instances = new InstanceContextManager(ThisLock);
 59140        }
 41
 42        public ServiceAuthorizationBehavior Authorization
 43        {
 44            get
 45            {
 946                if (Description == null)
 47                {
 048                    return null;
 49                }
 950                else if (State == CommunicationState.Created || State == CommunicationState.Opening)
 51                {
 952                    return EnsureAuthorization(Description);
 53                }
 54                else
 55                {
 056                    return null;
 57                }
 58            }
 59        }
 60
 61        // TODO: Bring in ServiceAuthenticationBehavior
 62        //public ServiceAuthenticationBehavior Authentication
 63        //{
 64        //    get
 65        //    {
 66        //        if (this.Description == null)
 67        //        {
 68        //            return null;
 69        //        }
 70        //        else if (this.State == CommunicationState.Created || this.State == CommunicationState.Opening)
 71        //        {
 72        //            return EnsureAuthentication(this.Description);
 73        //        }
 74        //        else
 75        //        {
 76        //            return this.readOnlyAuthentication;
 77        //        }
 78        //    }
 79        //}
 80
 81        public ReadOnlyCollection<Uri> BaseAddresses
 82        {
 83            get
 84            {
 117685                _externalBaseAddresses = new ReadOnlyCollection<Uri>(new List<Uri>(InternalBaseAddresses));
 117686                return _externalBaseAddresses;
 87            }
 88        }
 89
 763990        public ChannelDispatcherCollection ChannelDispatchers { get; }
 91
 92        public TimeSpan CloseTimeout
 93        {
 285194            get { return _closeTimeout; }
 95            set
 96            {
 097                if (value < TimeSpan.Zero)
 98                {
 099                    string message = SRCommon.SFxTimeoutOutOfRange0;
 0100                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 101                }
 0102                if (TimeoutHelper.IsTooLarge(value))
 103                {
 0104                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 105                }
 106
 0107                lock (ThisLock)
 108                {
 0109                    ThrowIfClosedOrOpened();
 0110                    _closeTimeout = value;
 0111                }
 0112            }
 113        }
 114
 115        public ServiceCredentials Credentials
 116        {
 117            get
 118            {
 17119                if (Description == null)
 120                {
 0121                    return null;
 122                }
 17123                else if (State == CommunicationState.Created || State == CommunicationState.Opening)
 124                {
 17125                    return Description.EnsureCredentials();
 126                }
 127                else
 128                {
 0129                    return null;
 130                }
 131            }
 132        }
 133
 134        protected override TimeSpan DefaultCloseTimeout
 135        {
 519136            get { return CloseTimeout; }
 137        }
 138
 139        protected override TimeSpan DefaultOpenTimeout
 140        {
 2141            get { return OpenTimeout; }
 142        }
 143
 3901144        public ServiceDescription Description { get; private set; }
 145
 146        public IExtensionCollection<ServiceHostBase> Extensions
 147        {
 1206148            get { return _extensions; }
 149        }
 150
 151        protected internal IDictionary<string, ContractDescription> ImplementedContracts
 152        {
 1152153            get { return _implementedContracts; }
 154        }
 155
 3009156        internal UriSchemeKeyedCollection InternalBaseAddresses { get; }
 157
 158        public int ManualFlowControlLimit
 159        {
 0160            get { return 0; /*return this.ServiceThrottle.ManualFlowControlLimit;*/ }
 0161            set { /*this.ServiceThrottle.ManualFlowControlLimit = value;*/ }
 162        }
 163
 164        public TimeSpan OpenTimeout
 165        {
 74166            get { return _openTimeout; }
 167            set
 168            {
 0169                if (value < TimeSpan.Zero)
 170                {
 0171                    string message = SRCommon.SFxTimeoutOutOfRange0;
 0172                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 173                }
 0174                if (TimeoutHelper.IsTooLarge(value))
 175                {
 0176                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 177                }
 178
 0179                lock (ThisLock)
 180                {
 0181                    ThrowIfClosedOrOpened();
 0182                    _openTimeout = value;
 0183                }
 0184            }
 185        }
 186
 187        internal virtual object DisposableInstance
 188        {
 189            get
 190            {
 0191                return null;
 192            }
 193        }
 194
 195        protected void AddBaseAddress(Uri baseAddress)
 196        {
 471197            if (_initializeDescriptionHasFinished)
 198            {
 0199                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(
 0200                    SR.SFxCannotCallAddBaseAddress));
 201            }
 202
 471203            InternalBaseAddresses.Add(baseAddress);
 471204        }
 205
 206        internal Uri MakeAbsoluteUri(Uri relativeOrAbsoluteUri, Binding binding)
 207        {
 641208            return MakeAbsoluteUri(relativeOrAbsoluteUri, binding, InternalBaseAddresses);
 209        }
 210
 211        internal static Uri MakeAbsoluteUri(Uri relativeOrAbsoluteUri, Binding binding, UriSchemeKeyedCollection baseAdd
 212        {
 641213            Uri result = relativeOrAbsoluteUri;
 641214            if (!result.IsAbsoluteUri)
 215            {
 527216                if (binding.Scheme == string.Empty)
 217                {
 0218                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SFxCustom
 219                }
 527220                result = GetVia(binding.Scheme, result, baseAddresses);
 527221                if (result == null)
 222                {
 0223                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 224                }
 225            }
 226
 641227            return result;
 228        }
 229
 230        protected virtual void ApplyConfiguration()
 231        {
 580232            if (Description == null)
 233            {
 0234                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.SFxServiceHos
 235            }
 236
 580237            EnsureAuthenticationAuthorizationDebug(Description);
 580238        }
 239
 240        internal void EnsureAuthenticationAuthorizationDebug(ServiceDescription description)
 241        {
 242            //EnsureAuthentication(description);
 580243            EnsureAuthorization(description);
 580244            EnsureDebug(description);
 580245        }
 246
 247        internal virtual void BindInstance(InstanceContext instance)
 248        {
 2463249            _instances.Add(instance);
 250            //if (null != this.servicePerformanceCounters)
 251            //{
 252            //    lock (this.ThisLock)
 253            //    {
 254            //        if (null != this.servicePerformanceCounters)
 255            //        {
 256            //            this.servicePerformanceCounters.ServiceInstanceCreated();
 257            //        }
 258            //    }
 259            //}
 2463260        }
 261
 262        void IDisposable.Dispose()
 263        {
 518264            CloseAsync().GetAwaiter().GetResult();
 518265        }
 266
 267        protected abstract ServiceDescription CreateDescription(out IDictionary<string, ContractDescription> implemented
 268
 269        protected virtual void InitializeRuntime()
 270        {
 0271            throw new PlatformNotSupportedException();
 272        }
 273
 274        private ServiceAuthorizationBehavior EnsureAuthorization(ServiceDescription description)
 275        {
 276            Fx.Assert(State == CommunicationState.Created || State == CommunicationState.Opening, "");
 589277            ServiceAuthorizationBehavior behavior = description.Behaviors.Find<ServiceAuthorizationBehavior>();
 278
 589279            if (behavior == null)
 280            {
 0281                behavior = description.ServiceProvider.GetService(typeof(ServiceAuthorizationBehavior)) as ServiceAuthor
 282            }
 283
 589284            return behavior;
 285        }
 286
 287        //ServiceAuthenticationBehavior EnsureAuthentication(ServiceDescription description)
 288        //{
 289        //    Fx.Assert(this.State == CommunicationState.Created || this.State == CommunicationState.Opening, "");
 290        //    ServiceAuthenticationBehavior a = description.Behaviors.Find<ServiceAuthenticationBehavior>();
 291
 292        //    if (a == null)
 293        //    {
 294        //        a = new ServiceAuthenticationBehavior();
 295        //        description.Behaviors.Add(a);
 296        //    }
 297        //    return a;
 298        //}
 299
 300        private ServiceDebugBehavior EnsureDebug(ServiceDescription description)
 301        {
 302            Fx.Assert(State == CommunicationState.Created || State == CommunicationState.Opening, "");
 580303            ServiceDebugBehavior m = description.Behaviors.Find<ServiceDebugBehavior>();
 304
 580305            if (m == null)
 306            {
 580307                m = new ServiceDebugBehavior();
 580308                description.Behaviors.Add(m);
 309            }
 310
 580311            return m;
 312        }
 313
 314        internal static string GetBaseAddressSchemes(UriSchemeKeyedCollection uriSchemeKeyedCollection)
 315        {
 0316            StringBuilder buffer = new StringBuilder();
 0317            bool firstScheme = true;
 0318            foreach (Uri address in uriSchemeKeyedCollection)
 319            {
 0320                if (firstScheme)
 321                {
 0322                    buffer.Append(address.Scheme);
 0323                    firstScheme = false;
 324                }
 325                else
 326                {
 0327                    buffer.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator).Append(address.Scheme);
 328                }
 329            }
 330
 0331            return buffer.ToString();
 332        }
 333
 334        internal static Uri GetVia(string scheme, Uri address, UriSchemeKeyedCollection baseAddresses)
 335        {
 527336            Uri via = address;
 527337            if (!via.IsAbsoluteUri)
 338            {
 527339                Uri baseAddress = null;
 1665340                foreach(var ba in baseAddresses)
 341                {
 569342                    if (ba.Scheme.Equals(scheme))
 343                    {
 527344                        baseAddress = ba;
 527345                        break;
 346                    }
 347                }
 527348                if (baseAddress == null)
 349                {
 0350                    return null;
 351                }
 352
 527353                via = GetUri(baseAddress, address);
 354            }
 527355            return via;
 356        }
 357
 358        internal Uri GetVia(string scheme, Uri address)
 359        {
 1202360            if (!address.IsAbsoluteUri)
 361            {
 1176362                Uri baseAddress = null;
 4095363                foreach (var ba in BaseAddresses)
 364                {
 1112365                    if (ba.Scheme.Equals(scheme))
 366                    {
 481367                        baseAddress = ba;
 481368                        break;
 369                    }
 370                }
 371
 1176372                if (baseAddress == null)
 373                {
 695374                    return null;
 375                }
 376
 481377                return GetUri(baseAddress, address.OriginalString);
 378            }
 379
 26380            return address;
 381        }
 382
 383        private static Uri GetUri(Uri baseUri, string path)
 384        {
 481385            if (path.StartsWith("/", StringComparison.Ordinal) || path.StartsWith("\\", StringComparison.Ordinal))
 386            {
 0387                int i = 1;
 0388                for (; i < path.Length; ++i)
 389                {
 0390                    if (path[i] != '/' && path[i] != '\\')
 391                    {
 392                        break;
 393                    }
 394                }
 0395                path = path.Substring(i);
 396            }
 397
 481398            if (path.Length == 0)
 481399                return baseUri;
 400
 0401            if (!baseUri.AbsoluteUri.EndsWith("/", StringComparison.Ordinal))
 402            {
 0403                baseUri = new Uri(baseUri.AbsoluteUri + "/");
 404            }
 0405            return new Uri(baseUri, path);
 406        }
 407
 408        internal static Uri GetUri(Uri baseUri, Uri relativeUri)
 409        {
 527410            string path = relativeUri.OriginalString;
 527411            if (path.StartsWith("/", StringComparison.Ordinal) || path.StartsWith("\\", StringComparison.Ordinal))
 412            {
 477413                int i = 1;
 477414                for (; i < path.Length; ++i)
 415                {
 477416                    if (path[i] != '/' && path[i] != '\\')
 417                    {
 418                        break;
 419                    }
 420                }
 477421                path = path.Substring(i);
 422            }
 423
 424            // new Uri(Uri, string.Empty) is broken
 527425            if (path.Length == 0)
 426            {
 1427                return baseUri;
 428            }
 429
 526430            if (!baseUri.AbsoluteUri.EndsWith("/", StringComparison.Ordinal))
 431            {
 13432                baseUri = new Uri(baseUri.AbsoluteUri + "/");
 433            }
 434
 526435            return new Uri(baseUri, path);
 436        }
 437
 438        //public int IncrementManualFlowControlLimit(int incrementBy)
 439        //{
 440        //    throw new PlatformNotSupportedException();
 441        //}
 442
 443        protected void InitializeDescription(UriSchemeKeyedCollection baseAddresses)
 444        {
 1354445            foreach (Uri baseAddress in baseAddresses)
 446            {
 90447                InternalBaseAddresses.Add(baseAddress);
 448            }
 449
 587450            Description = CreateDescription(out _implementedContracts);
 580451            ApplyConfiguration();
 580452            _initializeDescriptionHasFinished = true;
 580453        }
 454
 455        // Configuration
 456        //protected void LoadConfigurationSection(ServiceElement serviceSection)
 457        //{
 458        //    throw new PlatformNotSupportedException();
 459        //}
 460
 461        internal void OnAddChannelDispatcher(ChannelDispatcherBase channelDispatcher)
 462        {
 640463            lock (ThisLock)
 464            {
 640465                ThrowIfClosedOrOpened();
 640466                channelDispatcher.AttachInternal(this);
 640467                channelDispatcher.Faulted += new EventHandler(OnChannelDispatcherFaulted);
 640468            }
 640469        }
 470
 471        internal void OnRemoveChannelDispatcher(ChannelDispatcherBase channelDispatcher)
 472        {
 0473            lock (ThisLock)
 474            {
 0475                ThrowIfClosedOrOpened();
 0476                channelDispatcher.DetachInternal(this);
 0477            }
 0478        }
 479
 480        private void OnChannelDispatcherFaulted(object sender, EventArgs e)
 481        {
 0482            Fault();
 0483        }
 484
 485        //protected void ReleasePerformanceCounters()
 486        //{
 487        //    throw new PlatformNotSupportedException();
 488        //}
 489
 490        internal virtual void UnbindInstance(InstanceContext instance)
 491        {
 2390492            _instances.Remove(instance);
 493            //if (null != this.servicePerformanceCounters)
 494            //{
 495            //    lock (this.ThisLock)
 496            //    {
 497            //        if (null != this.servicePerformanceCounters)
 498            //        {
 499            //            this.servicePerformanceCounters.ServiceInstanceRemoved();
 500            //        }
 501            //    }
 502            //}
 2390503        }
 504
 505        private class ImplementedContractsContractResolver : IContractResolver
 506        {
 507            private readonly IDictionary<string, ContractDescription> _implementedContracts;
 508
 0509            public ImplementedContractsContractResolver(IDictionary<string, ContractDescription> implementedContracts)
 510            {
 0511                _implementedContracts = implementedContracts;
 0512            }
 513
 514            public ContractDescription ResolveContract(string contractName)
 515            {
 0516                return _implementedContracts != null && _implementedContracts.ContainsKey(contractName) ? _implementedCo
 517            }
 518        }
 519
 520        internal class ServiceAndBehaviorsContractResolver : IContractResolver
 521        {
 522            private readonly IContractResolver _serviceResolver;
 523
 0524            public Dictionary<string, ContractDescription> BehaviorContracts { get; }
 525
 0526            public ServiceAndBehaviorsContractResolver(IContractResolver serviceResolver)
 527            {
 0528                _serviceResolver = serviceResolver;
 0529                BehaviorContracts = new Dictionary<string, ContractDescription>();
 0530            }
 531
 532            public ContractDescription ResolveContract(string contractName)
 533            {
 0534                ContractDescription contract = _serviceResolver.ResolveContract(contractName);
 535
 0536                if (contract == null)
 537                {
 0538                    contract = BehaviorContracts.ContainsKey(contractName) ? BehaviorContracts[contractName] : null;
 539                }
 540
 0541                return contract;
 542            }
 543        }
 544    }
 545}

Methods/Properties

.cctor()
.ctor()
Authorization()
BaseAddresses()
ChannelDispatchers()
CloseTimeout()
CloseTimeout(System.TimeSpan)
Credentials()
DefaultCloseTimeout()
DefaultOpenTimeout()
Description()
Extensions()
ImplementedContracts()
InternalBaseAddresses()
ManualFlowControlLimit()
ManualFlowControlLimit(System.Int32)
OpenTimeout()
OpenTimeout(System.TimeSpan)
DisposableInstance()
AddBaseAddress(System.Uri)
MakeAbsoluteUri(System.Uri,CoreWCF.Channels.Binding)
MakeAbsoluteUri(System.Uri,CoreWCF.Channels.Binding,CoreWCF.UriSchemeKeyedCollection)
ApplyConfiguration()
EnsureAuthenticationAuthorizationDebug(CoreWCF.Description.ServiceDescription)
BindInstance(CoreWCF.InstanceContext)
System.IDisposable.Dispose()
InitializeRuntime()
EnsureAuthorization(CoreWCF.Description.ServiceDescription)
EnsureDebug(CoreWCF.Description.ServiceDescription)
GetBaseAddressSchemes(CoreWCF.UriSchemeKeyedCollection)
GetVia(System.String,System.Uri,CoreWCF.UriSchemeKeyedCollection)
GetVia(System.String,System.Uri)
GetUri(System.Uri,System.String)
GetUri(System.Uri,System.Uri)
InitializeDescription(CoreWCF.UriSchemeKeyedCollection)
OnAddChannelDispatcher(CoreWCF.Dispatcher.ChannelDispatcherBase)
OnRemoveChannelDispatcher(CoreWCF.Dispatcher.ChannelDispatcherBase)
OnChannelDispatcherFaulted(System.Object,System.EventArgs)
UnbindInstance(CoreWCF.InstanceContext)
.ctor(System.Collections.Generic.IDictionary`2<System.String,CoreWCF.Description.ContractDescription>)
ResolveContract(System.String)
BehaviorContracts()
.ctor(CoreWCF.Description.IContractResolver)
ResolveContract(System.String)