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: README.md
+42-23Lines changed: 42 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
# Atc.Hosting
4
4
5
-
The Atc.Hosting namespace serves as a toolbox for building scalable and reliable hosting solutions, with an emphasis on background services. It contains classes and extension methods designed to handle common hosting scenarios, providing enhanced features like custom logging, retries, and advanced configuration options. The namespace aims to streamline development efforts and improve the maintainability of hosted applications.
5
+
The Atc.Hosting namespace serves as a toolbox for building scalable and reliable hosting solutions, with an emphasis on background services. It contains classes and extension methods designed to handle common hosting scenarios, providing enhanced features like custom logging, and advanced configuration options. The namespace aims to streamline development efforts and improve the maintainability of hosted applications.
6
6
7
7
# Table of Contents
8
8
@@ -11,7 +11,6 @@ The Atc.Hosting namespace serves as a toolbox for building scalable and reliable
@@ -32,8 +31,7 @@ The Atc.Hosting namespace serves as a toolbox for building scalable and reliable
32
31
33
32
# BackgroundServiceBase`<T>`
34
33
35
-
The `BackgroundServiceBase<T>` class serves as a base for background services that require enhanced features like custom logging, retries, and configurable service options. It extends the ASP.NET Core's `BackgroundService` class, providing a more robust framework for handling
36
-
background tasks.
34
+
The `BackgroundServiceBase<T>` class serves as a base for continuous long running background services that require enhanced features like custom logging and configurable service options. It extends the ASP.NET Core's `BackgroundService` class, providing a more robust framework for handling background tasks.
37
35
38
36
## Features
39
37
@@ -42,20 +40,16 @@ background tasks.
42
40
- Utilizes `ILogger<T>` for type-specific, high-performance logging.
43
41
- Automatically enriches log entries with the name of the service type (`T`).
44
42
45
-
### Retry Mechanism
46
-
47
-
- Built-in retries using the `Polly` library.
48
-
- Retry count and exponential backoff settings are configurable.
49
-
50
43
### Error Handling
51
44
52
-
- Catches exceptions and logs them with a severity of `LogLevel.Warning`.
45
+
- Catches unhandled exceptions and logs them with a severity of `LogLevel.Warning`.
46
+
- Reruns the `DoWorkAsync` method after a configurable repeat interval.
47
+
- For manual error handling hook into the exception handling in `DoWorkAsync` by overriding the `OnExceptionAsync` method.
53
48
- Designed to log errors rather than crashing the service.
54
49
55
50
### Configuration Options
56
51
57
52
- Allows for startup delays.
58
-
- Configurable retry count.
59
53
- Configurable repeat interval for running tasks.
60
54
61
55
### Ease of Use
@@ -184,6 +178,23 @@ public override async Task DoWorkAsync(CancellationToken stoppingToken)
184
178
}
185
179
```
186
180
181
+
## Default implementation for `BackgroundServiceBase<>`
182
+
The `BackgroundServiceBase<>` automatically uses the `IBackgroundServiceHealthService` in the `DoWorkAsync` 'wait and retry' loop. This is archieved by providing the base constructor with a `IBackgroundServiceHealthService` instance.
183
+
184
+
```csharp
185
+
publicTimeFileWorker(
186
+
//...other parameters
187
+
IBackgroundServiceHealthServicehealthService,
188
+
//...other parameters
189
+
)
190
+
:base (healthService)
191
+
{
192
+
//...other initializations
193
+
}
194
+
```
195
+
196
+
Now you not have to set the running state of the service in the `BackgroundService.StartAsync` and `BackgroundService.StopAsync` methods.
197
+
187
198
# Complete TimeFileWorker example
188
199
189
200
A sample reference implementation can be found in the sample project [`Atc.Hosting.TimeFile.Sample`](sample/Atc.Hosting.TimeFile.Sample/Program.cs)
@@ -192,7 +203,6 @@ which shows an example of the service `TimeFileWorker` that uses `BackgroundServ
0 commit comments