My Web application needs to read the Document DB keys from appsettings.json file. I have created a class with the key names and reading the Config section in ConfigureServices()
as:
public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables(); Configuration = builder.Build(); } public IConfigurationRoot Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver()); services.AddSession(); Helpers.GetConfigurationSettings(services, Configuration); DIBuilder.AddDependency(services, Configuration); }
I'm looking for the ways to read the Key values in Test project.
AddJsonFile($"appsettings. json", true, true); var config = builder. Build(); var connectionString = config["ConnectionString"]; var emailHost = config["Smtp:Host"]; Console. WriteLine($"Connection String is: {connectionString}"); Console.
Net Core 2.0. The IConfiguration interface need to be injected as dependency in the Controller and then later used throughout the Controller. The IConfiguration interface is used to read Settings and Connection Strings from AppSettings. json file.
This is based on the blog post Using Configuration files in .NET Core Unit Test Projects (written for .NET Core 1.0).
Create (or copy) the appsettings.test.json in the Integration test project root directory, and in properties specify "Build Action" as Content and "Copy if newer" to Output Directory. Note that it’s better to have file name (e.g. appsettings.test.json
) different from normal appsettings.json
, because it is possible that a file from the main project override the file from the test project, if the same name will be used.
Include the JSON Configuration file NuGet package (Microsoft.Extensions.Configuration.Json) if it's not included yet.
In the test project create a method,
public static IConfiguration InitConfiguration() { var config = new ConfigurationBuilder() .AddJsonFile("appsettings.test.json") .AddEnvironmentVariables() .Build(); return config; }
AddEnvironmentVariables (suggested in @RickStrahl blog ) is useful if you want to pass some secrets, that you prefer not store in appsettings.test.json
Use the configuration as usual
var config = InitConfiguration(); var clientId = config["CLIENT_ID"]
BTW: You also may be interesting in reading the configuration into the IOptions class as described in Integration test with IOptions<> in .NET Core:
var options = config.Get<MySettings>();
Add the configuration file
First, add a appconfig.json file to the Integration test project
Configure the appconfig.json file to be copied to the output directory by updating
Add NuGet package
Use the configuration in your unit tests
[TestClass] public class IntegrationTests { public IntegrationTests() { var config = new ConfigurationBuilder().AddJsonFile("appconfig.json").Build(); _numberOfPumps = Convert.ToInt32(config["NumberOfPumps"]); _numberOfMessages = Convert.ToInt32(config["NumberOfMessages"]); _databaseUrl = config["DatabaseUrlAddress"]; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With