SlideShare a Scribd company logo
.NET Core: Configuration
Fundamentals
David Dieruf - Pivotal
July, 2019
Let's Talk About...
Cloud-native environment and goals
Why we are here
Designing config and environment variables
Different patterns
About .NET Core configuration
Building config providers in WebHost and its hierarchy of values
The options pattern
Extending basic config with objects
External configuration servers
How they work and why you choose them
Putting the pieces together
Using Steeltoe and Spring Config to reach cloud-native goals
Environment: Local
Network
Services: DB, Cache, SMB
Environment: Stage Environment: Prod
Team Foundation Server (CI/CD): Git, Build, Release
My-App
Network
Services: DB, Cache, SMB
Network
Services: DB, Cache, SMB
My-App My-App
- One build
- Test once (for all environments)
- Parity between environments
- Automation
My-App My-App
public void ConfigureServices(IServiceCollection services) {
String connString = Environment.GetEnvironmentVariable("conn_string");
services.AddDbContext<MyContext>(options => options.UseSqlServer(connString));
}
Environment variables in the cloud
How is the value set?
Do you have to manually update the value per
instance, or script the action?
How easy is promotion?
Do you have to recompile to move between
environments? Does your build pipeline manipulate
settings?
Do you have a history of changes?
Do you know when a value was set and by who?
Atomic value in the
cloud
Per app instance
App can not run without
a value
Agnostic to its platform
or IaaS (portable)
Appsettings.json
● [Good] External to the
compiled application
● [Good] No recompilation
needed
● [Challenge] Have to change
at every instance
Internal Class Command Line
● [Good] Secure information
in compiled bits
● [Challenge] Hard coded
values in app
● [Challenge] Can not change
without recompiling
● [Good] External to the
compiled application
● [Challenge] Have to change
at every instance
● [Challenge] Lost values
because no source check-in
(Manual)
Environment Variables
Popular Configuration Design
● [Good] External to the
compiled application
● [Good] Application
compiled once and
promoted through
environment
● [Challenge] Have to change
at every host
class MyClass {
string connString = "xxxx"
}
{
"ConnectionStrings": {
"a-database": "xxxxx"
}
}
C: dotnet run my-app `
--conn "xxxx" `
--something "yyyyy"
string connString =
Environment.GetEnvironmentVari
able("my-conn")
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration
.NET Core Configuration
Using Default
WebHost
Loading configuration into your app
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
- use Kestrel as the web server and configure it using the
application's configuration providers
- set the ContentRootPath to the result of GetCurrentDirectory()
- load IConfiguration from 'appsettings.json' and
'appsettings.[EnvironmentName].json'
- load IConfiguration from User Secrets when EnvironmentName
is 'Development' using the entry assembly
- load IConfiguration from environment variables
- load IConfiguration from supplied command line args
- configure the ILoggerFactory to log to the console and debug
output
- enable IIS integration
program.cs
WebHost.CreateDefaultBuilder(args)
Build your own
WebHost
Loading configuration into your app
public static IWebHost BuildWebHost(string[] args) {
var builder = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) => {
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json",
optional: true);
config.AddEnvironmentVariables();
config.AddCommandLine(args);
})
.UseIISIntegration()
.UseStartup<Startup>();
return builder.Build();
}
program.cs
- Options manipulate how configuration values are overwritten
- Options to [not] include other configuration providers, like
custom external config server provider
var builder = new WebHostBuilder()
Config value
hierarchy
Overwrite values based on
environment
public static IWebHost BuildWebHost(string[] args) {
var builder = new WebHostBuilder()
...
.ConfigureAppConfiguration((hostingContext, config) => {
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json");
config.AddEnvironmentVariables();
config.AddCommandLine(args);
})
...
1. Load values from appsettings.json
2. (if provided) Load values from environment
specific appsettings.{env}.json
3. (if present) Load values from environment
variables
4. (if present) Load values provided when app
was started
config.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json");
config.AddEnvironmentVariables();
config.AddCommandLine(args);
{
"my_val": "something"
}
Example Value Overwriting
1. config.AddJsonFile("appsettings.json")
{
"my_val": "another"
}
2. .AddJsonFile("appsettings.staging.json")
PS c: $env:my_val = "yet another"
3. config.AddEnvironmentVariables()
PS c: dotnet run my-app --my_val "again another"
4. config.AddCommandLine(args)
my_val == “again another”
Configuration
Providers
Supported providers for
configuration of ASP.NET core app
Provider
Provides configuration
from…
Azure Key Vault Configuration Provider (Security topics) Azure Key Vault
Azure App Configuration Provider (Azure documentation) Azure App Configuration
Command-line Configuration Provider Command-line parameters
Custom configuration provider Custom source
Environment Variables Configuration Provider Environment variables
File Configuration Provider Files (INI, JSON, XML)
Key-per-file Configuration Provider Directory files
Memory Configuration Provider In-memory collections
User secrets (Secret Manager) (Security topics) File in the user profile directory
Consuming
Values
Using
Microsoft.Extensions.Configuration
public class ValuesController : Controller{
private readonly IConfiguration _config;
public ValuesController(IConfiguration config){
_config = config;
}
[HttpGet]
public ActionResult<string> Get(){
string val = _config.GetValue<string>("my_val");
return val;
}
}
- No consideration for handler type(s)
- Simple key:value store
- Can be complex type
ValuesController.cs
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options
Options Pattern
Options Pattern Using Microsoft.Extensions.Configuration.Options
{
"section0": {
"key0": "value",
"key1": "value"
},
"section1": {
"key0": "value",
"key1": "value"
}
}
public void ConfigureServices(IServiceCollection services) {
services.AddOptions();
services.Configure<SectionZeroOptions>(Configuration.GetSection("section0"));
...
}
public class SectionZeroOptions {
public string key0 {get;set;}
public string key1 { get; set; }
}
public class MyService : IMyService {
private IOptions<SectionZeroOptions> _SectionZeroOptions;
public MyService(IOptions<SectionZeroOptions> sectionZeroOptions) {
_SectionZeroOptions = sectionZeroOptions;
}
public string DoSomething(){
string key0 = _SectionZeroOptions.Value.key0;
string key1 = _SectionZeroOptions.Value.key1;
return key0;
}
}
1. appsettings.json values
2. Matching class to json
3. Enable the Options configuration
4. Consume values
{
"section0": {
"key0": "value",
"key1": "value"
},
"section1": {
"key0": "value",
"key1": "value"
}
}
public class SectionZeroOptions {
public string key0 {get;set;}
public string key1 { get; set; }
}
public void ConfigureServices(IServiceCollection services) {
services.AddOptions();
services.Configure<SectionZeroOptions>(Configuration.GetSection("section0"));
...
}
public class MyService : IMyService {
private IOptions<SectionZeroOptions> _SectionZeroOptions;
public MyService(IOptions<SectionZeroOptions> sectionZeroOptions) {
_SectionZeroOptions = sectionZeroOptions;
}
public string DoSomething(){
string key0 = _SectionZeroOptions.Value.key0;
string key1 = _SectionZeroOptions.Value.key1;
return key0;
}
}
Business Case using options pattern
You have an api that is responsible for approving loans. There are
business rules within the app that need to be applied, to decide
approval. One of those rules compares the loan amount being asked
for, to a max loan amount the business is willing to approve. The max
amount changes throughout the year based on other events.
Place the max approval amount (integer) in an external config and give
yourself the flexibility to change values at any moment, with no
downtime.
External Configuration Server
Custom configuration provider
About Config
Server
At boot time…
➔ Platform puts values in environment variables
➔ Client looks for server
➔ Client gets config by NAME
➔ Config organised by PROFILE
At runtime...
➔ Config can be refreshed live
➔ Call /actuator/refresh
➔ Values get reloaded
➔ No need for restart!
Environment: Stage Environment: ProdEnvironment: Local
Network
Services: DB, Cache, SMB
Team Foundation Server (CI/CD): Git, Build, Release
Network
Services: DB, Cache, SMB
Network
Services: DB, Cache, SMB
Config Server: development.yml, staging.yml, production.yml
string connString = Environment.GetEnvironmentVariable("conn_string");
string configUri = Environment.GetEnvironmentVariable("config_uri");
Simplicity vs Flexibility
External configuration can be challenging in itself It offers so much
design options that things can get out of hand quickly. Use the domain
around you to decide how far to go.
Config First Approach
Don’t hold anything in your appsettings.json, only a pointer to the
config server. Everything is filled from there.
Steeltoe and Spring Config
Putting the pieces together
2. Set the application Name (in appsettings.json)...
Setup Configuration Client: .NET / Steeltoe / Spring Config
4. Include the custom config provider...
1: Add the dependencies...
(Steeltoe for Pivotal Spring Cloud Config Client)
3. Add your external configuration (in Git)...
"spring": {
"application": { "name": "loan-application-service" },
"cloud": {
"config": {
"uri": "http://10.0.7.254:8888",
"validate_certificates": false,
"fail_fast": true
...
<PackageReference
Include="Steeltoe.Extensions.Configuration.ConfigServerCore" ... />
public void ConfigureServices(IServiceCollection services){
services.AddOptions();
services.Configure<Services.SomeValuesOptions>
(Configuration.GetSection("SomeValues"));
services.AddMvc();
...
}
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>{
ILoggerFactory fac = GetLoggerFactory(hostingContext.HostingEnvironment);
var env = hostingContext.HostingEnvironment;
config.AddConfigServer(env, fac);
})
...
public class SomeValuesOptions {
public string scheme { get; set; }
public string address { get; set; }
}
program.cs
startup.cs
config options pattern
Logging:
IncludeScopes: false
Debug:
LogLevel:
Default: Information
SomeValues:
scheme: https
address: www.pivotal.io
5. Retrieve values and map to class...
<PackageReference
Include="Steeltoe.Extensions.Configuration.ConfigServerCore" ... />
"spring": {
"application": { "name": "loan-application-service" },
"cloud": {
"config": {
"uri": "http://127.0.0.1:8888",
"validate_certificates": false,
"fail_fast": true
...
Logging:
IncludeScopes: false
Debug:
LogLevel:
Default: Information
SomeValues:
scheme: https
address: www.pivotal.io
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>{
ILoggerFactory fac = GetLoggerFactory(hostingContext.HostingEnvironment);
var env = hostingContext.HostingEnvironment;
config.AddConfigServer(env, fac);
})
...
public void ConfigureServices(IServiceCollection services){
services.AddOptions();
services.Configure<Services.SomeValuesOptions>
(Configuration.GetSection("SomeValues"));
services.AddMvc();
...
}
public class SomeValuesOptions {
public string scheme { get; set; }
public string address { get; set; }
}
"name": "loan-application-service"
config.AddConfigServer(env, fac);
"uri": "http://127.0.0.1:8888"
© Copyright 2019 Pivotal Software, Inc. All rights Reserved.
Learn More.
Become a cloud-native .NET rock star
https://springoneplatform.io/2019/training
Pivotal Platform Acceleration Lab for Developers (.NET)
Experience best practices building cloud-native applications in
a hands-on setting with Pivotal educators and consultants. The
program is focused on enabling customers and partners
through “doing it” rather than “talking about it”.
https://pivotal.io/platform-acceleration-lab
© Copyright 2019 Pivotal Software, Inc. All rights Reserved.
Questions?
Thank you
Refreshing
Values
- Optionally provide an environment specific
json config
Startup.cs
public static IWebHost BuildWebHost(string[] args) {
var builder = new WebHostBuilder()
...
.ConfigureAppConfiguration((hostingContext, config) => {
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json",
reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json",
optional: true, reloadOnChange: true);
config.AddEnvironmentVariables();
config.AddCommandLine(args);
})
...
Optional
providers
- Optionally provide an environment specific
json config
Startup.cs
public static IWebHost BuildWebHost(string[] args) {
var builder = new WebHostBuilder()
...
.ConfigureAppConfiguration((hostingContext, config) => {
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json",
optional: true);
config.AddEnvironmentVariables();
config.AddCommandLine(args);
})
...

More Related Content

What's hot (19)

PDF
Developing For The Windows Azure Platform
drmarcustillett
 
PPTX
CloudStack Meetup London - Primary Storage Presentation by SolidFire
NetApp
 
PDF
BeJUG Meetup - What's coming in the OSGi R7 Specification
Stijn Van Den Enden
 
PPTX
Play + scala + reactive mongo
Max Kremer
 
PDF
Couchbase Performance Benchmarking
Renat Khasanshyn
 
PPTX
Kåre Rude Andersen - Be a hero – optimize scom and present your services
Nordic Infrastructure Conference
 
PDF
Practicing Continuous Deployment
zeeg
 
PPTX
Securing MongoDB to Serve an AWS-Based, Multi-Tenant, Security-Fanatic SaaS A...
MongoDB
 
PDF
High-Performance Hibernate Devoxx France 2016
Vlad Mihalcea
 
PPTX
Azure Table Storage: The Good, the Bad, the Ugly (15 min. lightning talk)
Sirar Salih
 
PPTX
CUBRID Inside - Architecture, Source & Management Components
CUBRID
 
PPTX
BASTA 2013: Custom OData Provider
Rainer Stropek
 
PPT
Multi-tenancy with Rails
Paul Gallagher
 
PDF
Language enhancements in cold fusion 11
ColdFusionConference
 
PPTX
Windows Azure HDInsight Service
Neil Mackenzie
 
PPTX
Growing in the Wild. The story by CUBRID Database Developers.
CUBRID
 
PDF
Keystone deep dive 1
Jsonr4
 
PPTX
Azure Data Storage
Ken Cenerelli
 
PDF
Cassandra 3.0 Data Modeling
DataStax Academy
 
Developing For The Windows Azure Platform
drmarcustillett
 
CloudStack Meetup London - Primary Storage Presentation by SolidFire
NetApp
 
BeJUG Meetup - What's coming in the OSGi R7 Specification
Stijn Van Den Enden
 
Play + scala + reactive mongo
Max Kremer
 
Couchbase Performance Benchmarking
Renat Khasanshyn
 
Kåre Rude Andersen - Be a hero – optimize scom and present your services
Nordic Infrastructure Conference
 
Practicing Continuous Deployment
zeeg
 
Securing MongoDB to Serve an AWS-Based, Multi-Tenant, Security-Fanatic SaaS A...
MongoDB
 
High-Performance Hibernate Devoxx France 2016
Vlad Mihalcea
 
Azure Table Storage: The Good, the Bad, the Ugly (15 min. lightning talk)
Sirar Salih
 
CUBRID Inside - Architecture, Source & Management Components
CUBRID
 
BASTA 2013: Custom OData Provider
Rainer Stropek
 
Multi-tenancy with Rails
Paul Gallagher
 
Language enhancements in cold fusion 11
ColdFusionConference
 
Windows Azure HDInsight Service
Neil Mackenzie
 
Growing in the Wild. The story by CUBRID Database Developers.
CUBRID
 
Keystone deep dive 1
Jsonr4
 
Azure Data Storage
Ken Cenerelli
 
Cassandra 3.0 Data Modeling
DataStax Academy
 

Similar to Learn Cloud-Native .NET: Core Configuration Fundamentals with Steeltoe (20)

PPTX
Go (con)figure - Making sense of .NET configuration
Alex Thissen
 
PDF
.NET Core, ASP.NET Core Course, Session 8
Amin Mesbahi
 
PDF
Pathway to Cloud-Native .NET
VMware Tanzu
 
PDF
Steeltoe: Develop .NET Microservices Without Cloud Platform Lock-In
VMware Tanzu
 
PPTX
Academy PRO: ASP .NET Core
Binary Studio
 
PPTX
Quick Interview Preparation Dot Net Core
Karmanjay Verma
 
PPTX
.NET Core: a new .NET Platform
Alex Thissen
 
PDF
Building .NET Microservices
VMware Tanzu
 
PDF
Azure App configuration
Muhammad Sajid
 
PDF
.NET Core, ASP.NET Core Course, Session 6
Amin Mesbahi
 
PDF
Bringing Microservices to .NET: Modernizing Windows Applications as Cloud-Native
VMware Tanzu
 
PDF
.NET Fest 2019. Alex Thissen. Architecting .NET solutions in a Docker ecosystem
NETFest
 
PPTX
ASP.NET Core 1.0
Ido Flatow
 
PDF
From Zero to Cloud in 12 Easy Factors
Ed King
 
PPTX
Steeltoe and the Open Source .NET Renaissance
VMware Tanzu
 
PDF
ASP.NET Core Interview Questions PDF By ScholarHat.pdf
Scholarhat
 
PDF
.NET Cloud-Native Bootcamp- Los Angeles
VMware Tanzu
 
PPTX
Introducing ASP.NET Core 2.0
Steven Smith
 
PPTX
Architecting .NET solutions in a Docker ecosystem - .NET Fest Kyiv 2019
Alex Thissen
 
PDF
.NET Cloud-Native Bootcamp Minneapolis
VMware Tanzu
 
Go (con)figure - Making sense of .NET configuration
Alex Thissen
 
.NET Core, ASP.NET Core Course, Session 8
Amin Mesbahi
 
Pathway to Cloud-Native .NET
VMware Tanzu
 
Steeltoe: Develop .NET Microservices Without Cloud Platform Lock-In
VMware Tanzu
 
Academy PRO: ASP .NET Core
Binary Studio
 
Quick Interview Preparation Dot Net Core
Karmanjay Verma
 
.NET Core: a new .NET Platform
Alex Thissen
 
Building .NET Microservices
VMware Tanzu
 
Azure App configuration
Muhammad Sajid
 
.NET Core, ASP.NET Core Course, Session 6
Amin Mesbahi
 
Bringing Microservices to .NET: Modernizing Windows Applications as Cloud-Native
VMware Tanzu
 
.NET Fest 2019. Alex Thissen. Architecting .NET solutions in a Docker ecosystem
NETFest
 
ASP.NET Core 1.0
Ido Flatow
 
From Zero to Cloud in 12 Easy Factors
Ed King
 
Steeltoe and the Open Source .NET Renaissance
VMware Tanzu
 
ASP.NET Core Interview Questions PDF By ScholarHat.pdf
Scholarhat
 
.NET Cloud-Native Bootcamp- Los Angeles
VMware Tanzu
 
Introducing ASP.NET Core 2.0
Steven Smith
 
Architecting .NET solutions in a Docker ecosystem - .NET Fest Kyiv 2019
Alex Thissen
 
.NET Cloud-Native Bootcamp Minneapolis
VMware Tanzu
 
Ad

More from VMware Tanzu (20)

PDF
Spring into AI presented by Dan Vega 5/14
VMware Tanzu
 
PDF
What AI Means For Your Product Strategy And What To Do About It
VMware Tanzu
 
PDF
Make the Right Thing the Obvious Thing at Cardinal Health 2023
VMware Tanzu
 
PPTX
Enhancing DevEx and Simplifying Operations at Scale
VMware Tanzu
 
PDF
Spring Update | July 2023
VMware Tanzu
 
PPTX
Platforms, Platform Engineering, & Platform as a Product
VMware Tanzu
 
PPTX
Building Cloud Ready Apps
VMware Tanzu
 
PDF
Spring Boot 3 And Beyond
VMware Tanzu
 
PDF
Spring Cloud Gateway - SpringOne Tour 2023 Charles Schwab.pdf
VMware Tanzu
 
PDF
Simplify and Scale Enterprise Apps in the Cloud | Boston 2023
VMware Tanzu
 
PDF
Simplify and Scale Enterprise Apps in the Cloud | Seattle 2023
VMware Tanzu
 
PPTX
tanzu_developer_connect.pptx
VMware Tanzu
 
PDF
Tanzu Virtual Developer Connect Workshop - French
VMware Tanzu
 
PDF
Tanzu Developer Connect Workshop - English
VMware Tanzu
 
PDF
Virtual Developer Connect Workshop - English
VMware Tanzu
 
PDF
Tanzu Developer Connect - French
VMware Tanzu
 
PDF
Simplify and Scale Enterprise Apps in the Cloud | Dallas 2023
VMware Tanzu
 
PDF
SpringOne Tour: Deliver 15-Factor Applications on Kubernetes with Spring Boot
VMware Tanzu
 
PDF
SpringOne Tour: The Influential Software Engineer
VMware Tanzu
 
PDF
SpringOne Tour: Domain-Driven Design: Theory vs Practice
VMware Tanzu
 
Spring into AI presented by Dan Vega 5/14
VMware Tanzu
 
What AI Means For Your Product Strategy And What To Do About It
VMware Tanzu
 
Make the Right Thing the Obvious Thing at Cardinal Health 2023
VMware Tanzu
 
Enhancing DevEx and Simplifying Operations at Scale
VMware Tanzu
 
Spring Update | July 2023
VMware Tanzu
 
Platforms, Platform Engineering, & Platform as a Product
VMware Tanzu
 
Building Cloud Ready Apps
VMware Tanzu
 
Spring Boot 3 And Beyond
VMware Tanzu
 
Spring Cloud Gateway - SpringOne Tour 2023 Charles Schwab.pdf
VMware Tanzu
 
Simplify and Scale Enterprise Apps in the Cloud | Boston 2023
VMware Tanzu
 
Simplify and Scale Enterprise Apps in the Cloud | Seattle 2023
VMware Tanzu
 
tanzu_developer_connect.pptx
VMware Tanzu
 
Tanzu Virtual Developer Connect Workshop - French
VMware Tanzu
 
Tanzu Developer Connect Workshop - English
VMware Tanzu
 
Virtual Developer Connect Workshop - English
VMware Tanzu
 
Tanzu Developer Connect - French
VMware Tanzu
 
Simplify and Scale Enterprise Apps in the Cloud | Dallas 2023
VMware Tanzu
 
SpringOne Tour: Deliver 15-Factor Applications on Kubernetes with Spring Boot
VMware Tanzu
 
SpringOne Tour: The Influential Software Engineer
VMware Tanzu
 
SpringOne Tour: Domain-Driven Design: Theory vs Practice
VMware Tanzu
 
Ad

Recently uploaded (20)

PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 

Learn Cloud-Native .NET: Core Configuration Fundamentals with Steeltoe

  • 1. .NET Core: Configuration Fundamentals David Dieruf - Pivotal July, 2019
  • 2. Let's Talk About... Cloud-native environment and goals Why we are here Designing config and environment variables Different patterns About .NET Core configuration Building config providers in WebHost and its hierarchy of values The options pattern Extending basic config with objects External configuration servers How they work and why you choose them Putting the pieces together Using Steeltoe and Spring Config to reach cloud-native goals
  • 3. Environment: Local Network Services: DB, Cache, SMB Environment: Stage Environment: Prod Team Foundation Server (CI/CD): Git, Build, Release My-App Network Services: DB, Cache, SMB Network Services: DB, Cache, SMB My-App My-App - One build - Test once (for all environments) - Parity between environments - Automation My-App My-App public void ConfigureServices(IServiceCollection services) { String connString = Environment.GetEnvironmentVariable("conn_string"); services.AddDbContext<MyContext>(options => options.UseSqlServer(connString)); }
  • 4. Environment variables in the cloud How is the value set? Do you have to manually update the value per instance, or script the action? How easy is promotion? Do you have to recompile to move between environments? Does your build pipeline manipulate settings? Do you have a history of changes? Do you know when a value was set and by who? Atomic value in the cloud Per app instance App can not run without a value Agnostic to its platform or IaaS (portable)
  • 5. Appsettings.json ● [Good] External to the compiled application ● [Good] No recompilation needed ● [Challenge] Have to change at every instance Internal Class Command Line ● [Good] Secure information in compiled bits ● [Challenge] Hard coded values in app ● [Challenge] Can not change without recompiling ● [Good] External to the compiled application ● [Challenge] Have to change at every instance ● [Challenge] Lost values because no source check-in (Manual) Environment Variables Popular Configuration Design ● [Good] External to the compiled application ● [Good] Application compiled once and promoted through environment ● [Challenge] Have to change at every host class MyClass { string connString = "xxxx" } { "ConnectionStrings": { "a-database": "xxxxx" } } C: dotnet run my-app ` --conn "xxxx" ` --something "yyyyy" string connString = Environment.GetEnvironmentVari able("my-conn")
  • 7. Using Default WebHost Loading configuration into your app public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); - use Kestrel as the web server and configure it using the application's configuration providers - set the ContentRootPath to the result of GetCurrentDirectory() - load IConfiguration from 'appsettings.json' and 'appsettings.[EnvironmentName].json' - load IConfiguration from User Secrets when EnvironmentName is 'Development' using the entry assembly - load IConfiguration from environment variables - load IConfiguration from supplied command line args - configure the ILoggerFactory to log to the console and debug output - enable IIS integration program.cs WebHost.CreateDefaultBuilder(args)
  • 8. Build your own WebHost Loading configuration into your app public static IWebHost BuildWebHost(string[] args) { var builder = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureAppConfiguration((hostingContext, config) => { var env = hostingContext.HostingEnvironment; config.AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); config.AddEnvironmentVariables(); config.AddCommandLine(args); }) .UseIISIntegration() .UseStartup<Startup>(); return builder.Build(); } program.cs - Options manipulate how configuration values are overwritten - Options to [not] include other configuration providers, like custom external config server provider var builder = new WebHostBuilder()
  • 9. Config value hierarchy Overwrite values based on environment public static IWebHost BuildWebHost(string[] args) { var builder = new WebHostBuilder() ... .ConfigureAppConfiguration((hostingContext, config) => { var env = hostingContext.HostingEnvironment; config.AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{env.EnvironmentName}.json"); config.AddEnvironmentVariables(); config.AddCommandLine(args); }) ... 1. Load values from appsettings.json 2. (if provided) Load values from environment specific appsettings.{env}.json 3. (if present) Load values from environment variables 4. (if present) Load values provided when app was started config.AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{env.EnvironmentName}.json"); config.AddEnvironmentVariables(); config.AddCommandLine(args);
  • 10. { "my_val": "something" } Example Value Overwriting 1. config.AddJsonFile("appsettings.json") { "my_val": "another" } 2. .AddJsonFile("appsettings.staging.json") PS c: $env:my_val = "yet another" 3. config.AddEnvironmentVariables() PS c: dotnet run my-app --my_val "again another" 4. config.AddCommandLine(args) my_val == “again another”
  • 11. Configuration Providers Supported providers for configuration of ASP.NET core app Provider Provides configuration from… Azure Key Vault Configuration Provider (Security topics) Azure Key Vault Azure App Configuration Provider (Azure documentation) Azure App Configuration Command-line Configuration Provider Command-line parameters Custom configuration provider Custom source Environment Variables Configuration Provider Environment variables File Configuration Provider Files (INI, JSON, XML) Key-per-file Configuration Provider Directory files Memory Configuration Provider In-memory collections User secrets (Secret Manager) (Security topics) File in the user profile directory
  • 12. Consuming Values Using Microsoft.Extensions.Configuration public class ValuesController : Controller{ private readonly IConfiguration _config; public ValuesController(IConfiguration config){ _config = config; } [HttpGet] public ActionResult<string> Get(){ string val = _config.GetValue<string>("my_val"); return val; } } - No consideration for handler type(s) - Simple key:value store - Can be complex type ValuesController.cs
  • 14. Options Pattern Using Microsoft.Extensions.Configuration.Options { "section0": { "key0": "value", "key1": "value" }, "section1": { "key0": "value", "key1": "value" } } public void ConfigureServices(IServiceCollection services) { services.AddOptions(); services.Configure<SectionZeroOptions>(Configuration.GetSection("section0")); ... } public class SectionZeroOptions { public string key0 {get;set;} public string key1 { get; set; } } public class MyService : IMyService { private IOptions<SectionZeroOptions> _SectionZeroOptions; public MyService(IOptions<SectionZeroOptions> sectionZeroOptions) { _SectionZeroOptions = sectionZeroOptions; } public string DoSomething(){ string key0 = _SectionZeroOptions.Value.key0; string key1 = _SectionZeroOptions.Value.key1; return key0; } } 1. appsettings.json values 2. Matching class to json 3. Enable the Options configuration 4. Consume values { "section0": { "key0": "value", "key1": "value" }, "section1": { "key0": "value", "key1": "value" } } public class SectionZeroOptions { public string key0 {get;set;} public string key1 { get; set; } } public void ConfigureServices(IServiceCollection services) { services.AddOptions(); services.Configure<SectionZeroOptions>(Configuration.GetSection("section0")); ... } public class MyService : IMyService { private IOptions<SectionZeroOptions> _SectionZeroOptions; public MyService(IOptions<SectionZeroOptions> sectionZeroOptions) { _SectionZeroOptions = sectionZeroOptions; } public string DoSomething(){ string key0 = _SectionZeroOptions.Value.key0; string key1 = _SectionZeroOptions.Value.key1; return key0; } }
  • 15. Business Case using options pattern You have an api that is responsible for approving loans. There are business rules within the app that need to be applied, to decide approval. One of those rules compares the loan amount being asked for, to a max loan amount the business is willing to approve. The max amount changes throughout the year based on other events. Place the max approval amount (integer) in an external config and give yourself the flexibility to change values at any moment, with no downtime.
  • 16. External Configuration Server Custom configuration provider
  • 17. About Config Server At boot time… ➔ Platform puts values in environment variables ➔ Client looks for server ➔ Client gets config by NAME ➔ Config organised by PROFILE At runtime... ➔ Config can be refreshed live ➔ Call /actuator/refresh ➔ Values get reloaded ➔ No need for restart!
  • 18. Environment: Stage Environment: ProdEnvironment: Local Network Services: DB, Cache, SMB Team Foundation Server (CI/CD): Git, Build, Release Network Services: DB, Cache, SMB Network Services: DB, Cache, SMB Config Server: development.yml, staging.yml, production.yml string connString = Environment.GetEnvironmentVariable("conn_string"); string configUri = Environment.GetEnvironmentVariable("config_uri");
  • 19. Simplicity vs Flexibility External configuration can be challenging in itself It offers so much design options that things can get out of hand quickly. Use the domain around you to decide how far to go. Config First Approach Don’t hold anything in your appsettings.json, only a pointer to the config server. Everything is filled from there.
  • 20. Steeltoe and Spring Config Putting the pieces together
  • 21. 2. Set the application Name (in appsettings.json)... Setup Configuration Client: .NET / Steeltoe / Spring Config 4. Include the custom config provider... 1: Add the dependencies... (Steeltoe for Pivotal Spring Cloud Config Client) 3. Add your external configuration (in Git)... "spring": { "application": { "name": "loan-application-service" }, "cloud": { "config": { "uri": "http://10.0.7.254:8888", "validate_certificates": false, "fail_fast": true ... <PackageReference Include="Steeltoe.Extensions.Configuration.ConfigServerCore" ... /> public void ConfigureServices(IServiceCollection services){ services.AddOptions(); services.Configure<Services.SomeValuesOptions> (Configuration.GetSection("SomeValues")); services.AddMvc(); ... } WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, config) =>{ ILoggerFactory fac = GetLoggerFactory(hostingContext.HostingEnvironment); var env = hostingContext.HostingEnvironment; config.AddConfigServer(env, fac); }) ... public class SomeValuesOptions { public string scheme { get; set; } public string address { get; set; } } program.cs startup.cs config options pattern Logging: IncludeScopes: false Debug: LogLevel: Default: Information SomeValues: scheme: https address: www.pivotal.io 5. Retrieve values and map to class... <PackageReference Include="Steeltoe.Extensions.Configuration.ConfigServerCore" ... /> "spring": { "application": { "name": "loan-application-service" }, "cloud": { "config": { "uri": "http://127.0.0.1:8888", "validate_certificates": false, "fail_fast": true ... Logging: IncludeScopes: false Debug: LogLevel: Default: Information SomeValues: scheme: https address: www.pivotal.io WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, config) =>{ ILoggerFactory fac = GetLoggerFactory(hostingContext.HostingEnvironment); var env = hostingContext.HostingEnvironment; config.AddConfigServer(env, fac); }) ... public void ConfigureServices(IServiceCollection services){ services.AddOptions(); services.Configure<Services.SomeValuesOptions> (Configuration.GetSection("SomeValues")); services.AddMvc(); ... } public class SomeValuesOptions { public string scheme { get; set; } public string address { get; set; } } "name": "loan-application-service" config.AddConfigServer(env, fac); "uri": "http://127.0.0.1:8888"
  • 22. © Copyright 2019 Pivotal Software, Inc. All rights Reserved. Learn More. Become a cloud-native .NET rock star https://springoneplatform.io/2019/training Pivotal Platform Acceleration Lab for Developers (.NET) Experience best practices building cloud-native applications in a hands-on setting with Pivotal educators and consultants. The program is focused on enabling customers and partners through “doing it” rather than “talking about it”. https://pivotal.io/platform-acceleration-lab
  • 23. © Copyright 2019 Pivotal Software, Inc. All rights Reserved. Questions? Thank you
  • 24. Refreshing Values - Optionally provide an environment specific json config Startup.cs public static IWebHost BuildWebHost(string[] args) { var builder = new WebHostBuilder() ... .ConfigureAppConfiguration((hostingContext, config) => { var env = hostingContext.HostingEnvironment; config.AddJsonFile("appsettings.json", reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); config.AddEnvironmentVariables(); config.AddCommandLine(args); }) ...
  • 25. Optional providers - Optionally provide an environment specific json config Startup.cs public static IWebHost BuildWebHost(string[] args) { var builder = new WebHostBuilder() ... .ConfigureAppConfiguration((hostingContext, config) => { var env = hostingContext.HostingEnvironment; config.AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); config.AddEnvironmentVariables(); config.AddCommandLine(args); }) ...