-
Notifications
You must be signed in to change notification settings - Fork 66
/
SettingValidationHostedService.cs
40 lines (35 loc) · 1.22 KB
/
SettingValidationHostedService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#if NETSTANDARD2_1
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
namespace NetEscapades.Configuration.Validation
{
/// <summary>
/// An <see cref="IHostedService"/> that validates <see cref="IValidatable"/> objects are valid on app startup
/// </summary>
public class SettingValidationHostedService : IHostedService
{
readonly IEnumerable<IValidatable> _validatableObjects;
/// <summary>
/// Create a new instance of <see cref="SettingValidationHostedService"/>
/// </summary>
/// <param name="validatableObjects"></param>
public SettingValidationHostedService(IEnumerable<IValidatable> validatableObjects)
{
_validatableObjects = validatableObjects;
}
/// <inheritdoc />
public Task StartAsync(CancellationToken cancellationToken)
{
foreach (var validatableObject in _validatableObjects)
{
validatableObject.Validate();
}
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
}
#endif