ASP.NET Core AppSettings & Azure

ASP.NET Core had its configuration system re-architected from being limited to reading from XML files to reading configuration settings from any key/value based settings files such as JSON, INI and XML.

In this article we will explore the minimum amount of work required to read these settings out in your ASP.NET Core application. This includes taking a look at how it behaves when deployed in Azure.

The Startup Class

The extract from ASP.NET Core docs:

The Startup class configures the request pipeline that handles all requests made to the application.

ASP.NET Core apps require the Startup class to initialise the application. The handy thing about the Startup classes is that you can have one for each environment if you wish you. It has Configure and ConfigureServices methods that are called when the application starts. The run-time calls ConfigureServices prior to calling Configure method.

The ConfigureServices method is where you will inform the APS.NET Core which application features you want to configure for use. This is the place where we setup dependency injection, CORS policies,  MVC options, API versioning, Swagger and how we will load the the Application Settings.

The Configure method is where will specify which features we actually want to use and how they will interact with the request pipeline. For example, here we will configure environment related settings, such as using BrowserLink in development environment, to the logging and how we want to MVC to behave in the request pipeline.

The Setup

NET_CORE_AppSettings_Setup.png

When you create a sample ASP.NET Core application, the Startup class constructor will already contain the above code to read the configuration settings from the appsettings.json file. Notice that it stores the built Configuration object into the Configuration property, so that it is accessible to other methods inside the Startup class.

Now that we have loaded the Configuration object, lets find out how we can inform APS.NET Core that we want to use this data in our request pipeline.

The Load

The snippet below is all we require to inform ASP.NET Core that we want to use the Options Pattern to retrieve the AppSettings section the appsettings.json file and store it into a simple POCO called AppSettings.

NET_CORE_AppSettings_Configure

The Load – In Azure

If we were to run the above setup in Azure you will find that it will map to nothing. It seems when the application is loaded in Azure, it doesn’t receive a Configuration object that contains multiple sections like AppSettings and Logging, instead it gets a single Configuration object with all the Key/Value pairs in the root. What this means to us is, that we need the below snippet to load the values depending on the environment it is running on.

Hopefully someone can explain to me why Azure behaves so differently.

NET_CORE_AppSettings_Configure_Azure.png

Using inside the Startup class

If you need to access any of these configuration settings in the Configure method, for example you want to switch your CORS Policy based on the environment the application running on then the following snippet is how you can get access to your AppSettings class that you configured earlier.

NET_CORE_AppSettings_Usage_Startup.png

Using in your application

Very similar to how we use it inside the Configure method, but a bit simpler as your Dependency Injection framework will take care of the resolving for you.

NET_CORE_AppSettings_Usage_App.png

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s