我有一个使用 ASP.NET Core 的测试项目,并尝试读取应用程序设置值。
使用以下配置在测试项目中创建了 appsetting.test.json 文件
{
"baseUrl": "https://xxxxx.io/",
"user": {
"username": "[email protected]",
"password": "xxxxxxx@1xx7"
}
}
创建了一个辅助类来读取json文件
public interface IAppSettingConfiguration
{
public static IConfiguration InitConfiguration()
{
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.test.json")
.Build();
return config;
}
}
要读取基本网址,我使用以下代码
private IConfiguration _iAppSettingConfiguration;
private static string _readonlypageUrl;
public GivenAccount(PlaywrightFixture playwrightFixture)
{
_iAppSettingConfiguration = IAppSettingConfiguration.InitConfiguration();
_readonlypageUrl = _iAppSettingConfiguration["baseUrl"];
}
工作正常,我能够获取基本 URL 的值。如何使用
IOption<>
读取整个对象。就我而言,我想阅读user
无法从
Microsoft.Extensions.Configuration
命名空间中找到bind方法
namespace Microsoft.Extensions.Configuration
{
/// <summary>Represents a set of key/value application configuration properties.</summary>
public interface IConfiguration
{
/// <summary>Gets or sets a configuration value.</summary>
/// <param name="key">The configuration key.</param>
/// <returns>The configuration value.</returns>
string this[string key] { get; set; }
/// <summary>Gets a configuration sub-section with the specified key.</summary>
/// <param name="key">The key of the configuration section.</param>
/// <returns>The <see cref="T:Microsoft.Extensions.Configuration.IConfigurationSection" />.</returns>
IConfigurationSection GetSection(string key);
/// <summary>Gets the immediate descendant configuration sub-sections.</summary>
/// <returns>The configuration sub-sections.</returns>
IEnumerable<IConfigurationSection> GetChildren();
/// <summary>Returns a <see cref="T:Microsoft.Extensions.Primitives.IChangeToken" /> that can be used to observe when this configuration is reloaded.</summary>
/// <returns>A <see cref="T:Microsoft.Extensions.Primitives.IChangeToken" />.</returns>
IChangeToken GetReloadToken();
}
}
如果您希望将用户详细信息作为对象,您可以创建一个类并将其绑定到配置中的
user
部分。
类似的东西,
private IConfiguration _iAppSettingConfiguration;
private static string _readonlypageUrl;
public GivenAccount(PlaywrightFixture playwrightFixture)
{
_iAppSettingConfiguration = IAppSettingConfiguration.InitConfiguration();
_readonlypageUrl = _iAppSettingConfiguration["baseUrl"];
var _user = new UserDetail();
_iAppSettingConfiguration.GetSection(UserDetail.User).Bind(_user);
}
public class UserDetail
{
public const string User = "user";
public string UserName { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
}
选项 1:
在构建服务时,您可以使用 Services.Configure 方法。
选项2:
但是,如果您需要手动完成,则需要获得一个子部分
var username = IAppSettingConfiguration.GetSection("user").GetValue<string>("username");
var password = IAppSettingConfiguration.GetSection("user").GetValue<string>("password");
通常,如果您想从 appsettings.json 中获取某些字段,您可以使用 Startup.cs 中 WebApplicationBuilder 中的配置方法(.NET 6 中的 Program.cs)
{
"Foo": {
"bar": "someValue"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
例如,如果有这样的 appsettings.json 文件,我们可以通过这样做获得“bar”值(.NET 6 示例)
var Foo = builder.Configuration.GetSection("Foo");
var Bar = Foo.GetSection("bar");
或者 .NET 6 之前的情况。
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
var Foo = Configuration.GetSection("Foo")
var Bar = Foo.GetSection("Bar")
}
我想这就是你需要思考的方向,祝你好运!