< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Configuration.ServiceBuilder
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Configuration/ServiceBuilder.cs
Line coverage
74%
Covered lines: 64
Uncovered lines: 22
Coverable lines: 86
Total lines: 250
Line coverage: 74.4%
Branch coverage
50%
Covered branches: 9
Total branches: 18
Branch coverage: 50%
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/Configuration/ServiceBuilder.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.Threading;
 7using System.Threading.Tasks;
 8using CoreWCF.Channels;
 9using CoreWCF.Description;
 10using Microsoft.Extensions.Hosting;
 11using Microsoft.Extensions.DependencyInjection;
 12
 13namespace CoreWCF.Configuration
 14{
 15    internal class ServiceBuilder : CommunicationObject, IServiceBuilder
 16    {
 57317        private readonly IDictionary<Type, IServiceConfiguration> _services = new Dictionary<Type, IServiceConfiguration
 57318        private readonly TaskCompletionSource<object> _openingCompletedTcs = new TaskCompletionSource<object>(TaskCreati
 19
 57320        public ServiceBuilder(IServiceProvider serviceProvider, IApplicationLifetime appLifetime)
 21        {
 57322            ServiceProvider = serviceProvider;
 57323            appLifetime.ApplicationStarted.Register(() =>
 57324            {
 50525                if (State == CommunicationState.Created)
 57326                {
 57327                    // Using discard as any exceptions are swallowed from the ApplicationStarted
 57328                    // callback and no place to await the OpenAsync call. Should only hit this code
 57329                    // when hosting in IIS.
 330                    _ = OpenAsync();
 57331                }
 107832            });
 57333            Opened += OpenedCallback;
 57334        }
 35
 51536        public ICollection<IServiceConfiguration> ServiceConfigurations => _services.Values;
 37
 183138        public ICollection<Uri> BaseAddresses { get; } = new List<Uri>();
 39
 54340        ICollection<Type> IServiceBuilder.Services => _services.Keys;
 41
 68042        public IServiceProvider ServiceProvider { get; }
 43
 044        protected override TimeSpan DefaultCloseTimeout => TimeSpan.FromMinutes(1);
 45
 5446        protected override TimeSpan DefaultOpenTimeout => TimeSpan.FromMinutes(1);
 47
 48        public IServiceBuilder AddService<TService>() where TService : class
 49        {
 54850            return AddService(typeof(TService));
 51        }
 52
 53        public IServiceBuilder AddService<TService>(Action<ServiceOptions> options) where TService : class
 54        {
 3555            return AddService(typeof(TService), options);
 56        }
 57
 58        public IServiceBuilder AddService(Type service)
 59        {
 55160            if (service is null)
 61            {
 062                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(service)));
 63            }
 55164            var serviceConfig = (IServiceConfiguration)ServiceProvider.GetRequiredService(
 55165                typeof(IServiceConfiguration<>).MakeGenericType(service));
 55166            _services[serviceConfig.ServiceType] = serviceConfig;
 55167            return this;
 68        }
 69
 70        public IServiceBuilder AddService(Type service, Action<ServiceOptions> options)
 71        {
 3572            if (service is null)
 73            {
 074                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(service)));
 75            }
 3576            var serviceConfig = (IServiceConfiguration)ServiceProvider.GetRequiredService(
 3577                typeof(IServiceConfiguration<>).MakeGenericType(service));
 3578            _services[serviceConfig.ServiceType] = serviceConfig;
 3579            ServiceConfigurationDelegateHolder holder = (ServiceConfigurationDelegateHolder)ServiceProvider
 3580                .GetRequiredService(typeof(ServiceConfigurationDelegateHolder<>).MakeGenericType(service));
 3581            holder.AddServiceOptionsDelegate(options);
 3582            return this;
 83        }
 84
 85
 86        public IServiceBuilder AddServiceEndpoint<TService, TContract>(Binding binding, string address)
 87        {
 55588            return AddServiceEndpoint<TService>(typeof(TContract), binding, address);
 89        }
 90
 91        public IServiceBuilder AddServiceEndpoint<TService, TContract>(Binding binding, string address, Action<ServiceEn
 92        {
 093            return AddServiceEndpoint<TService>(typeof(TContract), binding, address, configureEndpoint);
 94        }
 95
 96        public IServiceBuilder AddServiceEndpoint<TService, TContract>(Binding binding, Uri address)
 97        {
 5698            return AddServiceEndpoint<TService>(typeof(TContract), binding, address);
 99        }
 100
 101        public IServiceBuilder AddServiceEndpoint<TService, TContract>(Binding binding, Uri address, Action<ServiceEndpo
 102        {
 0103            return AddServiceEndpoint<TService>(typeof(TContract), binding, address, configureEndpoint);
 104        }
 105
 106        public IServiceBuilder AddServiceEndpoint<TService, TContract>(Binding binding, string address, Uri listenUri)
 107        {
 0108            return AddServiceEndpoint<TService>(typeof(TContract), binding, address, listenUri);
 109        }
 110
 111        public IServiceBuilder AddServiceEndpoint<TService, TContract>(Binding binding, string address, Uri listenUri, A
 112        {
 0113            return AddServiceEndpoint<TService>(typeof(TContract), binding, address, listenUri, configureEndpoint);
 114        }
 115
 116        public IServiceBuilder AddServiceEndpoint<TService, TContract>(Binding binding, Uri address, Uri listenUri)
 117        {
 0118            return AddServiceEndpoint<TService>(typeof(TContract), binding, address, listenUri);
 119        }
 120
 121        public IServiceBuilder AddServiceEndpoint<TService, TContract>(Binding binding, Uri address, Uri listenUri, Acti
 122        {
 0123            return AddServiceEndpoint<TService>(typeof(TContract), binding, address, listenUri, configureEndpoint);
 124        }
 125
 126        public IServiceBuilder AddServiceEndpoint<TService>(Type implementedContract, Binding binding, string address)
 127        {
 557128            return AddServiceEndpoint<TService>(implementedContract, binding, address, (Uri)null);
 129        }
 130
 131        public IServiceBuilder AddServiceEndpoint<TService>(Type implementedContract, Binding binding, string address, A
 132        {
 0133            return AddServiceEndpoint<TService>(implementedContract, binding, address, (Uri)null, configureEndpoint);
 134        }
 135
 136        public IServiceBuilder AddServiceEndpoint<TService>(Type implementedContract, Binding binding, Uri address)
 137        {
 57138            return AddServiceEndpoint<TService>(implementedContract, binding, address, (Uri)null);
 139        }
 140
 141        public IServiceBuilder AddServiceEndpoint<TService>(Type implementedContract, Binding binding, Uri address, Acti
 142        {
 0143            return AddServiceEndpoint<TService>(implementedContract, binding, address, (Uri)null, configureEndpoint);
 144        }
 145
 146        public IServiceBuilder AddServiceEndpoint<TService>(Type implementedContract, Binding binding, string address, U
 147        {
 557148            if (address is null)
 149            {
 0150                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(address)));
 151            }
 152
 557153            return AddServiceEndpoint<TService>(implementedContract, binding, new Uri(address, UriKind.RelativeOrAbsolut
 154        }
 155
 156        public IServiceBuilder AddServiceEndpoint<TService>(Type implementedContract, Binding binding, string address, U
 157        {
 0158            if (address is null)
 159            {
 0160                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(address)));
 161            }
 162
 0163            return AddServiceEndpoint<TService>(implementedContract, binding, new Uri(address, UriKind.RelativeOrAbsolut
 164        }
 165
 166        public IServiceBuilder AddServiceEndpoint<TService>(Type implementedContract, Binding binding, Uri address, Uri 
 167        {
 614168            return AddServiceEndpoint(typeof(TService), implementedContract, binding, address, listenUri, null);
 169        }
 170
 171        public IServiceBuilder AddServiceEndpoint<TService>(Type implementedContract, Binding binding, Uri address, Uri 
 172        {
 0173            return AddServiceEndpoint(typeof(TService), implementedContract, binding, address, listenUri, configureEndpo
 174        }
 175
 176        public IServiceBuilder AddServiceEndpoint(Type service, Type implementedContract, Binding binding, Uri address, 
 177        {
 2178            return AddServiceEndpoint(service, implementedContract, binding, address, listenUri, null);
 179        }
 180
 181        public IServiceBuilder AddServiceEndpoint(Type service, Type implementedContract, Binding binding, Uri address, 
 182        {
 653183            if (service is null)
 184            {
 0185                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(service)));
 186            }
 187
 653188            if (implementedContract is null)
 189            {
 0190                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(implementedCo
 191            }
 192
 653193            if (binding is null)
 194            {
 0195                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(binding)));
 196            }
 197
 653198            if (_services.TryGetValue(service, out IServiceConfiguration serviceConfig))
 199            {
 653200                serviceConfig.Endpoints.Add(new ServiceEndpointConfiguration()
 653201                {
 653202                    Address = address,
 653203                    Binding = binding,
 653204                    Contract = implementedContract,
 653205                    ListenUri = listenUri,
 653206                    ConfigureEndpoint = configureEndpoint
 653207                });
 208            }
 209            else
 210            {
 0211                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError((new ArgumentException(SR.Format(SR.UnknownSer
 212            }
 213
 653214            return this;
 215        }
 216
 217        protected override void OnAbort()
 218        {
 0219        }
 220
 221        protected override Task OnCloseAsync(CancellationToken token)
 222        {
 0223            return Task.CompletedTask;
 224        }
 225
 571226        protected override Task OnOpenAsync(CancellationToken token) => Task.CompletedTask;
 227
 228        private void OpenedCallback(object sender, EventArgs e)
 229        {
 230            // Using a callback to complete the TCS means the state will have transitioned to Opened before completing t
 231            // which means if another thread is waiting on this TCS, ServiceBuilder is in the expected state when it con
 232            // This callback needs to run before any other event handlers as they can (and the HTTP middleware indirectl
 233            // the TCS being completed before they run.
 571234            _openingCompletedTcs.TrySetResult(null);
 571235        }
 236
 237        protected override void OnFaulted()
 238        {
 10239            base.OnFaulted();
 10240            _openingCompletedTcs.TrySetResult(null);
 10241        }
 242
 243        // This is to allow the ServiceHostObjectModel to wait until all the Opening event handlers have ran
 244        // to do some configuration such as adding base addresses before the actual service dispatcher is created.
 245        internal Task WaitForOpening()
 246        {
 583247            return _openingCompletedTcs.Task;
 248        }
 249    }
 250}

Methods/Properties

.ctor(System.IServiceProvider,Microsoft.Extensions.Hosting.IApplicationLifetime)
ServiceConfigurations()
BaseAddresses()
WCF.Configuration.IServiceBuilder.get_Services()
ServiceProvider()
DefaultCloseTimeout()
DefaultOpenTimeout()
AddService()
AddService(System.Action`1<CoreWCF.Configuration.ServiceOptions>)
AddService(System.Type)
AddService(System.Type,System.Action`1<CoreWCF.Configuration.ServiceOptions>)
AddServiceEndpoint(CoreWCF.Channels.Binding,System.String)
AddServiceEndpoint(CoreWCF.Channels.Binding,System.String,System.Action`1<CoreWCF.Description.ServiceEndpoint>)
AddServiceEndpoint(CoreWCF.Channels.Binding,System.Uri)
AddServiceEndpoint(CoreWCF.Channels.Binding,System.Uri,System.Action`1<CoreWCF.Description.ServiceEndpoint>)
AddServiceEndpoint(CoreWCF.Channels.Binding,System.String,System.Uri)
AddServiceEndpoint(CoreWCF.Channels.Binding,System.String,System.Uri,System.Action`1<CoreWCF.Description.ServiceEndpoint>)
AddServiceEndpoint(CoreWCF.Channels.Binding,System.Uri,System.Uri)
AddServiceEndpoint(CoreWCF.Channels.Binding,System.Uri,System.Uri,System.Action`1<CoreWCF.Description.ServiceEndpoint>)
AddServiceEndpoint(System.Type,CoreWCF.Channels.Binding,System.String)
AddServiceEndpoint(System.Type,CoreWCF.Channels.Binding,System.String,System.Action`1<CoreWCF.Description.ServiceEndpoint>)
AddServiceEndpoint(System.Type,CoreWCF.Channels.Binding,System.Uri)
AddServiceEndpoint(System.Type,CoreWCF.Channels.Binding,System.Uri,System.Action`1<CoreWCF.Description.ServiceEndpoint>)
AddServiceEndpoint(System.Type,CoreWCF.Channels.Binding,System.String,System.Uri)
AddServiceEndpoint(System.Type,CoreWCF.Channels.Binding,System.String,System.Uri,System.Action`1<CoreWCF.Description.ServiceEndpoint>)
AddServiceEndpoint(System.Type,CoreWCF.Channels.Binding,System.Uri,System.Uri)
AddServiceEndpoint(System.Type,CoreWCF.Channels.Binding,System.Uri,System.Uri,System.Action`1<CoreWCF.Description.ServiceEndpoint>)
AddServiceEndpoint(System.Type,System.Type,CoreWCF.Channels.Binding,System.Uri,System.Uri)
AddServiceEndpoint(System.Type,System.Type,CoreWCF.Channels.Binding,System.Uri,System.Uri,System.Action`1<CoreWCF.Description.ServiceEndpoint>)
OnAbort()
OnCloseAsync(System.Threading.CancellationToken)
OnOpenAsync(System.Threading.CancellationToken)
OpenedCallback(System.Object,System.EventArgs)
OnFaulted()
WaitForOpening()