< Summary

Information
Class: ProjectTemplate.Web.Services.ApplicationAuditCompletionOutboxHostedService
Assembly: ProjectTemplate.Web
File(s): /home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/Services/ApplicationAuditCompletionOutboxHostedService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 29
Coverable lines: 29
Total lines: 63
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)0%110100%
ExecuteAsync()0%2040%

File(s)

/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/Services/ApplicationAuditCompletionOutboxHostedService.cs

#LineLine coverage
 1using Microsoft.Extensions.Options;
 2using ProjectTemplate.Infrastructure.Data.Auditing;
 3
 4namespace ProjectTemplate.Web.Services;
 5
 6/// <summary>
 7/// Dispatches durable audit-completion entries after their originating transaction commits.
 8/// </summary>
 09public sealed partial class ApplicationAuditCompletionOutboxHostedService(
 010    IServiceScopeFactory scopeFactory,
 011    IOptions<ApplicationAuditCompletionOutboxOptions> options,
 012    TimeProvider timeProvider,
 013    ILogger<ApplicationAuditCompletionOutboxHostedService> logger)
 14    : BackgroundService
 15{
 016    private readonly IServiceScopeFactory _scopeFactory =
 017        scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory));
 018    private readonly ApplicationAuditCompletionOutboxOptions _options =
 019        options?.Value ?? throw new ArgumentNullException(nameof(options));
 020    private readonly TimeProvider _timeProvider =
 021        timeProvider ?? throw new ArgumentNullException(nameof(timeProvider));
 022    private readonly ILogger<ApplicationAuditCompletionOutboxHostedService> _logger =
 023        logger ?? throw new ArgumentNullException(nameof(logger));
 24
 25    [LoggerMessage(
 26        EventId = 19100,
 27        Level = LogLevel.Error,
 28        Message = "The audit-completion outbox dispatch cycle failed and will be retried.")]
 29    private static partial void LogDispatchCycleFailure(
 30        ILogger logger,
 31        Exception exception);
 32
 33    /// <inheritdoc />
 34    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
 35    {
 036        if (!_options.Enabled)
 37        {
 038            return;
 39        }
 40
 041        while (!stoppingToken.IsCancellationRequested)
 42        {
 43            try
 44            {
 045                using IServiceScope scope = _scopeFactory.CreateScope();
 046                IApplicationAuditCompletionOutboxDispatcher dispatcher = scope.ServiceProvider
 047                    .GetRequiredService<IApplicationAuditCompletionOutboxDispatcher>();
 048                _ = await dispatcher.DispatchReadyAsync(stoppingToken).ConfigureAwait(false);
 049            }
 050            catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
 51            {
 052                break;
 53            }
 054            catch (Exception exception)
 55            {
 056                LogDispatchCycleFailure(_logger, exception);
 057            }
 58
 059            await Task.Delay(_options.PollInterval, _timeProvider, stoppingToken)
 060                .ConfigureAwait(false);
 61        }
 062    }
 63}