You can use ConfigurationBuilder in a test project with a few steps. I do not think that you will need the IHostingEnvironment interface IHostingEnvironment .
First add two NuGet packages to the project that have ConfigurationBuilder extension methods:
"dependencies": { "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc1-final", "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final" }
Secondly, put the necessary environment variables in the properties of the test project:

Then you can create your own builder in a test project:
private readonly IConfigurationRoot _configuration; public BuildConfig() { var environmentName = Environment.GetEnvironmentVariable("Hosting:Environment"); var config = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .AddJsonFile($"appsettings.{environmentName}.json", true) .AddEnvironmentVariables(); _configuration = config.Build(); }
If you want to use exactly the same settings file (not a copy), then you will need to add a path to it.
source share