Store configuration settings of .NET Core APIs in the environment.

Srikar Gandhi
3 min readMay 11, 2020

I would like to share how to externalize the configuration settings in ASP.NET Core APIs on the Pivotal Cloud Foundry (PAAS platform) using the Spring Cloud Config Server.

What do you mean by externalizing the application’s settings?

An app’s config is everything that is likely to vary between deploys (staging, production, developer environments, etc). This includes:

  • Resource handles to the database, Memcached, and other backing services
  • Credentials to external services such as Amazon S3 or Twitter
  • Per-deploy values such as the canonical hostname for the deploy

Apps sometimes store config as constants in the code. This is a violation of twelve-factor, which requires strict separation of config from code. Config varies substantially across deploys, code does not.

You can configure the config server in your application in 4 simple steps.

Step 1

You need to spin up Spring Cloud Config Server service as follows. You can refer to Spring Cloud documentation for additional details.

cf create-service -c C:\<filepath>\cloudConfig.json  p.config-server standard config1you can get the config file refferred in the above command here.
Note: If your git repo need authentication then use this file in above command.

Step 2

Upload the app settings file in the same git Repository that you specified in the cloudConfig.json file. This file will be referred in the API solution as shown below.

The application setting should be in YAML format. There are three different files appsettings-values-Dev.yml, appsettings-values-Test.yml, and appsettings-values-Prod.yml for Development, Test and Production environments respectively.

Step 3

Create .NET core API and refer below Nuget packages in the solution.

Steeltoe.Extensions.Configuration.CloudFoundryCore, Steeltoe.Extensions.Configuration.ConfigServerCore and Steeltoe.Management.CloudFoundryCore

Add the below code in the program.cs file to configure the Configu server and load appsettings.json as per the environment variable value.

Configure Cloud Foundry actuators in the request pipeline as in the startup.cs shown below. The actuators endpoints expose the information related to an application like Info, health, and metrics, etc. (Refer link for additional details.)

Create applicationsettings.json file for each environment and refer the appsettings-values-Dev.yml according to the environment.

Finally, configure the spring config server settings as shown below. That’s it you are done with the configuration!

Step 4

Push the application on the Pivotal cloud foundry and bind the configure server created in the 1st step to the application pushed in step 4. As shown below.

At last, you need to set the environment available so that an appropriate config file is picked up.

Conclusion

You can promote the application in the Dev environment to Test and production by just changing the Environment variable value. The Application settings always reside in the GIT repo & managed by the Config server.

You can get the complete code here. Hopefully, It helps you!

Happy Coding!

--

--