1+ namespace ServiceControl . Hosting ;
2+
3+ using System ;
4+ using System . Runtime . Versioning ;
5+ using Microsoft . Extensions . Hosting ;
6+ using Microsoft . Extensions . Hosting . WindowsServices ;
7+ using Microsoft . Extensions . Logging ;
8+ using Microsoft . Extensions . Options ;
9+
10+ [ SupportedOSPlatform ( "windows" ) ]
11+ sealed class WindowsServiceWithRequestTimeout : WindowsServiceLifetime
12+ {
13+ static readonly TimeSpan CancellationDuration = TimeSpan . FromSeconds ( 5 ) ;
14+ readonly HostOptions hostOptions ;
15+
16+ // TODO: I don't think this constructor is needed and exist for backwards compability in the runtime
17+
18+ // public WindowsServiceWithRequestTimeout(IHostEnvironment environment, IHostApplicationLifetime applicationLifetime, ILoggerFactory loggerFactory, IOptions<HostOptions> optionsAccessor)
19+ // : this(environment, applicationLifetime, loggerFactory, optionsAccessor, Options.Create(new WindowsServiceLifetimeOptions()))
20+ // {
21+ // }
22+
23+ public WindowsServiceWithRequestTimeout ( IHostEnvironment environment , IHostApplicationLifetime applicationLifetime , ILoggerFactory loggerFactory , IOptions < HostOptions > optionsAccessor , IOptions < WindowsServiceLifetimeOptions > windowsServiceOptionsAccessor )
24+ : base ( environment , applicationLifetime , loggerFactory , optionsAccessor , windowsServiceOptionsAccessor )
25+ {
26+ hostOptions = optionsAccessor . Value ;
27+ }
28+
29+ protected override void OnStop ( )
30+ {
31+ RequestAdditionalTime ( hostOptions . ShutdownTimeout + CancellationDuration ) ;
32+ base . OnStop ( ) ;
33+ }
34+
35+ protected override void OnShutdown ( )
36+ {
37+ RequestAdditionalTime ( hostOptions . ShutdownTimeout + CancellationDuration ) ;
38+ base . OnShutdown ( ) ;
39+ }
40+ }
0 commit comments