-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathWindowsServiceWithRequestTimeout.cs
More file actions
40 lines (33 loc) · 1.5 KB
/
WindowsServiceWithRequestTimeout.cs
File metadata and controls
40 lines (33 loc) · 1.5 KB
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
namespace ServiceControl.Hosting;
using System;
using System.Runtime.Versioning;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.WindowsServices;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
[SupportedOSPlatform("windows")]
sealed class WindowsServiceWithRequestTimeout : WindowsServiceLifetime
{
static readonly TimeSpan CancellationDuration = TimeSpan.FromSeconds(5);
readonly HostOptions hostOptions;
public WindowsServiceWithRequestTimeout(IHostEnvironment environment, IHostApplicationLifetime applicationLifetime, ILoggerFactory loggerFactory, IOptions<HostOptions> optionsAccessor, IOptions<WindowsServiceLifetimeOptions> windowsServiceOptionsAccessor)
: base(environment, applicationLifetime, loggerFactory, optionsAccessor, windowsServiceOptionsAccessor)
{
hostOptions = optionsAccessor.Value;
}
protected override void OnStop()
{
var logger = NLog.LogManager.GetCurrentClassLogger();
var additionalTime = hostOptions.ShutdownTimeout + CancellationDuration;
logger.Info("OnStop invoked, going to ask for additional time: {additionalTime}", additionalTime);
RequestAdditionalTime(additionalTime);
logger.Info("Additional time requested");
base.OnStop();
}
protected override void OnShutdown()
{
var logger = NLog.LogManager.GetCurrentClassLogger();
logger.Info("OnShutdown invoked, process may exit ungracefully");
base.OnShutdown();
}
}