0

我正在使用.NET Core 3.1和 Docker 机密来存储敏感的配置数据。我正在使用AddKeyPerFile扩展方法告诉 .NET Core 从文件系统上的文件中加载键值对。我想设置directoryPath这些文件的位置并IgnoreCondition忽略特定文件。但是,KeyPerFileConfigurationBuilderExtensions只包含以下两种方法:

public static IConfigurationBuilder AddKeyPerFile(this IConfigurationBuilder builder, Action<KeyPerFileConfigurationSource> configureSource);
public static IConfigurationBuilder AddKeyPerFile(this IConfigurationBuilder builder, string directoryPath, bool optional);

...并KeyPerFileConfigurationSource包含以下属性:

public IFileProvider FileProvider { get; set; }
public Func<string, bool> IgnoreCondition { get; set; }
public string IgnorePrefix { get; set; }
public bool Optional { get; set; }

这是我目前拥有的代码:

ConfigurationBuilder cb = new ConfigurationBuilder();
// I want to set 'IgnoreCondition' as well
cb.AddKeyPerFile(directoryPath: "/abc123", optional: true);

如何同时设置directoryPath AND IgnoreCondition

4

1 回答 1

0

我找到了解决方案。你应该PhysicalFileProvider像这样使用:

ConfigurationBuilder cb = new ConfigurationBuilder()
    .AddKeyPerFile(x =>
    {
        x.FileProvider = new PhysicalFileProvider("/abc123");
        x.Optional = true;
        x.IgnoreCondition = fileName => !fileName.StartsWith("MyApp_");
    });
于 2021-08-10T10:52:51.880 回答