Пример #1
0
        private void ProcessInSeparateScope(IServiceScope serviceScope, ReceivedMessage message)
        {
            var validationContext = serviceScope.ServiceProvider.GetRequiredService <IValidationContext>();

            validationContext.When(message, m => m.Elapsed)
            .Is(timeSpan => timeSpan.Seconds % 2 == 0)
            .AddValidationDetail($"Elapsed: {message.Elapsed.Seconds} seconds");

            if (validationContext.IsValid())
            {
                logger.LogInformation("Everything is ok");
            }
            else
            {
                var(validationKey, validationMessage) = validationContext.ValidationDetails.Single();

                logger.LogError($"Key: '{validationKey}', message: '{validationMessage}'");
            }
        }
Пример #2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var stopwatch = Stopwatch.StartNew();

            while (!stoppingToken.IsCancellationRequested)
            {
                using (var serviceScope = serviceProvider.CreateScope())
                {
                    var message = new ReceivedMessage
                    {
                        Elapsed = stopwatch.Elapsed
                    };

                    ProcessInSeparateScope(serviceScope, message);
                }

                await Task.Delay(1000, stoppingToken);
            }
        }