< Summary

Information
Class: Microsoft.Extensions.DependencyInjection.ValidatorHostedService
Assembly: SimpleOptions
File(s): /home/runner/work/SimpleOptions/SimpleOptions/src/SimpleOptions/ValidatorHostedService.cs
Tag: 28_8817130891
Line coverage
53%
Covered lines: 15
Uncovered lines: 13
Coverable lines: 28
Total lines: 48
Line coverage: 53.5%
Branch coverage
37%
Covered branches: 3
Total branches: 8
Branch coverage: 37.5%
Method coverage

Method coverage is only available for sponsors.

Upgrade to PRO version

Coverage History

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
StartAsync(...)37.5%845.83%
StopAsync(...)100%1100%

File(s)

/home/runner/work/SimpleOptions/SimpleOptions/src/SimpleOptions/ValidatorHostedService.cs

#LineLine coverage
 1using System.Runtime.ExceptionServices;
 2using Microsoft.Extensions.Hosting;
 3using Microsoft.Extensions.Options;
 4
 5namespace Microsoft.Extensions.DependencyInjection;
 6
 17internal class ValidatorHostedService(IEnumerable<SimpleOptionValid> validators) : IHostedService
 8{
 9    public Task StartAsync(CancellationToken cancellationToken)
 110    {
 111        List<Exception> exceptions = [];
 12
 713        foreach (var validator in validators)
 214        {
 15            try
 216            {
 217                validator();
 218            }
 019            catch (OptionsValidationException ex)
 020            {
 021                exceptions.Add(ex);
 022            }
 223        }
 24
 125        if (exceptions.Any())
 026        {
 027            if (exceptions.Count == 1)
 028            {
 29                // Rethrow if it's a single error
 030                ExceptionDispatchInfo.Capture(exceptions[0]).Throw();
 031            }
 32
 033            if (exceptions.Count > 1)
 034            {
 35                // Aggregate if we have many errors
 036                throw new AggregateException(exceptions);
 37            }
 038        }
 39
 140        return Task.CompletedTask;
 141    }
 42    public Task StopAsync(CancellationToken cancellationToken)
 143    {
 144        return Task.CompletedTask;
 145    }
 46
 47
 48}