Skip to content

Commit a05519e

Browse files
authored
Merge pull request #21 from ralphhendriks/sample-to-netcoreapp3.1
Bring sample app more in line with Microsoft docs
2 parents 2baf34f + c0a24d4 commit a05519e

File tree

5 files changed

+27
-46
lines changed

5 files changed

+27
-46
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ dlldata.c
4646
project.lock.json
4747
project.fragment.lock.json
4848
artifacts/
49-
**/Properties/launchSettings.json
5049

5150
*_i.c
5251
*_p.c

samples/SimpleServiceSample/PrintTimeService.cs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,22 @@
66

77
namespace SimpleServiceSample
88
{
9-
public class PrintTimeService : IHostedService, IDisposable
9+
public class PrintTimeService : BackgroundService
1010
{
1111
private readonly ILogger _logger;
12-
private Timer _timer;
1312

1413
public PrintTimeService(ILogger<PrintTimeService> logger)
1514
{
1615
_logger = logger;
1716
}
1817

19-
public Task StartAsync(CancellationToken cancellationToken)
18+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
2019
{
21-
_timer = new Timer(DoWork, null, TimeSpan.Zero,
22-
TimeSpan.FromSeconds(5));
23-
24-
return Task.CompletedTask;
25-
}
26-
27-
private void DoWork(object state)
28-
{
29-
_logger.LogInformation($"The current time is: {DateTimeOffset.UtcNow}");
30-
}
31-
32-
public Task StopAsync(CancellationToken cancellationToken)
33-
{
34-
_timer?.Change(Timeout.Infinite, 0);
35-
36-
return Task.CompletedTask;
37-
}
38-
39-
public void Dispose()
40-
{
41-
_timer?.Dispose();
20+
while (!stoppingToken.IsCancellationRequested)
21+
{
22+
_logger.LogInformation("The current time is: {CurrentTime}", DateTimeOffset.UtcNow);
23+
await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken);
24+
}
4225
}
4326
}
4427
}

samples/SimpleServiceSample/Program.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ public static int Main(string[] args)
3030
try
3131
{
3232
Log.Information("Getting the motors running...");
33-
34-
BuildHost(args).Run();
35-
33+
CreateHostBuilder(args).Build().Run();
3634
return 0;
3735
}
3836
catch (Exception ex)
@@ -46,11 +44,9 @@ public static int Main(string[] args)
4644
}
4745
}
4846

49-
public static IHost BuildHost(string[] args) =>
50-
new HostBuilder()
51-
.ConfigureHostConfiguration(BuildConfiguration)
52-
.ConfigureServices(services => services.AddSingleton<IHostedService, PrintTimeService>())
53-
.UseSerilog()
54-
.Build();
47+
public static IHostBuilder CreateHostBuilder(string[] args) =>
48+
Host.CreateDefaultBuilder(args)
49+
.ConfigureServices(services => services.AddHostedService<PrintTimeService>())
50+
.UseSerilog();
5551
}
5652
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"profiles": {
3+
"SimpleServiceSample": {
4+
"commandName": "Project",
5+
"environmentVariables": {
6+
"ASPNETCORE_ENVIRONMENT": "Development"
7+
}
8+
}
9+
}
10+
}

samples/SimpleServiceSample/SimpleServiceSample.csproj

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk.Worker">
2+
23
<PropertyGroup>
3-
<TargetFramework>netcoreapp2.1</TargetFramework>
4-
<OutputType>Exe</OutputType>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55
</PropertyGroup>
6-
<ItemGroup>
7-
<Content Include="appsettings.json" CopyToOutputDirectory="PreserveNewest" />
8-
<Content Include="appsettings.Development.json" CopyToOutputDirectory="PreserveNewest" DependentUpon="appsettings.json" />
9-
</ItemGroup>
10-
6+
117
<ItemGroup>
128
<ProjectReference Include="..\..\src\Serilog.Extensions.Hosting\Serilog.Extensions.Hosting.csproj" />
139
</ItemGroup>
1410

1511
<ItemGroup>
16-
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.4" />
17-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
18-
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
19-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
12+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.4" />
2013
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
2114
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
2215
</ItemGroup>

0 commit comments

Comments
 (0)