< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Configuration.ConfigurationManagerServiceModelOptions
Assembly: CoreWCF.ConfigurationManager
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.ConfigurationManager/src/CoreWCF/Configuration/ConfigurationManagerServiceModelOptions.cs
Line coverage
95%
Covered lines: 66
Uncovered lines: 3
Coverable lines: 69
Total lines: 127
Line coverage: 95.6%
Branch coverage
61%
Covered branches: 21
Total branches: 34
Branch coverage: 61.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)50%101093.54%
Configure(...)100%11100%
Configure(...)100%22100%
ReadConfigSection(...)50%161690%
AddEndpoint(...)100%44100%
AddBinding(...)100%22100%
ParseConfig()100%11100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.ConfigurationManager/src/CoreWCF/Configuration/ConfigurationManagerServiceModelOptions.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;
 6using System.Configuration;
 7using System.IO;
 8using System.Linq;
 9using System.Reflection;
 10using System.Runtime.InteropServices;
 11using Microsoft.Extensions.DependencyInjection;
 12using Microsoft.Extensions.Options;
 13
 14namespace CoreWCF.Configuration
 15{
 16    internal class ConfigurationManagerServiceModelOptions : IConfigureNamedOptions<ServiceModelOptions>
 17    {
 18        private readonly Lazy<ServiceModelSectionGroup> _section;
 19
 20        private readonly IConfigurationHolder _holder;
 21
 4022        public ConfigurationManagerServiceModelOptions(IServiceProvider builder, string path)
 23        {
 4024            _holder = builder.GetRequiredService<IConfigurationHolder>();
 25
 4026            _section = new Lazy<ServiceModelSectionGroup>(() =>
 4027            {
 4028                var assembly = Assembly.GetEntryAssembly();
 4029                var basePath = string.IsNullOrEmpty(assembly?.Location) ? AppContext.BaseDirectory : Path.GetDirectoryNa
 4030
 4031                // hack - in .net core 2.1 on linux directory not correct(on unit tests),
 4032                // for example:
 4033                // /home/vsts/.nuget/packages/microsoft.testplatform.testhost/16.7.1/lib/netcoreapp2.1/
 4034                var isNetCore21 = RuntimeInformation.FrameworkDescription.Contains(".NET Core 4.6");
 4035                if (isNetCore21 && RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
 4036                {
 037                    basePath = AppContext.BaseDirectory;
 4038                }
 4039
 4040                var configMap = new ExeConfigurationFileMap(Path.Combine(basePath, "CoreWCF.machine.config"))
 4041                {
 4042                    ExeConfigFilename = path
 4043                };
 4044                System.Configuration.Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(confi
 4045                var section = ServiceModelSectionGroup.GetSectionGroup(configuration);
 4046
 4047                if (section is null)
 4048                {
 049                    throw new ServiceModelConfigurationException("Section not found");
 4050                }
 4051
 4052                return section;
 4053            }, true);
 4054        }
 55
 56        public void Configure(string name, ServiceModelOptions options)
 57        {
 258            Configure(options);
 259        }
 60
 61        public void Configure(ServiceModelOptions options)
 62        {
 4063            var configHolder = ParseConfig();
 10264            foreach (var serviceEndPoint in configHolder.Endpoints)
 65            {
 1366               IXmlConfigEndpoint configEndpoint = configHolder.GetXmlConfigEndpoint(serviceEndPoint);
 1367                options.ConfigureService(configEndpoint.Service, serviceConfig =>
 1368                {
 269                    serviceConfig.AddServiceEndpoint(configEndpoint.Contract, configEndpoint.Binding, configEndpoint.Add
 1570                });
 71            }
 3872        }
 73
 74        private void ReadConfigSection(ServiceModelSectionGroup group)
 75        {
 4076            if (group is null)
 77            {
 078                return;
 79            }
 80
 4081            AddBinding(group.Bindings?.BasicHttpBinding.Bindings);
 4082            AddBinding(group.Bindings?.NetTcpBinding.Bindings);
 4083            AddBinding(group.Bindings?.NetHttpBinding.Bindings);
 4084            AddBinding(group.Bindings?.wsHttpBinding.Bindings);
 4085            AddBinding(group.Bindings?.WebHttpBinding.Bindings);
 4086            AddBinding(group.Bindings?.CustomBinding.Bindings);
 3887            AddEndpoint(group.Services?.Services);
 3888        }
 89
 90        private void AddEndpoint(IEnumerable endpoints)
 91        {
 9692            foreach (ServiceElement bindingElement in endpoints.OfType<ServiceElement>())
 93            {
 1094                string serviceName = bindingElement.Name;
 95
 4696                foreach (ServiceEndpointElement endpoint in bindingElement.Endpoints.OfType<ServiceEndpointElement>())
 97                {
 1398                    _holder.AddServiceEndpoint(
 1399                        endpoint.Name,
 13100                        serviceName,
 13101                        endpoint.Address,
 13102                        endpoint.Contract,
 13103                        endpoint.Binding,
 13104                        endpoint.BindingConfiguration,
 13105                        endpoint.BindingNamespace);
 106                }
 107            }
 38108        }
 109
 110        private void AddBinding(IEnumerable bindings)
 111        {
 556112            foreach (IStandardBindingElement bindingElement in bindings.OfType<IStandardBindingElement>())
 113            {
 39114                Channels.Binding binding = bindingElement.CreateBinding();
 37115                _holder.AddBinding(binding);
 116            }
 238117        }
 118
 119        private IConfigurationHolder ParseConfig()
 120        {
 40121            ReadConfigSection(_section.Value);
 38122            return _holder;
 123        }
 124
 125
 126    }
 127}