Skip to content

Commit 77f1266

Browse files
committed
updated slides
1 parent 0a7ea5d commit 77f1266

1 file changed

Lines changed: 103 additions & 5 deletions

File tree

slides/Slides.md

Lines changed: 103 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ footer: 'https://chris-ayers.com'
146146
```
147147

148148

149-
```cs
149+
```csharp
150150
private string greeting = "";
151151
private int majorDotNetVersion = 0;
152152
public HomeController()
@@ -182,7 +182,59 @@ footer: 'https://chris-ayers.com'
182182

183183
---
184184

185-
# Keys are flattened
185+
# Binding
186+
187+
<div class="columns">
188+
<div>
189+
190+
```json
191+
{
192+
"Settings": {
193+
"KeyOne": 1,
194+
"KeyTwo": true,
195+
"KeyThree": {
196+
"Message": "Oh, that's nice...",
197+
"SupportedVersions": {
198+
"v1": "1.0.0",
199+
"v3": "3.0.7"
200+
}
201+
}
202+
}
203+
}
204+
```
205+
206+
</div>
207+
<div>
208+
209+
```csharp
210+
public sealed class Settings
211+
{
212+
public required int KeyOne { get; set; }
213+
public required bool KeyTwo { get; set; }
214+
public required NestedSettings KeyThree { get; set; };
215+
}
216+
public sealed class NestedSettings
217+
{
218+
public required string Message { get; set; };
219+
}
220+
221+
IConfigurationRoot config = new ConfigurationBuilder()
222+
.AddJsonFile("appsettings.json")
223+
.Build();
224+
225+
Settings? settings =
226+
config.GetRequiredSection("Settings")
227+
.Get<Settings>();
228+
229+
```
230+
231+
</div>
232+
</div>
233+
234+
---
235+
236+
# Hierarchical Configuration Data
237+
## Keys are Flattened
186238

187239
<div class="columns">
188240
<div>
@@ -289,7 +341,7 @@ footer: 'https://chris-ayers.com'
289341
```xml
290342
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
291343
```
292-
```cs
344+
```csharp
293345
IHostEnvironment env = builder.Environment;
294346

295347
builder.Configuration
@@ -330,7 +382,7 @@ builder.Configuration
330382
```xml
331383
<PackageReference Include="Microsoft.Extensions.Configuration.Xml" Version="8.0.0" />
332384
```
333-
```cs
385+
```csharp
334386
IHostEnvironment env = builder.Environment;
335387

336388
builder.Configuration
@@ -371,7 +423,7 @@ builder.Configuration
371423
```xml
372424
<PackageReference Include="Microsoft.Extensions.Configuration.Ini" Version="8.0.0" />
373425
```
374-
```cs
426+
```csharp
375427
IHostEnvironment env = builder.Environment;
376428

377429
builder.Configuration
@@ -404,9 +456,55 @@ Microsoft=Warning
404456
<div class="columns">
405457
<div>
406458

459+
- Typically used to override settings found in appsettings.json or user secrets
460+
- the : delimieter doesn't work for Hierarchical data on all platforms
461+
- the `__` delimieter is used instead of `:`
462+
463+
</div>
464+
<div>
465+
466+
```csharp
467+
public class TransientFaultHandlingOptions
468+
{
469+
public bool Enabled { get; set; }
470+
public TimeSpan AutoRetryDelay { get; set; }
471+
}
472+
```
473+
474+
```bash
475+
set SecretKey="Secret key from environment"
476+
set TransientFaultHandlingOptions__Enabled="true"
477+
set TransientFaultHandlingOptions__AutoRetryDelay="00:00:13"
478+
```
479+
480+
</div>
481+
</div>
482+
483+
---
484+
485+
# Environment Variables
486+
487+
<div class="columns">
488+
<div>
489+
490+
- There are built-in prefixes, like
491+
- ASPNETCORE_ for ASP.NET Core
492+
- DOTNET_ for .NET Core
493+
- You can provide your own prefix
494+
407495
</div>
408496
<div>
409497

498+
```csharp
499+
builder.Configuration.AddEnvironmentVariables(
500+
prefix: "MyCustomPrefix_");
501+
```
502+
```bash
503+
set MyCustomPrefix_MyKey="My key with MyCustomPrefix_"
504+
set MyCustomPrefix_Position__Title=Editor_with_custom
505+
set MyCustomPrefix_Position__Name=Environment
506+
dotnet run
507+
```
410508
</div>
411509
</div>
412510

0 commit comments

Comments
 (0)