< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.ServiceBehaviorAttribute
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/ServiceBehaviorAttribute.cs
Line coverage
82%
Covered lines: 90
Uncovered lines: 19
Coverable lines: 109
Total lines: 314
Line coverage: 82.5%
Branch coverage
78%
Covered branches: 55
Total branches: 70
Branch coverage: 78.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/ServiceBehaviorAttribute.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 System.ComponentModel;
 7using CoreWCF.Channels;
 8using CoreWCF.Description;
 9using CoreWCF.Dispatcher;
 10using CoreWCF.Runtime;
 11using Microsoft.Extensions.DependencyInjection;
 12
 13namespace CoreWCF
 14{
 15    [AttributeUsage(CoreWCFAttributeTargets.ServiceBehavior)]
 16    public sealed class ServiceBehaviorAttribute : Attribute, IServiceBehavior
 17    {
 18        private class ServiceProviderExtension : IExtension<InstanceContext>, IKeyedServiceProvider
 19        {
 20            private readonly IServiceProvider _serviceProvider;
 021            private IKeyedServiceProvider KeyedServiceProvider { get; }
 22
 7923            public ServiceProviderExtension(IServiceProvider serviceProvider)
 24            {
 7925                _serviceProvider = serviceProvider;
 7926                KeyedServiceProvider = _serviceProvider as IKeyedServiceProvider;
 7927            }
 28
 29            public void Attach(InstanceContext owner)
 30            {
 31                // intentionally left blank
 7932            }
 33
 34            public void Detach(InstanceContext owner)
 35            {
 36                // intentionally left blank
 037            }
 38
 339            public object GetService(Type serviceType) => _serviceProvider.GetService(serviceType);
 40
 041            public object GetKeyedService(Type serviceType, object serviceKey) => KeyedServiceProvider.GetKeyedService(s
 42
 43            public object GetRequiredKeyedService(Type serviceType, object serviceKey) =>
 044                KeyedServiceProvider.GetRequiredKeyedService(serviceType, serviceKey);
 45        }
 46
 47        private ConcurrencyMode _concurrencyMode;
 48        private bool _ensureOrderedDispatch = false;
 49        private string _configurationName;
 50        private InstanceContextMode _instanceMode;
 51        private object _wellKnownSingleton;  // if the user passes an object to the ServiceHost, it is stored here
 52        private object _hiddenSingleton;     // if the user passes a type to the ServiceHost, and instanceMode==Single, 
 115953        private readonly bool _validateMustUnderstand = true;
 54        private readonly bool _ignoreExtensionDataObject = DataContractSerializerDefaults.IgnoreExtensionDataObject;
 115955        private readonly int _maxItemsInObjectGraph = DataContractSerializerDefaults.MaxItemsInObjectGraph;
 115956        private readonly bool _automaticSessionShutdown = true;
 57        private IInstanceProvider _instanceProvider = null;
 58        private IServiceProvider _serviceProvider = null;
 115959        private bool _useSynchronizationContext = true;
 60        private AddressFilterMode _addressFilterMode = AddressFilterMode.Exact;
 61
 62        [DefaultValue(null)]
 58763        public string Name { get; set; }
 64
 65        [DefaultValue(null)]
 58766        public string Namespace { get; set; }
 67
 68        internal IInstanceProvider InstanceProvider
 69        {
 100670            set { _instanceProvider = value; }
 71        }
 72
 73        [DefaultValue(AddressFilterMode.Exact)]
 74        public AddressFilterMode AddressFilterMode
 75        {
 192176            get { return _addressFilterMode; }
 77            set
 78            {
 279                if (!AddressFilterModeHelper.IsDefined(value))
 80                {
 081                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 82                }
 83
 284                _addressFilterMode = value;
 285            }
 86        }
 87
 88        [DefaultValue(null)]
 89        public string ConfigurationName
 90        {
 58791            get { return _configurationName; }
 92            set
 93            {
 094                if (value == null)
 95                {
 096                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value));
 97                }
 98
 099                if (value == string.Empty)
 100                {
 0101                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 0102                        SR.SFxConfigurationNameCannotBeEmpty));
 103                }
 104
 0105                _configurationName = value;
 0106            }
 107        }
 108
 109        [DefaultValue(false)]
 1038110        public bool IncludeExceptionDetailInFaults { get; set; } = false;
 111
 112        [DefaultValue(ConcurrencyMode.Single)]
 113        public ConcurrencyMode ConcurrencyMode
 114        {
 0115            get { return _concurrencyMode; }
 116            set
 117            {
 266118                if (!ConcurrencyModeHelper.IsDefined(value))
 119                {
 0120                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 121                }
 122
 266123                _concurrencyMode = value;
 266124            }
 125        }
 126
 127        [DefaultValue(false)]
 128        public bool EnsureOrderedDispatch
 129        {
 3130            get { return _ensureOrderedDispatch; }
 14131            set { _ensureOrderedDispatch = value; }
 132        }
 133
 134        [DefaultValue(InstanceContextMode.PerSession)]
 135        public InstanceContextMode InstanceContextMode
 136        {
 1169137            get { return _instanceMode; }
 138            set
 139            {
 334140                if (!InstanceContextModeHelper.IsDefined(value))
 141                {
 0142                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 143                }
 144
 334145                _instanceMode = value;
 334146            }
 147        }
 148
 149        [DefaultValue(true)]
 150        public bool UseSynchronizationContext
 151        {
 1152            get => _useSynchronizationContext;
 16153            set => _useSynchronizationContext = value;
 154        }
 155
 156        internal IServiceProvider ServicePovider
 157        {
 79158            set => _serviceProvider = value;
 159        }
 160
 161        public object GetWellKnownSingleton()
 162        {
 582163            return _wellKnownSingleton;
 164        }
 165
 166        internal void SetWellKnownSingleton(object value)
 167        {
 7168            _wellKnownSingleton = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value
 7169        }
 170
 171        internal object GetHiddenSingleton()
 172        {
 575173            return _hiddenSingleton;
 174        }
 175
 176        internal void SetHiddenSingleton(object value)
 177        {
 72178            _hiddenSingleton = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value));
 72179        }
 180
 181        void IServiceBehavior.Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
 182        {
 581183            if (_concurrencyMode != ConcurrencyMode.Single && _ensureOrderedDispatch)
 184            {
 2185                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFx
 186            }
 579187        }
 188
 189        void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription,
 190            ServiceHostBase serviceHostBase,
 191            Collection<ServiceEndpoint> endpoints,
 192            BindingParameterCollection bindingParameters)
 193        {
 664194        }
 195
 196        void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
 197        {
 2432198            for (int i = 0; i < serviceHostBase.ChannelDispatchers.Count; i++)
 199            {
 640200                if (serviceHostBase.ChannelDispatchers[i] is ChannelDispatcher channelDispatcher)
 201                {
 640202                    channelDispatcher.IncludeExceptionDetailInFaults = IncludeExceptionDetailInFaults;
 203
 640204                    if (channelDispatcher.HasApplicationEndpoints)
 205                    {
 2562206                        foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints)
 207                        {
 641208                            if (endpointDispatcher.IsSystemEndpoint)
 209                            {
 210                                continue;
 211                            }
 641212                            DispatchRuntime behavior = endpointDispatcher.DispatchRuntime;
 641213                            behavior.ConcurrencyMode = _concurrencyMode;
 641214                            behavior.EnsureOrderedDispatch = _ensureOrderedDispatch;
 641215                            behavior.ValidateMustUnderstand = _validateMustUnderstand;
 641216                            behavior.AutomaticInputSessionShutdown = _automaticSessionShutdown;
 641217                            if (!_useSynchronizationContext)
 218                            {
 4219                                behavior.SynchronizationContext = null;
 220                            }
 221
 641222                            if (!endpointDispatcher.AddressFilterSetExplicit)
 223                            {
 641224                                EndpointAddress address = endpointDispatcher.OriginalAddress;
 641225                                if (address == null || AddressFilterMode == AddressFilterMode.Any)
 226                                {
 1227                                    endpointDispatcher.AddressFilter = new MatchAllMessageFilter();
 228                                }
 640229                                else if (AddressFilterMode == AddressFilterMode.Prefix)
 230                                {
 0231                                    endpointDispatcher.AddressFilter = new PrefixEndpointAddressMessageFilter(address);
 232                                }
 640233                                else if (AddressFilterMode == AddressFilterMode.Exact)
 234                                {
 640235                                    endpointDispatcher.AddressFilter = new EndpointAddressMessageFilter(address);
 236                                }
 237                            }
 238                        }
 239                    }
 240                }
 241            }
 576242            DataContractSerializerServiceBehavior.ApplySerializationSettings(description, _ignoreExtensionDataObject, _m
 576243            ApplyInstancing(description, serviceHostBase);
 576244        }
 245
 246        private void ApplyInstancing(ServiceDescription description, ServiceHostBase serviceHostBase)
 247        {
 576248            Type serviceType = description.ServiceType;
 576249            InstanceContext singleton = null;
 250
 2432251            for (int i = 0; i < serviceHostBase.ChannelDispatchers.Count; i++)
 252            {
 640253                if (serviceHostBase.ChannelDispatchers[i] is ChannelDispatcher channelDispatcher)
 254                {
 2562255                    foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints)
 256                    {
 641257                        if (endpointDispatcher.IsSystemEndpoint)
 258                        {
 259                            continue;
 260                        }
 641261                        DispatchRuntime dispatch = endpointDispatcher.DispatchRuntime;
 641262                        if (dispatch.InstanceProvider == null)
 263                        {
 641264                            if (_instanceProvider == null)
 265                            {
 102266                                if (serviceType == null && _wellKnownSingleton == null)
 267                                {
 0268                                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationExcept
 269                                }
 270
 102271                                if (_instanceMode != InstanceContextMode.Single && _wellKnownSingleton != null)
 272                                {
 0273                                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationExcept
 274                                }
 275                            }
 276                            else
 277                            {
 539278                                dispatch.InstanceProvider = _instanceProvider;
 279                            }
 280                        }
 641281                        dispatch.Type = serviceType;
 641282                        dispatch.InstanceContextProvider = InstanceContextProviderBase.GetProviderForMode(_instanceMode,
 283
 641284                        if ((_instanceMode == InstanceContextMode.Single) &&
 641285                            (dispatch.SingletonInstanceContext == null))
 286                        {
 102287                            if (singleton == null)
 288                            {
 79289                                if (_wellKnownSingleton != null)
 290                                {
 7291                                    singleton = new InstanceContext(serviceHostBase, _wellKnownSingleton, true, false);
 292                                }
 72293                                else if (_hiddenSingleton != null)
 294                                {
 72295                                    singleton = new InstanceContext(serviceHostBase, _hiddenSingleton, false, false);
 296                                }
 297                                else
 298                                {
 0299                                    singleton = new InstanceContext(serviceHostBase, false);
 300                                }
 301
 79302                                singleton.AutoClose = false;
 79303                                singleton.IsSingleton = true;
 304                                Fx.Assert(_serviceProvider != null, $"{nameof(_serviceProvider)} is null.");
 79305                                singleton.Extensions.Add(new ServiceProviderExtension(_serviceProvider));
 306                            }
 102307                            dispatch.SingletonInstanceContext = singleton;
 308                        }
 309                    }
 310                }
 311            }
 576312        }
 313    }
 314}