读取 asp.net 核心 5 测试项目中的 appsetting-test.json 文件

Reading appsetting-test.json file in asp.net core 5 test project

我有一个 Asp.net 核心 5 的测试项目并试图读取应用程序设置值。

使用以下配置在测试项目中创建了一个 appsetting.test.json 文件

{
    "baseUrl": "https://xxxxx.io/",
    "user": {
        "username": "xxxx.xxxx@gmail.com",
        "password": "xxxxxxx@1xx7"
    }
}

创建了一个助手 class 来读取 json 文件

  public interface IAppSettingConfiguration
    {
        public static IConfiguration InitConfiguration()
        {
            var config = new ConfigurationBuilder()
                .AddJsonFile("appsettings.test.json")
                .Build();
            return config;
        }
    }

要读取 baseurl,我使用以下代码

private IConfiguration _iAppSettingConfiguration;
private static string _readonlypageUrl;
public GivenAccount(PlaywrightFixture playwrightFixture)
        {
            _iAppSettingConfiguration = IAppSettingConfiguration.InitConfiguration();
            _readonlypageUrl = _iAppSettingConfiguration["baseUrl"];
            
        }

这工作正常我能够获得基 URL 的值。如何使用 IOption<> 读取整个对象。就我而言,我想阅读 user

无法从 Microsoft.Extensions.Configuration 命名空间中找到绑定方法

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();
  }
}

选项 1:

您可以在构建服务时使用 Services.Configure 方法。

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0#configure-options-with-a-delegate

选项 2:

但是,如果需要手动操作,则需要获取子部分

var username = IAppSettingConfiguration.GetSection("user").GetValue<string>("username");
var password = IAppSettingConfiguration.GetSection("user").GetValue<string>("password");

如果您希望将用户详细信息作为对象,您可以创建一个 class 并将其绑定到配置中的 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;
}

通常,如果您想从 appsettings.json 中获取一些字段,您可以使用 WebApplicationBuilder 中位于 Startup.cs(在 .NET 6 中位于 Program.cs)的配置方法

{
  "Foo": {
    "bar": "someValue"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

例如,对于 appsettings.json 这样的文件,我们可以通过执行此操作(.NET 6 示例)获得“bar”值

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")
}

我认为这是你需要思考的方向,祝你好运!