Skip to content

Commit 168cd82

Browse files
committed
Update docs
1 parent 4e8caea commit 168cd82

5 files changed

Lines changed: 29 additions & 16 deletions

File tree

Docs.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</PropertyGroup>
1111
<ItemGroup>
1212
<Content Include="$(MSBuildThisFileDirectory)docs\downloads.md" />
13+
<Content Include="$(MSBuildThisFileDirectory)docs\roadmap.md" />
1314
<Content Include="$(MSBuildThisFileDirectory)docs\upgrading-v2-v3.md" />
1415
<Content Include="$(MSBuildThisFileDirectory)docs\_layouts\cayman-with-menu.html" />
1516
<Content Include="$(MSBuildThisFileDirectory)docs\_config.yml" />

docs/downloads.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Downloads
3-
order: 3
3+
order: 4
44
---
55
## Get it now on NuGet
66
Through [NuGet](https://www.nuget.org/packages/CodeCaster.WindowsServiceExtensions/):

docs/index.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ order: 1
66
# Windows Service Extensions
77
This package is relevant to developers who want to write reliable background tasks running under a Windows Service. The .NET BackgroundService is nice, but is abstracted away from a Windows Service, because it's not designed to be one.
88

9-
This is meant as a utility library that glues BackgroundServices and Windows Services together.
9+
Running as a _Windows Service_ and _running BackgroundServices_ are two separate things though, and they are not connected in any way. A non-service console app can run background services, and so can and do web applications. Each of those are different hosting environments, with their own lifetimes.
1010

11-
**NOTE**: these docs are currently for the develop branch. Once the according version v3.0.0 is released, it'll be from main.
11+
This project exists of a few classes that make building reliable Windows Services easier, to gap this disconnect.
1212

1313
## Installation
1414
Through [NuGet](https://www.nuget.org/packages/CodeCaster.WindowsServiceExtensions/):
1515

1616
> Install-Package CodeCaster.WindowsServiceExtensions
1717

1818
## Why should you use this?
19-
A usual Windows Service program might look like this:
19+
Using .NET's [`UseWindowsService()`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.windowsservicelifetimehostbuilderextensions.usewindowsservice?view=dotnet-plat-ext-6.0) (from the Platform Extensions package `Microsoft.Extensions.Hosting.WindowsServices`) and [`Microsoft.Extensions.Hosting.BackgroundService`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.backgroundservice?view=dotnet-plat-ext-6.0) from `Microsoft.Extensions.Hosting.Abstractions`, a usual Windows Service program hosting some long-running background tasks could look like this:
2020

2121
```csharp
2222
var hostBuilder = new HostBuilder()
@@ -25,6 +25,7 @@ var hostBuilder = new HostBuilder()
2525
{
2626
// Add our IHostedService
2727
s.AddHostedService<MyCoolBackgroundService>();
28+
s.AddHostedService<MyShortRunningBackgroundService>();
2829
})
2930
.UseWindowsService();
3031

@@ -33,14 +34,14 @@ var host = hostBuilder.Build;
3334
await host.RunAsync();
3435
```
3536

36-
And then your service would look like this:
37+
And then a service would look like this:
3738

3839
```csharp
3940
public class MyCoolBackgroundService : BackgroundService
4041
{
4142
protected override Task ExecuteAsync(CancellationToken stoppingToken)
4243
{
43-
// Do your continuous or periodic background work.
44+
// Do your continuous or periodic background work, or just a short task and return.
4445
await SomeLongRunningTaskAsync();
4546
}
4647
}
@@ -55,6 +56,8 @@ This library used to contain exception handling code in a base service, which is
5556
5657
With the [retirement of .NET 5 on May 8, 2022](https://docs.microsoft.com/en-us/lifecycle/products/microsoft-net-and-net-core), this WindowsServiceExtensions library targets .NET (Platform Extensions) 6 going forward from v3.0.0.
5758

59+
However, in the case of a background service excption, the service doesn't report an error to the Service Control Manager, who will think the process exited nicely. This library fixes that.
60+
5861
## Host Builder (dependency injection)
5962
To receive session or power events, call `UseWindowsServiceExtensions()` on your Host Builder:
6063

@@ -75,7 +78,7 @@ var hostBuilder = new HostBuilder()
7578
```
7679

7780
## Events
78-
If you let your service inherit `CodeCaster.WindowsServiceExtensions.Service.WindowsServiceBackgroundService` instead of [`Microsoft.Extensions.Hosting.BackgroundService`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.backgroundservice?view=dotnet-plat-ext-5.0) (the former indirectly inherits the latter, see above), you get two new methods:
81+
If you let your service inherit `CodeCaster.WindowsServiceExtensions.Service.WindowsServiceBackgroundService` (or implement `IWindowsServiceAwareHostedService`) instead of [`Microsoft.Extensions.Hosting.BackgroundService`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.backgroundservice?view=dotnet-plat-ext-5.0) (the former indirectly inherits the latter, see above), you get two new methods:
7982

8083
```csharp
8184
public class MyCoolBackgroundService : WindowsServiceBackgroundService
@@ -136,9 +139,4 @@ public class MyCoolBackgroundService : WindowsServiceBackgroundService
136139

137140
You might receive multiple `OnPowerEvent()`/`OnSessionChange()` calls in succession, be sure to lock and/or debounce where appropriate.
138141

139-
**TODO**: we can do that.
140-
141142
Do note that the statuses received can vary. You get either `ResumeSuspend`, `ResumeAutomatic` or both, never neither, after a machine wake, reboot or boot.
142-
143-
## TODO
144-
When the task returns, the host stays up. This might be a problem if you start multiple background services that should shut down the application when the last one has done its work.

docs/roadmap.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: Roadmap
3+
order: 3
4+
---
5+
# v3+
6+
Wishlist for the next versions:
7+
8+
* Debounce the power events.
9+
* Expose more relevant service methods/events to BackgroundServices, use events/CancellationTokens?
10+
* When the last BackgroundService's `ExecuteAsync()` returns, the host stays up. This might be a problem if you start multiple background services that should shut down the application when the last one has done its work.

docs/upgrading-v2-v3.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ order: 2
44
---
55
# Upgrade Guide
66

7-
**TODO**: explain this, taken from root Readme.md.
7+
## Dependency injection
8+
The DI extension method `IHostBuilder.UsePowerEventAwareWindowsService()` is now called `UseWindowsServiceExtensions()` because we do more than power events now.
9+
10+
## BackgroundService base class
11+
The long-running hosted service base class `CodeCaster.WindowsServiceExtensions.PowerEventAwareBackgroundService` was renamed to `CodeCaster.WindowsServiceExtensions.Service.WindowsServiceBackgroundService`, because the former didn't have enough "Service" in its name.
12+
13+
## Exception handling
14+
Instead of `BackgroundService.ExecuteAsync()`, which is now sealed, override `WindowsServiceBackgroundService.TryExecuteAsync()` to do your long-running work.
815

9-
* The DI extension method `IHostBuilder.UsePowerEventAwareWindowsService()` is now called `UseWindowsServiceExtensions()` because we do more than power events now.
10-
* The long-running hosted service base class `CodeCaster.WindowsServiceExtensions.PowerEventAwareBackgroundService` was renamed to `CodeCaster.WindowsServiceExtensions.Service.WindowsServiceBackgroundService`, because the former didn't have enough "Service" in its name.
11-
* Instead of `BackgroundService.ExecuteAsync()`, which is now sealed, override `WindowsServiceBackgroundService.TryExecuteAsync()` to do your long-running work.

0 commit comments

Comments
 (0)