Compare commits

..

2 Commits

Author SHA1 Message Date
1974c5328d Merge pull request 'background service doc' (#3) from feat/background-service into main
All checks were successful
Build, Test & Publish / Build (push) Successful in 18s
Build, Test & Publish / Build and Publish Container Image (push) Successful in 28s
Build, Test & Publish / Deploy to Infrastructure (push) Successful in 1m2s
Reviewed-on: #3
2024-10-11 09:41:11 +11:00
589793841e background service doc
All checks were successful
Build, Test & Publish / Build (pull_request) Successful in 23s
Build, Test & Publish / Build and Publish Container Image (pull_request) Has been skipped
Build, Test & Publish / Deploy to Infrastructure (pull_request) Has been skipped
2024-10-11 09:38:58 +11:00
3 changed files with 60 additions and 1 deletions

View File

@ -24,6 +24,7 @@ export default defineConfig({
link: '/dotnet/', link: '/dotnet/',
collapsed: true, collapsed: true,
items: [ items: [
{ text: 'Background Services', link: '/dotnet/background-service' },
{ text: 'Blazor with an API', link: '/dotnet/blazor-with-api' }, { text: 'Blazor with an API', link: '/dotnet/blazor-with-api' },
{ text: 'Database Seeding', link: '/dotnet/database-seed' }, { text: 'Database Seeding', link: '/dotnet/database-seed' },
{ text: 'Dockerising Blazor', link: '/dotnet/dockerising-blazor' }, { text: 'Dockerising Blazor', link: '/dotnet/dockerising-blazor' },
@ -60,7 +61,7 @@ export default defineConfig({
link: '/docker/', link: '/docker/',
collapsed: true, collapsed: true,
items: [ items: [
{ text: 'Docker Exec', link: '/docker/exec-into-contanainer' }, { text: 'Docker Exec', link: '/docker/exec-into-container' },
] ]
}, },
{ {

View File

@ -0,0 +1,57 @@
# Background Services
Often in application you will want logic to execute in the background. This could be for a variety of reasons, such as:
* Processing data
* Sending emails
* Performing maintenance tasks
* Running scheduled jobs
In .NET Core, you can create a background service by creating a class that inherits from the `BackgroundService` class. This class is part of the `Microsoft.Extensions.Hosting` namespace.
::: warn
The ability of a background service to run is dependent on the lifetime of the host. If the host is stopped, the background service will also stop. This means for software running in IIS you will need to ensure the site is always running. For this reason docker deployments are usually preferred
:::
## Creating a Background Service
To create a background service, you need to create a class that inherits from the `BackgroundService` class. This class has a single method that you need to override, called `ExecuteAsync`. This method is called when the background service is started, and it should contain the logic that you want to run in the background.
Here is an example of a simple background service that writes to the console every second:
```csharp
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
namespace BackgroundServiceExample
{
public class MyBackgroundService : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
Console.WriteLine("Background service is running...");
await Task.Delay(1000);
}
}
}
}
```
## Registering the Background Service
The next step is to register the background service with the dependency injection container. You can do this in `Program.cs` when you are building the host.
```csharp
// ...
builder.Services.AddHostedService<MyBackgroundService>();
// ...
```
## Conclusion
This is just a simple way to use background services, for more complex scenarios you may want to look at writing a console app that runs as a service which can be triggered by a scheduler or a message queue. This will also give you flexibility to run the service on a different machine or in a container and have separate configuration and so on.

View File

@ -1,5 +1,6 @@
# .NET Snippets and Musings # .NET Snippets and Musings
#### [Background Services](./background-service.md)
#### [Blazor with an inbuilt API](./blazor-with-api.md) #### [Blazor with an inbuilt API](./blazor-with-api.md)
#### [Database Seeding](./database-seed.md) #### [Database Seeding](./database-seed.md)
#### [Dockerising a Blazor Web App](./dockerising-blazor.md) #### [Dockerising a Blazor Web App](./dockerising-blazor.md)