Skip to content

Accessing the appsettings.json file

Jon P Smith edited this page Nov 8, 2021 · 1 revision

It is sometimes useful to have somewhere you can put variable data for use in your unit tests. EfCore.TestSupport has a small static class called AppSettings which mainly contains connection strings, but you can use it for other data as well. It works in the same way as the ASP.NET Core appsettings.json file (EfCore.TestSupport uses the same code).

NOTE: the appsettings.json file is optional, but the Creating connections strings feature relies on it.

The AppSettings.GetConfiguration() methods

The method AppSettings.GetConfiguration() will get the configuration file using the ASP.NET Core code. You can place any setting for your unit tests. There are two variants:

  1. AppSettings.GetConfiguration(string settingsFilename = "appsettings.json")
    This loads the configuration from the appsettings.json file in the unit test project. It doesn't fail if there is no file there (that is on purpose, because CompareEfSql calls it to see if there is a connection). This version allows you to define a different named .json file via the settingsFilename: parameter. For instance the code below would get a database connection string in the appsetting.json file in the project that the code is run in (typically your unit test project).
var config = AppSettings.GetConfiguration();
var connectionString = config .GetConnectionString("DefaultConnection");
  1. AppSettings.GetConfiguration(string relativeToCallingAssembly, string settingsFilename = "appsettings.json")
    This allows you to access a json configuration file in another directory. The base directory is the calling's assembly top level directory, e.g. "Test". You can add relative directory reference, e.g. "../SomeOtherProject/". For instance the code below would get the database connection string from the appsettings.json file in the project MyAspNetCoreApp.
var mainConfig = AppSettings.GetConfiguration("../MyAspNetCoreApp/");
var connectionString = mainConfig.GetConnectionString("DefaultConnection");

Click here for an example of the appsettings.json file.
Click here for examples of the different usages.

Reading connection strings

If you just want to read a connection string from the appsettings.json file then you can use the GetConnectionString method, with the name of the connection string you want as the other parameter.

var config = AppSettings.GetConfiguration();
var connectionString = config .GetConnectionString("DefaultConnection");

Reading other information

Just like ASP.NET Core you can any data from the appsettings.json file. Here is an example json file

{
  "ConnectionStrings": {
    "UnitTestConnection": "Server=(localdb)\\mssqllocaldb;Database=EfCore.TestSupport-Test;Trusted_Connection=True;MultipleActiveResultSets=true",
  },
  "MyInt": 1,
  "MyObject": {
    "MyInnerInt":  2
  } 
}

And here is how to read the non-connection string values

var config = AppSettings.GetConfiguration();

var myInt = config["MyInt"];  //value returned is "1"
var myInnerInt = config["MyObject:MyInnerInt"]; //value returns is "2"
Clone this wiki locally