Skip to content

Commit a0a94dc

Browse files
authored
Add all the worker sample source code (#4787)
1 parent 4052c8a commit a0a94dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1051
-0
lines changed

core/workers/.dockerignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
**/.classpath
2+
**/.dockerignore
3+
**/.env
4+
**/.git
5+
**/.gitignore
6+
**/.project
7+
**/.settings
8+
**/.toolstarget
9+
**/.vs
10+
**/.vscode
11+
**/*.*proj.user
12+
**/*.dbmdl
13+
**/*.jfm
14+
**/azds.yaml
15+
**/bin
16+
**/charts
17+
**/docker-compose*
18+
**/compose*
19+
**/Dockerfile*
20+
**/node_modules
21+
**/npm-debug.log
22+
**/obj
23+
**/secrets.dev.yaml
24+
**/values.dev.yaml
25+
README.md

core/workers/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
languages:
3+
- csharp
4+
products:
5+
- dotnet
6+
page_type: sample
7+
name: "Worker Services in .NET"
8+
urlFragment: "csharp-workers-fundamentals"
9+
description: "Several .NET 5 worker service applications that contain sample source code for interacting with IHostedService, and BackgroundService."
10+
---
11+
12+
# Logging in .NET sample source code
13+
14+
There are six sample source code projects in this collection of samples. The samples are written in C# and the related docs content is covered in [Worker Services in .NET][workers] articles. In addition to an overview, there are in-depth articles discussing queue service implementations, `BackgroundService` scenarios, custom `IHostedService` implementations, Windows Services interop with the `BackgroundService`, and even deploying a worker to Azure.
15+
16+
## Sample prerequisites
17+
18+
The samples are written in C# and targets .NET 5. It requires the [.NET 5.0 SDK](https://dotnet.microsoft.com/download/dotnet/5.0) or later.
19+
20+
## Building the sample
21+
22+
To download and run the sample, follow these steps:
23+
24+
1. Download and unzip the sample.
25+
2. In Visual Studio (2019 or later):
26+
1. On the menu bar, choose **File** > **Open** > **Project/Solution**.
27+
2. Navigate to the folder that holds the unzipped sample code, and open the C# project (.csproj) file.
28+
3. Choose the <kbd>F5</kbd> key to run with debugging, or <kbd>Ctrl</kbd>+<kbd>F5</kbd> keys to run the project without debugging.
29+
3. From the command line:
30+
1. Navigate to the folder that holds the unzipped sample code.
31+
2. At the command line, type [`dotnet run`](https://docs.microsoft.com/dotnet/core/tools/dotnet-run).
32+
33+
## More information
34+
35+
- [Worker Services in .NET][workers]
36+
- [Create a Queue Service][queue]
37+
- [Use scoped services within a `BackgroundService`][scoped-bgs]
38+
- [Create a Windows Service using `BackgroundService`][win-bgs]
39+
- [Implement the `IHostedService` interface][timer-svc]
40+
- [Deploy a Worker Service to Azure][cloud-svc]
41+
42+
[workers]: https://docs.microsoft.com/dotnet/core/extensions/workers
43+
[queue]: https://docs.microsoft.com/dotnet/core/extensions/queue-service
44+
[scoped-bgs]: https://docs.microsoft.com/dotnet/core/extensions/scoped-service
45+
[win-bgs]: https://docs.microsoft.com/dotnet/core/extensions/windows-service
46+
[timer-svc]: https://docs.microsoft.com/dotnet/core/extensions/timer-service
47+
[cloud-svc]: https://docs.microsoft.com/dotnet/core/extensions/cloud-service
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Worker">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<RootNamespace>App.WorkerService</RootNamespace>
6+
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
12+
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.14" />
13+
</ItemGroup>
14+
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# See https://aka.ms/containerfastmode to understand how Visual Studio uses this
2+
# Dockerfile to build your images for faster debugging.
3+
4+
FROM mcr.microsoft.com/dotnet/runtime:5.0 AS base
5+
WORKDIR /app
6+
7+
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
8+
WORKDIR /src
9+
COPY ["background-service/App.WorkerService.csproj", "background-service/"]
10+
RUN dotnet restore "background-service/App.WorkerService.csproj"
11+
COPY . .
12+
WORKDIR "/src/background-service"
13+
RUN dotnet build "App.WorkerService.csproj" -c Release -o /app/build
14+
15+
FROM build AS publish
16+
RUN dotnet publish "App.WorkerService.csproj" -c Release -o /app/publish
17+
18+
FROM base AS final
19+
WORKDIR /app
20+
COPY --from=publish /app/publish .
21+
ENTRYPOINT ["dotnet", "App.WorkerService.dll"]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace App.WorkerService
9+
{
10+
public class Program
11+
{
12+
public static void Main(string[] args)
13+
{
14+
CreateHostBuilder(args).Build().Run();
15+
}
16+
17+
public static IHostBuilder CreateHostBuilder(string[] args) =>
18+
Host.CreateDefaultBuilder(args)
19+
.ConfigureServices((hostContext, services) =>
20+
{
21+
services.AddHostedService<Worker>();
22+
});
23+
}
24+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"profiles": {
3+
"App.WorkerService": {
4+
"commandName": "Project",
5+
"environmentVariables": {
6+
"DOTNET_ENVIRONMENT": "Development"
7+
},
8+
"dotnetRunMessages": "true"
9+
},
10+
"Docker": {
11+
"commandName": "Docker"
12+
}
13+
}
14+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.Extensions.Hosting;
2+
using Microsoft.Extensions.Logging;
3+
using System;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
namespace App.WorkerService
8+
{
9+
public class Worker : BackgroundService
10+
{
11+
private readonly ILogger<Worker> _logger;
12+
13+
public Worker(ILogger<Worker> logger)
14+
{
15+
_logger = logger;
16+
}
17+
18+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
19+
{
20+
while (!stoppingToken.IsCancellationRequested)
21+
{
22+
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
23+
try
24+
{
25+
await Task.Delay(1000, stoppingToken);
26+
}
27+
catch (OperationCanceledException)
28+
{
29+
break;
30+
}
31+
}
32+
}
33+
}
34+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
}
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
**/.classpath
2+
**/.dockerignore
3+
**/.env
4+
**/.git
5+
**/.gitignore
6+
**/.project
7+
**/.settings
8+
**/.toolstarget
9+
**/.vs
10+
**/.vscode
11+
**/*.*proj.user
12+
**/*.dbmdl
13+
**/*.jfm
14+
**/azds.yaml
15+
**/bin
16+
**/charts
17+
**/docker-compose*
18+
**/compose*
19+
**/Dockerfile*
20+
**/node_modules
21+
**/npm-debug.log
22+
**/obj
23+
**/secrets.dev.yaml
24+
**/values.dev.yaml
25+
README.md

0 commit comments

Comments
 (0)