-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyServiceZ.cs
46 lines (36 loc) · 1.84 KB
/
MyServiceZ.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
41
42
43
44
45
46
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace DotNetCoreGenericHostSample
{
public class MyServiceZ : BackgroundService
{
public MyServiceZ(ILoggerFactory loggerFactory)
{
Logger = loggerFactory.CreateLogger<MyServiceZ>();
Logger.LogInformation("{0} : MyServiceZ constructed.", DateTime.Now.ToString());
}
public ILogger Logger { get; }
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
Logger.LogInformation("{0} : MyServiceZ background task is starting.", DateTime.Now.ToString());
stoppingToken.Register(() => Logger.LogInformation("{0} : MyServiceZ is stopping.", DateTime.Now.ToString()));
// Add a delay before first run to allow time for any database to first be created etc.
//await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken);
// Perform any required preparation here...
while (!stoppingToken.IsCancellationRequested)
{
Logger.LogInformation("{0} : MyServiceZ is doing background work.", DateTime.Now.ToString());
//await Task.Delay(TimeSpan.FromSeconds(5.2), stoppingToken).ContinueWith(task => { });
await Task.Delay(TimeSpan.FromSeconds(5.2));
}
Logger.LogInformation("{0} : MyServiceZ background task is stopping.", DateTime.Now.ToString());
// Perform any required clean-up here...
// Close database connections etc...
await Task.Delay(TimeSpan.FromMilliseconds(1000)); // exaggerated to 1 second
Logger.LogInformation("{0} : MyServiceZ background task clean-up completed.", DateTime.Now.ToString());
}
}
}