Application configuration
ASP.NET Core C# Dependency Injection Patterns Visual Studio

Using Application Settings in a .NET Core Application

Hi and welcome back to my blogs on development!

Today I will go over how you can efficiently use application settings within your .NET Core applications using some useful dependency injection patterns built into .NET Core 2.x.

The file used to store configuration settings for our .NET Core web application is the appsettings.json file. A sample format of this file is shown as follows:

{
  "ConnectionStrings": {
    "AppDbContext": "[some connection string]"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AppSettings": {
    "AppName": "BookLoan",
    "AppVersion": "1.0.0",
    "AlertEmail": "[email protected]"
  }
}

To obtain a connection string, we can use the following command:

string dbConnStr = Configuration.GetConnectionString("AppDbContext")

(where Configuration is injected through a constructor, e.g. from Startup():

public IConfiguration Configuration { get; }

public Startup(IConfiguration configuration)
{
   Configuration = configuration;
}

This is quite straightforward, and quite similar to traditional .NET 4.x Framework applications.

How about the key / value pairs in the AppSettings section?

The following will also work:

string sAppName = Configuration.GetValue<string>("AppSettings:AppName");
string sAppVersion = Configuration.GetValue<string>("AppSettings:AppVersion");

If we had many settings key / values to retrieve into our service classes, the above would be quite cumbersome and repeat itself. The solution to this is to strongly type the app settings section into a class and take advantage of dependency injection built into .NET Core. 

These are the steps to take to achieve this:

Step 1 – Create a class to containing your application settings

using System;

namespace BookLoan.Services
{
    public class AppConfiguration
    {
        public AppConfiguration() { } 
        public string AppName { get; set; } 
        public string AppVersion { get; set; } 
    }
}

Step 2 – Update ConfigureServices()

In your ConfigureServices() method within startup.cs add the following two lines:

public void ConfigureServices(IServiceCollection services)
{
…           services.Configure<AppConfiguration>(Configuration.GetSection("AppSettings"));
		…
}

Include also the Microsoft.Extensions.Configuration namespace at the top of the source.

The following command:

services.Configure<AppConfiguration>(Configuration.GetSection("AppSettings"));

allows your options to be bound to the respective configuration key from appsettings.json.

[See also here for more detailed reference.]

Step 3 – Access the settings from your class

Reference the configuration class in your service class and obtain settings properties:

namespace BookLoan.Data
{
    public class SeedAccounts
    {
        private ApplicationDbContext db;
        private AppConfiguration appConfiguration;

        public SeedAccounts(ApplicationDbContext _db, AppConfiguration _appConfiguration)
        {
            db = _db;
            appConfiguration = _appConfiguration;
        }

        public async Task ConfigureApplicationFeatures()
        {
	    string appversion = appConfiguration.ApplicationVersion;
		..
	}
	..
}

That’s all!

I hope you enjoyed this post and find it useful.

Social media & sharing icons powered by UltimatelySocial