Skip to content

Commit be37fb2

Browse files
(#2) Add example add fix issues
1 parent 8298e8e commit be37fb2

File tree

11 files changed

+203
-4
lines changed

11 files changed

+203
-4
lines changed

src/Example/Example.csproj

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<UserSecretsId>7f191ac2-a110-4f9d-989f-0c4afa1d9e45</UserSecretsId>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\Stravaig.Configuration.SqlServer\Stravaig.Configuration.SqlServer.csproj" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.0" />
16+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
17+
<PackageReference Include="Stravaig.Configuration.Diagnostics.Logging" Version="1.0.4" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<None Remove="appsettings.json" />
22+
<Content Include="appsettings.json">
23+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
24+
</Content>
25+
</ItemGroup>
26+
27+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Example;
2+
3+
public class MyFeatureConfiguration
4+
{
5+
public int SomeNumber { get; set; }
6+
7+
public string SomeString { get; set; }
8+
9+
public double SomeFloatingPointNumber { get; set; }
10+
11+
public bool SomeBoolean { get; set; }
12+
}

src/Example/Program.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// See https://aka.ms/new-console-template for more information
2+
3+
using Example;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Hosting;
7+
using Microsoft.Extensions.Logging;
8+
using Stravaig.Configuration.SqlServer;
9+
10+
await Host.CreateDefaultBuilder(args)
11+
.ConfigureAppConfiguration(builder =>
12+
{
13+
builder.AddUserSecrets<Program>();
14+
builder.AddSqlServer(opts =>
15+
{
16+
opts.FromExistingConfiguration();
17+
});
18+
})
19+
.ConfigureLogging(builder =>
20+
{
21+
builder.AddConsole();
22+
builder.AddDebug();
23+
})
24+
.ConfigureServices((ctx, services) =>
25+
{
26+
IConfigurationRoot configRoot = (IConfigurationRoot) ctx.Configuration;
27+
services.AddTransient<IHostedService, TheHostedService>();
28+
services.AddSingleton(configRoot);
29+
services.Configure<MyFeatureConfiguration>(configRoot.GetSection("MyConfiguration"));
30+
})
31+
.RunConsoleAsync();

src/Example/TheHostedService.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Text.Json;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using System.Timers;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
using Microsoft.Extensions.Options;
10+
using Stravaig.Configuration.Diagnostics.Logging;
11+
using Timer = System.Timers.Timer;
12+
13+
14+
namespace Example;
15+
16+
public class TheHostedService : IHostedService, IDisposable
17+
{
18+
private readonly ILogger<TheHostedService> _logger;
19+
private readonly IConfigurationRoot _configRoot;
20+
private readonly IOptions<MyFeatureConfiguration> _featureValues;
21+
private readonly Timer _timer;
22+
23+
public TheHostedService(
24+
ILogger<TheHostedService> logger,
25+
IConfigurationRoot configRoot,
26+
IOptions<MyFeatureConfiguration> featureValues)
27+
{
28+
_logger = logger;
29+
_configRoot = configRoot;
30+
_featureValues = featureValues;
31+
_timer = new Timer(10000);
32+
_timer.Elapsed += TimerOnElapsed;
33+
}
34+
35+
private void TimerOnElapsed(object? sender, ElapsedEventArgs e)
36+
{
37+
var jsonOptions = new JsonSerializerOptions()
38+
{
39+
WriteIndented = true,
40+
};
41+
string json = JsonSerializer.Serialize(_featureValues.Value, jsonOptions);
42+
Console.Clear();
43+
_logger.LogInformation(
44+
"At {Time} the object looks like:\n{Json}",
45+
e.SignalTime,
46+
json);
47+
_logger.LogConfigurationValuesAsInformation(_configRoot.GetSection("MyConfiguration"));
48+
}
49+
50+
public Task StartAsync(CancellationToken cancellationToken)
51+
{
52+
_logger.LogInformation("The hosted service is starting.");
53+
_logger.LogProvidersAsInformation(_configRoot);
54+
_timer.Enabled = true;
55+
return Task.CompletedTask;
56+
}
57+
58+
public Task StopAsync(CancellationToken cancellationToken)
59+
{
60+
_timer.Enabled = false;
61+
_logger.LogInformation("The hosted service is ending.");
62+
return Task.CompletedTask;
63+
}
64+
65+
public void Dispose()
66+
{
67+
_timer.Dispose();
68+
}
69+
}

src/Example/appsettings.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"Stravaig": {
3+
"AppConfiguration": {
4+
"SchemaName": "Stravaig",
5+
"TableName": "AppConfiguration",
6+
"ConnectionString": "*** Found in User Secrets ***"
7+
}
8+
},
9+
"MyConfiguration": {
10+
"SomeNumber": 123,
11+
"SomeString": "A value from appsettings",
12+
"SomeBoolean": "true",
13+
"SomeFloatingPointNumber": 123.456
14+
}
15+
}

src/SQL Scripts/CreateAppConfigurationTable.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
CREATE SCHEMA Stravaig;
44
GO
5-
CREATE TABLE Stravaig.AppConfiguation
5+
CREATE TABLE Stravaig.AppConfiguration
66
(
77
ConfigKey NVARCHAR(1024) PRIMARY KEY CLUSTERED,
88
ConfigValue NVARCHAR(MAX)

src/Stravaig.Configuration.SqlServer.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stravaig.Configuration.SqlS
2626
EndProject
2727
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stravaig.Configuration.SqlServer.Tests", "Stravaig.Configuration.SqlServer.Tests\Stravaig.Configuration.SqlServer.Tests.csproj", "{53C021C0-CFED-42BE-AD1D-DDAB4F44E83D}"
2828
EndProject
29+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "Example\Example.csproj", "{A5C751ED-34E2-49E2-8A49-E927D1B674EA}"
30+
EndProject
2931
Global
3032
GlobalSection(SolutionProperties) = preSolution
3133
HideSolutionNode = FALSE
@@ -42,6 +44,10 @@ Global
4244
{53C021C0-CFED-42BE-AD1D-DDAB4F44E83D}.Debug|Any CPU.Build.0 = Debug|Any CPU
4345
{53C021C0-CFED-42BE-AD1D-DDAB4F44E83D}.Release|Any CPU.ActiveCfg = Release|Any CPU
4446
{53C021C0-CFED-42BE-AD1D-DDAB4F44E83D}.Release|Any CPU.Build.0 = Release|Any CPU
47+
{A5C751ED-34E2-49E2-8A49-E927D1B674EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
48+
{A5C751ED-34E2-49E2-8A49-E927D1B674EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
49+
{A5C751ED-34E2-49E2-8A49-E927D1B674EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
50+
{A5C751ED-34E2-49E2-8A49-E927D1B674EA}.Release|Any CPU.Build.0 = Release|Any CPU
4551
EndGlobalSection
4652
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4753
Debug|Any CPU = Debug|Any CPU

src/Stravaig.Configuration.SqlServer/DataLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public IEnumerable<KeyValuePair<string, string>> RetrieveData(SqlServerConfigura
2020
connection.Open();
2121
var cmd = new SqlCommand(sql, connection);
2222
using var reader = cmd.ExecuteReader();
23-
while (reader.HasRows)
23+
while (reader.Read())
2424
{
2525
var key = reader.GetString(keyColumnPosition);
2626
var value = reader.GetString(valueColumnPosition);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Stravaig.Configuration.SqlServer;
2+
3+
public static class DefaultValues
4+
{
5+
public const string SchemaName = "Stravaig";
6+
public const string TableName = "AppConfiguration";
7+
public const string ConfigurationSection = "Stravaig:AppConfiguration";
8+
}
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
using System;
2+
13
namespace Stravaig.Configuration.SqlServer;
24

35
public class SqlServerConfigurationOptions
46
{
57
public string? ConnectionString { get; set; }
68
public string? ConfigurationSection { get; set; }
7-
public string SchemaName { get; set; } = "Stravaig";
8-
public string TableName { get; set; } = "AppConfiguration";
9+
public string SchemaName { get; set; } = DefaultValues.SchemaName;
10+
public string TableName { get; set; } = DefaultValues.TableName;
11+
12+
public SqlServerConfigurationOptions FromExistingConfiguration(string configurationSection = DefaultValues.ConfigurationSection)
13+
{
14+
ConfigurationSection = configurationSection ?? throw new ArgumentNullException(nameof(configurationSection));
15+
ConnectionString = null;
16+
return this;
17+
}
918
}

0 commit comments

Comments
 (0)