KeyPerFileConfigurationSource 没有同时设置目录路径和忽略条件的选项
KeyPerFileConfigurationSource doesn't have an option to set directory path and ignore condition at the same time
我正在使用 .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
?
我找到了解决办法。你应该像这样使用 PhysicalFileProvider
:
ConfigurationBuilder cb = new ConfigurationBuilder()
.AddKeyPerFile(x =>
{
x.FileProvider = new PhysicalFileProvider("/abc123");
x.Optional = true;
x.IgnoreCondition = fileName => !fileName.StartsWith("MyApp_");
});
我正在使用 .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
?
我找到了解决办法。你应该像这样使用 PhysicalFileProvider
:
ConfigurationBuilder cb = new ConfigurationBuilder()
.AddKeyPerFile(x =>
{
x.FileProvider = new PhysicalFileProvider("/abc123");
x.Optional = true;
x.IgnoreCondition = fileName => !fileName.StartsWith("MyApp_");
});