Created
February 4, 2023 07:03
-
-
Save jcotton42/f27b459d517c8378e1a504a50a63e896 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Logging": { | |
"LogLevel": { | |
"Default": "Information", | |
"Microsoft.Hosting.Lifetime": "Information" | |
} | |
}, | |
"Test": { | |
"2": 7 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Logging": { | |
"LogLevel": { | |
"Default": "Information", | |
"Microsoft.Hosting.Lifetime": "Information" | |
} | |
}, | |
"Test": [ | |
4, | |
5, | |
6, | |
7 | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Test; | |
IHost host = Host.CreateDefaultBuilder(args) | |
.ConfigureServices(services => | |
{ | |
services.AddHostedService<Worker>(); | |
}) | |
.Build(); | |
var config = host.Services.GetRequiredService<IConfiguration>(); | |
var test = config.GetSection("Test").Get<int[]>(); | |
foreach (var i in test) { Console.WriteLine(i); } | |
host.Run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk.Worker"> | |
<PropertyGroup> | |
<TargetFramework>net7.0</TargetFramework> | |
<Nullable>enable</Nullable> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<UserSecretsId>dotnet-Test-cfd5f096-f5a0-4e31-bd59-1542b24ca3ec</UserSecretsId> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" /> | |
</ItemGroup> | |
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Test; | |
public class Worker : BackgroundService | |
{ | |
private readonly ILogger<Worker> _logger; | |
public Worker(ILogger<Worker> logger) | |
{ | |
_logger = logger; | |
} | |
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | |
{ | |
while (!stoppingToken.IsCancellationRequested) | |
{ | |
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); | |
await Task.Delay(1000, stoppingToken); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment