Skip to content

Commit 7df9ce6

Browse files
committed
Simplify DI
1 parent 6814f7e commit 7df9ce6

3 files changed

Lines changed: 19 additions & 24 deletions

File tree

docs/index.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,24 @@ public class MyCoolBackgroundService : WindowsServiceBackgroundService
8282
{
8383
public MyCoolBackgroundService(
8484
ILogger<MyFaultyWindowsServiceBackgroundService> logger,
85-
IHostLifetime hostLifetime,
86-
IHostApplicationLifetime applicationLifetime
85+
IHostLifetime hostLifetime
8786
)
88-
: base(logger, hostLifetime, applicationLifetime)
87+
: base(logger, hostLifetime)
8988
{
9089
}
9190

9291
// This still runs your long-running background job
93-
protected override Task TryExecuteAsync(CancellationToken stoppingToken)
92+
protected override async Task TryExecuteAsync(CancellationToken stoppingToken)
9493
{
9594
// Do your continuous or periodic background work.
9695
await SomeLongRunningTaskAsync();
9796

9897
// We're done, let the service stop.
9998
ServiceLifetime.ExitCode = 0;
100-
ApplicationLifetime.StopApplication();
99+
100+
// This kills the process about immediately, you can also inject `IHostApplication`
101+
// and call StopAsync() on that.
102+
await ServiceLifetime.StopAsync();
101103
}
102104

103105
// This one tells you when we're shutting down or resuming from semi-hibernation

src/CodeCaster.WindowsServiceExtensions/Service/WindowsServiceBackgroundService.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,18 @@ public abstract class WindowsServiceBackgroundService : BackgroundService, IWind
2424
/// </summary>
2525
protected readonly ILogger<IHostedService> Logger;
2626

27+
#pragma warning disable CS0419, CS1574 // Ambiguous reference in cref attribute
2728
/// <summary>
28-
/// To interact with the Service Control Manager.
29+
/// To interact with the Service Control Manager. <c>null</c> when <see cref="WindowsServiceLifetimeHostBuilderExtensionsAdapter.UseWindowsServiceExtensions"/> wasn't called.
2930
/// </summary>
31+
#pragma warning restore CS0419, CS1574 // Ambiguous reference in cref attribute
3032
protected readonly ExtendedWindowsServiceLifetime? ServiceLifetime;
3133

32-
/// <summary>
33-
/// To ask nicely to stop the host when cancellation is requested. We're just a BackgroundService, returning from ExecuteAsync() won't stop the host application. Better us than Windows.
34-
/// </summary>
35-
protected readonly IHostApplicationLifetime ApplicationLifetime;
36-
3734
/// <inheritdoc />
38-
protected WindowsServiceBackgroundService(
39-
ILogger<IHostedService> logger,
40-
IHostLifetime hostLifetime,
41-
IHostApplicationLifetime applicationLifetime
42-
)
35+
protected WindowsServiceBackgroundService(ILogger<IHostedService> logger, IHostLifetime hostLifetime)
4336
{
4437
Logger = logger;
4538
ServiceLifetime = hostLifetime as ExtendedWindowsServiceLifetime;
46-
ApplicationLifetime = applicationLifetime;
4739
}
4840

4941
/// <summary>
@@ -75,7 +67,7 @@ protected sealed override async Task ExecuteAsync(CancellationToken stoppingToke
7567
{
7668
Logger.LogDebug("Setting process exit code to {exitCode}", ErrorInvalidData);
7769

78-
// The host will shut down with code 0 if we don't do this.
70+
// The .NET host will shut down with code 0 if we don't do this.
7971
Environment.ExitCode = ErrorInvalidData;
8072

8173
#pragma warning disable CA1416 // Validate platform compatibility - we are a Windows Service.

test/TestServiceThatThrows/Program.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,20 @@ public class MyFaultyWindowsServiceBackgroundService : WindowsServiceBackgroundS
120120
{
121121
public MyFaultyWindowsServiceBackgroundService(
122122
ILogger<MyFaultyWindowsServiceBackgroundService> logger,
123-
IHostLifetime hostLifetime,
124-
IHostApplicationLifetime applicationLifetime
123+
IHostLifetime hostLifetime
125124
)
126-
: base(logger, hostLifetime, applicationLifetime)
125+
: base(logger, hostLifetime)
127126
{
128127
}
129128

130129
protected override async Task TryExecuteAsync(CancellationToken stoppingToken)
131130
{
132131
Logger.LogInformation("Sleeping, then throwing");
133-
134-
// Fake doing at least some work...
135-
await Task.Delay(1000, stoppingToken);
132+
133+
// Fake doing at least a second's work to not throw immediately...
134+
int secondsToWait = Debugger.IsAttached ? 1 : 31;
135+
136+
await Task.Delay(secondsToWait * 1000, stoppingToken);
136137

137138
// This will now stop the host application.
138139
throw new InvalidOperationException("This service is not supposed to start");

0 commit comments

Comments
 (0)