Adding AppSettings.json Configuration to a .NET Core Console Application

This post will walk you through the steps necessary to add a strongly typed appsettings.json configuration file to your console application. I’ll dispense with the formalities of writing a detailed introduction on why you would be interested in doing this and jump right into the steps involved.

Note: This post assumes .NET Core 2.1, but I believe any 2.x version will work.

1. Add the following NuGet packages to your console application:

Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Binder
Microsoft.Extensions.Configuration.EnvironmentVariables
Microsoft.Extensions.Configuration.FileExtensions
Microsoft.Extensions.Configuration.Json
Microsoft.Extensions.Configuration.UserSecrets

2. Right-click your console application, and select “Unload project“. Once the project has been unloaded, right click the project and select “Edit <your_console_project_name>.csproj“.

3. Add the following just below the <TargetFramework> element:
<UserSecretsId>93d033dc-12eb-446a-91a6-efec88dfb437</UserSecretsId>
(note: you should replace this guid with your own)

4. Add the following to a <ItemGroup> with other CliToolReference elements, or add your own:

<ItemGroup>
  <DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
</ItemGroup>

5. Right-click your console application and select the “Reload project” option.

6. Open the developer command prompt and navigate to the directory with your console application, then run:

dotnet user-secrets set MySecret "MySecretValue"

Note: This will create a secrets.json file located at: %APPDATA%\Microsoft\UserSecrets\<SecretId_From_Step_3>\secrets.json
Edit the newly created file and change the contents to:

{
   "MySettings": {
     "ApiSecret":  "21u021u401u412u4102u410"
   }
}

7. Create a new JSON configuration file for your console application and name it appsettings.json. Set the following to the contents of the file to the JSON below. Important: Be sure to right-click this file and select properties. Change the “Copy to Output Directory” option to “Copy if newer”.

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=MyApplicationDatabase;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "MySettings": {
    "AccountName": "myaccount",
    "ApiKey": "3ut3902ut32ut9238uy932u902421414141"
  }
}

8. Create a class named MySettingsConfig:

public class MySettingsConfig
{
  public string AccountName { get; set; }
  public string ApiKey { get; set; }
  public string ApiSecret { get; set; }
}

9. Add the following code to your Main routine inside Program.cs

var builder = new ConfigurationBuilder()
               .SetBasePath(Directory.GetCurrentDirectory())
               .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
               .AddUserSecrets()
               .AddEnvironmentVariables(); 

IConfigurationRoot configuration = builder.Build();
var mySettingsConfig = new MySettingsConfig();
configuration.GetSection("MySettings").Bind(mySettingsConfig);

Console.WriteLine("Setting from appsettings.json: " + mySettingsConfig.AccountName);
Console.WriteLine("Setting from secrets.json: " + mySettingsConfig.ApiSecret);
Console.WriteLine("Connection string: " + configuration.GetConnectionString("DefaultConnection"));

Console.ReadKey();

If your console shows your settings correctly, congratulations! If you’re missing the AccountName and ConnectionString settings, be sure you followed the step to “Copy to Output Directory” listed in step 7.

 

Kyle Ballard