[英]asp.net core Logging not following the ConfigurationBuilder hierarchy
[英]Using ASP.NET Core's ConfigurationBuilder in a Test Project
我想在我的功能测试项目中使用IHostingEnvironment
和ConfigurationBuilder
,以便根据环境使用不同的配置集运行功能测试。 我想使用下面的代码:
public IConfigurationRoot ConfigureConfiguration(IHostingEnvironment hostingEnvironment)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", true)
.AddEnvironmentVariables();
return builder.Build();
}
我想要一个appSettings.json
和appSettings.Production.json
文件来将我的功能测试指向生产。 这可能吗? 如何实现? 我需要一个IHostingEnvironment
实例。
您可以通过几个步骤在测试项目中使用ConfigurationBuilder
。 我认为您不需要IHostingEnvironment
接口本身。
首先,将两个 NuGet 包添加到具有ConfigurationBuilder
扩展方法的项目中:
"dependencies": {
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final"
}
其次,将所需的环境变量放入测试项目的属性中:
然后你可以在测试项目中创建你自己的构建器:
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();
}
如果您想使用完全相同的设置文件(而不是副本),那么您需要添加一个路径。
为了完整起见,我将在此处添加此答案,因为我遇到了与 @vaindil 在 Will's answer here 中描述的问题相同的问题。 原因是我们从被测代码中的环境变量填充了我们的 IConfiguration。 这覆盖了我们在测试中使用 appsettings.json 设置的任何配置。 我们的解决方案是使用System.Environment.SetEnvironvironmentVariable("variableName", "variableValue")
为测试过程创建环境变量
实际上,我们测试中的 WebHostBuilder 实例的创建方式与我们托管的 API 相同:
// Code omitted for brevity
var builder = new WebHostBuilder()
.UseEnvironment("Development")
.ConfigureAppConfiguration(configurationBuilder => configurationBuilder.AddEnvironmentVariables())
.UseStartup<Startup>();
var testServer = new TestServer(builder); // test against this
这是可能的,根据 ASP.NET Core 文档 ( http://docs.asp.net/en/latest/fundamentals/configuration.html ),您可以在 Startup 方法中构建它并添加环境变量 appSettings。 在指定可选标志时,您可以将 appSettings.Production.json 用于生产环境,并在没有 appSettings.Development.json 可用时使用 appSettings.json 进行开发。 appsettings.{env.EnvironmentName}.json 仅在文件存在且后备为默认 appsettings.json 时使用
public Startup(IHostingEnvironment env)
{
// Set up configuration providers.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
我在我的测试项目的根目录中添加了一个appsettings.Development.json
。 然后我通过 .ConfigureAppConfiguration .ConfigureAppConfiguration()
方法添加了 appsettings 文件。 然后它将调用主应用程序的Startup()
方法,但使用appsettings.Development.json
的配置设置。
string Environment = "Development";
var server = new TestServer(new WebHostBuilder()
.UseEnvironment(Environment)
.ConfigureAppConfiguration(x => {
x.SetBasePath(Directory.GetCurrentDirectory());
x.AddJsonFile($"appsettings.{Environment}.json", optional: false, reloadOnChange: true);
x.AddEnvironmentVariables();
})
.UseStartup<Startup>()
);
httpClient = server.CreateClient();
然后可以使用 httpClient 来发出 Web 请求,例如:
var response = await httpClient.GetAsync(url);
var responseString = await response.Content.ReadAsStringAsync();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.