Skip to content

Commit ce72f39

Browse files
committed
More docs
1 parent 9914a7c commit ce72f39

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

Readme.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
# WindowsServiceExtensions
2-
Make a .NET Core Windows Service that runs `IHostedService` background services power event aware.
2+
Make a .NET Core Windows Service that runs `IHostedService` background services power event aware. On consumer OS Windows 10, shutting down the computer will actually hibernate the OS. Services won't get another OnStart call.
33

4-
On consumer OS Windows 10, shutting down the computer will actually hibernate the OS. Services won't get another OnStart call.
4+
This project exists of a few classes that make building reliable Windows Services easier. The Lifetime class also include [kmcclellan's fixes](https://github.com/dotnet/runtime/issues/50019#issuecomment-678658133) that make the service throw when something fails on startup, instead of reprorting it started successfully.
55

6-
These extensions allow your IHostedServices to respond to this power state change:
6+
## Installation
7+
Through [NuGet](https://www.nuget.org/packages/CodeCaster.WindowsServiceExtensions/):
8+
9+
> Install-Package CodeCaster.WindowsServiceExtensions
10+
11+
## Usage
12+
These extensions allow your IHostedServices to respond to this power state change.:
713

814
* On your Host Builder, call `UsePowerEventAwareWindowsService()` instead of [`UseWindowsService()`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.windowsservicelifetimehostbuilderextensions.usewindowsservice?view=dotnet-plat-ext-3.1).
915
* Instead of letting your service inherit [`Microsoft.Extensions.Hosting.BackgroundService`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.backgroundservice?view=dotnet-plat-ext-5.0), inherit from `CodeCaster.WindowsServiceExtensions.PowerEventAwareBackgroundService`.
10-
* Implement the method `public override bool OnPowerEvent(PowerBroadcastStatus powerStatus)`
16+
* Implement the method `public override bool OnPowerEvent(PowerBroadcastStatus powerStatus)` and do your thing when it's called with a certain status.
1117

1218
Do note that the statuses received can vary. You get either `ResumeSuspend`, `ResumeAutomatic` or both, never neither, after a machine wake, reboot or boot.

src/CodeCaster.WindowsServiceExtensions/PowerEventAwareWindowsServiceLifetime.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class PowerEventAwareWindowsServiceLifetime : WindowsServiceLifetime, IHo
2525
private readonly IHostApplicationLifetime _applicationLifetime;
2626
private IReadOnlyCollection<IPowerEventAwareHostedService>? _hostedServices;
2727

28+
/// <inheritdoc />
2829
public PowerEventAwareWindowsServiceLifetime(IServiceProvider serviceProvider, IHostEnvironment environment, IHostApplicationLifetime applicationLifetime, ILoggerFactory loggerFactory, IOptions<HostOptions> optionsAccessor)
2930
: base(environment, applicationLifetime, loggerFactory, optionsAccessor)
3031
{
@@ -38,6 +39,7 @@ public PowerEventAwareWindowsServiceLifetime(IServiceProvider serviceProvider, I
3839
}
3940
}
4041

42+
/// <inheritdoc />
4143
public new async Task WaitForStartAsync(CancellationToken cancellationToken)
4244
{
4345
_logger.LogInformation("Windows Service start requested");
@@ -88,6 +90,7 @@ protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
8890
return true;
8991
}
9092

93+
/// <inheritdoc />
9194
protected override void OnStart(string[] args)
9295
{
9396
_logger.LogInformation("Windows Service OnStart() was called");

0 commit comments

Comments
 (0)