Skip to content

Commit 38c2254

Browse files
committed
Added configuration to appsettings
1 parent 1c97fc0 commit 38c2254

File tree

7 files changed

+84
-23
lines changed

7 files changed

+84
-23
lines changed

LongRunningConsole/AppConfig.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Text;
5+
6+
namespace LongRunningConsole
7+
{
8+
public class AppConfig
9+
{
10+
public string ApplicationName { get; set; }
11+
12+
public Collection<ServiceConfig> Services { get; set; }
13+
}
14+
}

LongRunningConsole/LongRunningConsole.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@
1818
<PackageReference Include="WindowsAzure.Storage" Version="7.2.1" />
1919
</ItemGroup>
2020

21+
<ItemGroup>
22+
<None Update="appsettings.json">
23+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
24+
</None>
25+
</ItemGroup>
26+
2127
</Project>

LongRunningConsole/Program.cs

+6-15
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,21 @@ public static async Task Main(string[] args)
4747
{
4848
services.AddOptions();
4949
services.AddLogging();
50+
services.Configure<AppConfig>(hostContext.Configuration.GetSection("AppConfig"));
5051
services.AddHostedService<MyService>();
5152
// services.AddHostedService<FileUploadService>();
5253
/* Add other services here as needed */
5354
})
5455
.ConfigureLogging(
55-
(hostContext, services) =>
56-
{
57-
services.AddConsole();
56+
(hostContext, logging) =>
57+
{
58+
logging.AddConfiguration(hostContext.Configuration.GetSection("Logging"));
59+
logging.AddConsole();
5860
})
5961
.RunConsoleAsync();
6062

6163
watch.Stop();
62-
Print($"\n\nPress any key to exit. Elapsed: {watch.Elapsed}");
63-
}
64-
65-
/// <summary>
66-
/// The print.
67-
/// </summary>
68-
/// <param name="message">
69-
/// The message.
70-
/// </param>
71-
private static void Print(string message)
72-
{
73-
Console.WriteLine(message);
64+
Console.WriteLine($"\n\nPress any key to exit. Elapsed: {watch.Elapsed}");
7465
}
7566
}
7667
}

LongRunningConsole/ServiceConfig.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace LongRunningConsole
6+
{
7+
public class ServiceConfig
8+
{
9+
public string ServiceName { get; set; }
10+
public int Frequency { get; set; }
11+
}
12+
}

LongRunningConsole/Services/FileUploadService.cs

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using System.Threading;
45
using System.Threading.Tasks;
56
using LongRunningConsole.Library;
67
using Microsoft.Extensions.Hosting;
78
using Microsoft.Extensions.Logging;
9+
using Microsoft.Extensions.Options;
810
using OfficeOpenXml;
911

1012
namespace LongRunningConsole.Services
@@ -31,19 +33,31 @@ public class FileUploadService : IHostedService, IDisposable
3133
/// The _timer.
3234
/// </summary>
3335
private Timer _timer;
34-
36+
37+
private readonly IOptions<AppConfig> _appConfig;
38+
39+
private readonly ServiceConfig serviceConfig;
40+
3541
/// <inheritdoc />
36-
public FileUploadService(ILogger<FileUploadService> logger)
42+
public FileUploadService(ILogger<FileUploadService> logger, IOptions<AppConfig> appConfig)
3743
{
38-
this._logger = logger ?? throw new ArgumentNullException(nameof(logger));
3944
this._logger.LogTrace("FileUploadService object has been instantiated.");
45+
this._logger = logger ?? throw new ArgumentNullException(nameof(logger));
46+
this._appConfig = appConfig ?? throw new ArgumentNullException(nameof(appConfig));
47+
48+
serviceConfig = this._appConfig.Value.Services.OfType<ServiceConfig>().FirstOrDefault(s => s.ServiceName.Equals(nameof(FileUploadService)));
49+
50+
if (serviceConfig is null)
51+
{
52+
throw new ApplicationException($"{nameof(serviceConfig)} cannot be null.");
53+
}
4054
}
4155

4256
/// <inheritdoc />
4357
public Task StartAsync(CancellationToken cancellationToken)
4458
{
4559
Console.WriteLine($"[{DateTime.Now}] {nameof(FileUploadService)} background Service is starting.");
46-
this._timer = new Timer(this.LoadFile, null, TimeSpan.Zero, TimeSpan.FromSeconds(60));
60+
this._timer = new Timer(this.LoadFile, null, TimeSpan.Zero, TimeSpan.FromSeconds(serviceConfig.Frequency));
4761
return Task.CompletedTask;
4862
}
4963

LongRunningConsole/Services/MyService.cs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
2+
using System.Linq;
23
using System.Threading;
34
using System.Threading.Tasks;
45
using Microsoft.Extensions.Hosting;
56
using Microsoft.Extensions.Logging;
7+
using Microsoft.Extensions.Options;
68

79
namespace LongRunningConsole.Services
810
{
@@ -21,15 +23,26 @@ public class MyService : IHostedService, IDisposable
2123
/// </summary>
2224
private Timer timer;
2325

26+
private readonly IOptions<AppConfig> _appConfig;
27+
28+
private readonly ServiceConfig serviceConfig;
29+
2430
/// <summary>
2531
/// Initializes a new instance of the <see cref="MyService"/> class.
2632
/// </summary>
2733
/// <param name="logger">
2834
/// The logger.
2935
/// </param>
30-
public MyService(ILogger<MyService> logger)
36+
public MyService(ILogger<MyService> logger, IOptions<AppConfig> appConfig)
3137
{
3238
this.logger = logger?? throw new ArgumentNullException(nameof(logger));
39+
this._appConfig = appConfig ?? throw new ArgumentNullException(nameof(appConfig));
40+
41+
serviceConfig = this._appConfig.Value.Services.OfType<ServiceConfig>().FirstOrDefault(s => s.ServiceName.Equals(nameof(MyService)));
42+
43+
if (serviceConfig is null) {
44+
throw new ApplicationException($"{nameof(serviceConfig)} cannot be null.");
45+
}
3346
}
3447

3548
/// <summary>
@@ -44,7 +57,7 @@ public MyService(ILogger<MyService> logger)
4457
public Task StartAsync(CancellationToken cancellationToken)
4558
{
4659
Console.WriteLine($"[{DateTime.Now}] {nameof(MyService)} background Service is starting.");
47-
this.timer = new Timer(this.DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(5));
60+
this.timer = new Timer(this.DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(serviceConfig.Frequency));
4861
return Task.CompletedTask;
4962
}
5063

LongRunningConsole/appsettings.json

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
{
2-
3-
}
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Trace"
5+
}
6+
},
7+
"AppConfig": {
8+
"ApplicationName": "Long-running console app",
9+
"Services": [
10+
{ "ServiceName": "MyService", "Frequency": 10 },
11+
{ "ServiceName": "FileUploadService", "Frequency": 5}
12+
]
13+
}
14+
}

0 commit comments

Comments
 (0)