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
description: Learn how to configure and manage startup tasks in .NET Orleans.
4
-
ms.date: 07/03/2024
2
+
title: Background Services and Startup Tasks
3
+
description: Learn how to configure and manage background services and startup tasks in .NET Orleans.
4
+
ms.date: 11/19/2024
5
5
---
6
6
7
-
# Startup tasks
7
+
# Background Services and Startup Tasks
8
8
9
-
In many cases, some task needs to be performed automatically as soon as a silo becomes available. Startup tasks provide this functionality.
9
+
When building Orleans applications, you often need to perform background operations or initialize components when the application starts.
10
10
11
-
Some use cases include, but are not limited to:
11
+
Startup tasks can be used to perform initialization work when a silo starts, before or after it begins accepting requests. Common use cases include:
12
12
13
-
* Starting background timers to perform periodic housekeeping tasks
14
-
* Pre-loading some cache grains with data downloaded from external backing storage
13
+
* Initializing grain state or preloading data
14
+
* Setting up external service connections
15
+
* Performing database migrations
16
+
* Validating configuration
17
+
* Warming up caches
15
18
16
-
Any exceptions that are thrown from a startup task during startup will be reported in the silo log and will stop the silo.
19
+
## Using BackgroundService (Recommended)
17
20
18
-
This fail-fast approach is the standard way that Orleans handles silo start-up issues, and is intended to allow any problems with silo configuration and/or bootstrap logic to be easily detected during testing phases rather than being silently ignored and causing unexpected problems later in the silo lifecycle.
21
+
The recommended approach is to use .NET [BackgroundService or `IHostedService`](/aspnet/core/fundamentals/host/hosted-services). See the [Background tasks with hosted services in ASP.NET Core](/aspnet/core/fundamentals/host/hosted-services) documentation for more information.
19
22
20
-
## Configure startup tasks
23
+
Here's an example that pings a grain every 5 seconds:
21
24
22
-
Startup tasks can be configured using the <xref:Orleans.Hosting.ISiloHostBuilder> either by registering a delegate to be invoked during startup or by registering an implementation of <xref:Orleans.Runtime.IStartupTask>.
catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
61
+
{
62
+
// Ignore cancellation during shutdown.
63
+
}
64
+
finally
65
+
{
66
+
_logger.LogInformation("Grain ping service is shutting down.");
67
+
}
68
+
}
69
+
}
70
+
```
71
+
72
+
Registration order is significant, since services added to the host builder are started one-by-one, in the order they are registered. You can register the background service as follows:
> While startup tasks are still supported, we recommend using `BackgroundService` or `IHostedService` instead as they are the common .NET hosting mechanism for running background tasks.
133
+
134
+
> [!WARNING]
135
+
> Any exceptions thrown from a startup task will be reported in the silo log and will stop the silo. This fail-fast approach helps detect configuration and bootstrap issues during testing rather than having them cause unexpected problems later, but it can also mean that transient failures in a startup task will cause unavailability of the host.
136
+
137
+
If you need to use the built-in startup task system, you can configure them as follows:
138
+
139
+
### Register a delegate
140
+
141
+
A delegate can be registered as a startup task using the appropriate <xref:Orleans.Hosting.SiloBuilderStartupExtensions.AddStartupTask*> extension method on <xref:Orleans.Hosting.ISiloBuilder>.
First, we must define an implementation of `IStartupTask`:
155
+
The <xref:Orleans.Runtime.IStartupTask> interface can be implemented and registered as a startup task using the <xref:Orleans.Hosting.SiloBuilderStartupExtensions.AddStartupTask*> extension method on <xref:Orleans.Hosting.ISiloBuilder>.
42
156
43
157
```csharp
44
158
publicclassCallGrainStartupTask : IStartupTask
@@ -50,14 +164,14 @@ public class CallGrainStartupTask : IStartupTask
0 commit comments