< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Channels.UnixDomainSocketHostedService
Assembly: CoreWCF.UnixDomainSocket
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.UnixDomainSocket/src/CoreWCF/Channels/UnixDomainSocketHostedService.cs
Line coverage
70%
Covered lines: 17
Uncovered lines: 7
Coverable lines: 24
Total lines: 81
Line coverage: 70.8%
Branch coverage
25%
Covered branches: 1
Total branches: 4
Branch coverage: 25%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
StartAsync()100%11100%
StopAsync(...)50%22100%
UpdateServerAddressesFeature()0%220%
Dispose()100%110%
DisposeAsync()100%11100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.UnixDomainSocket/src/CoreWCF/Channels/UnixDomainSocketHostedService.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 CoreWCF.Channels;
 5using CoreWCF.Configuration;
 6using Microsoft.AspNetCore.Hosting.Server;
 7using Microsoft.AspNetCore.Hosting.Server.Features;
 8using Microsoft.AspNetCore.Server.Kestrel.Core;
 9using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets;
 10using Microsoft.Extensions.DependencyInjection;
 11using Microsoft.Extensions.Hosting;
 12using Microsoft.Extensions.Logging;
 13using Microsoft.Extensions.Options;
 14using System;
 15using System.Diagnostics;
 16using System.Linq;
 17using System.Runtime.InteropServices;
 18using System.Threading;
 19using System.Threading.Tasks;
 20
 21namespace CoreWCF.Channels
 22{
 23    internal class UnixDomainSocketHostedService : IHostedService, IAsyncDisposable, IDisposable
 24    {
 25        private readonly IServiceBuilder _serviceBuilder;
 26        private readonly ILogger<UnixDomainSocketHostedService> _logger;
 27        private readonly IServiceProvider _serviceProvider;
 28        private KestrelServer _kestrel;
 29        private CancellationTokenRegistration _applicationStartedRegistration;
 30        private bool _started;
 31
 332        public UnixDomainSocketHostedService(IServiceBuilder serviceBuilder, ILogger<UnixDomainSocketHostedService> logg
 33        {
 334            _serviceBuilder = serviceBuilder;
 335            _logger = logger;
 336            _serviceProvider = serviceProvider;
 337            IApplicationLifetime appLifetime = _serviceProvider.GetRequiredService<IApplicationLifetime>();
 338        }
 39
 40        public async Task StartAsync(CancellationToken cancellationToken)
 41        {
 342            _started = true;
 343            var transportFactory = _serviceProvider.GetRequiredService<SocketTransportFactory>();
 644            var unixDomainSocketFramingOptionsSetup = _serviceProvider.GetServices<IConfigureOptions<KestrelServerOption
 345            unixDomainSocketFramingOptionsSetup.AttachUDS = true;
 346            _kestrel = ActivatorUtilities.CreateInstance(_serviceProvider, typeof(KestrelServer), transportFactory) as K
 47            Debug.Assert(unixDomainSocketFramingOptionsSetup.AttachUDS);
 48
 349            await _kestrel.StartAsync<int>(null, cancellationToken);
 350        }
 51
 52        public Task StopAsync(CancellationToken cancellationToken)
 53        {
 354            if (!_started) return Task.CompletedTask;
 355            _started = false;
 356            return _kestrel.StopAsync(cancellationToken);
 57        }
 58
 59        private void UpdateServerAddressesFeature()
 60        {
 61            // This method needs to be called from appLifetime.ApplicationStarted otherwise an IServer might try to list
 62            // the address that Kestrel is listening on. It's not needed if Kestrel is the IServer implementation for th
 063            var kestrelServerAddresses = _kestrel.Features.Get<IServerAddressesFeature>();
 064            var serverAddressesFeature = _serviceProvider.GetRequiredService<IServer>().Features.Get<IServerAddressesFea
 065            foreach (var address in kestrelServerAddresses.Addresses)
 66            {
 067                serverAddressesFeature.Addresses.Add(address);
 68            }
 069        }
 70
 71        public void Dispose()
 72        {
 073            StopAsync(default).GetAwaiter().GetResult();
 074        }
 75
 76        public ValueTask DisposeAsync()
 77        {
 378            return new ValueTask(StopAsync(default));
 79        }
 80    }
 81}