You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/index.md
+9-11Lines changed: 9 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,17 +6,17 @@ order: 1
6
6
# Windows Service Extensions
7
7
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.
8
8
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.
10
10
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.
12
12
13
13
## Installation
14
14
Through [NuGet](https://www.nuget.org/packages/CodeCaster.WindowsServiceExtensions/):
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:
20
20
21
21
```csharp
22
22
varhostBuilder=newHostBuilder()
@@ -25,6 +25,7 @@ var hostBuilder = new HostBuilder()
// Do your continuous or periodic background work.
44
+
// Do your continuous or periodic background work, or just a short task and return.
44
45
awaitSomeLongRunningTaskAsync();
45
46
}
46
47
}
@@ -55,6 +56,8 @@ This library used to contain exception handling code in a base service, which is
55
56
56
57
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.
57
58
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
+
58
61
## Host Builder (dependency injection)
59
62
To receive session or power events, call `UseWindowsServiceExtensions()` on your Host Builder:
60
63
@@ -75,7 +78,7 @@ var hostBuilder = new HostBuilder()
75
78
```
76
79
77
80
## 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:
@@ -136,9 +139,4 @@ public class MyCoolBackgroundService : WindowsServiceBackgroundService
136
139
137
140
You might receive multiple `OnPowerEvent()`/`OnSessionChange()` calls in succession, be sure to lock and/or debounce where appropriate.
138
141
139
-
**TODO**: we can do that.
140
-
141
142
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.
* 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.
Copy file name to clipboardExpand all lines: docs/upgrading-v2-v3.md
+8-4Lines changed: 8 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,8 +4,12 @@ order: 2
4
4
---
5
5
# Upgrade Guide
6
6
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.
8
15
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