dotnet/runtime

Json Configuration custom IFileProvider stopped working for .NET 5.0

Open

#47.455 geöffnet am 26. Jan. 2021

Auf GitHub ansehen
 (8 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)C# (5.445 Forks)batch import
area-Extensions-Configurationbughelp wanted

Repository-Metriken

Stars
 (17.886 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 12T 11h) (661 gemergte PRs in 30 T)

Beschreibung

We built support for encrypted appsettings.json in our app by using encrypted xml support built into .NET. To integrate this we use JsonConfigurationSource with a custom File Provider to return custom IFileInfo with CreateReadStream overriden to decrypt file. Like code below:

public static IConfigurationBuilder AddJsonFileProtectedByEncryptedXml(this IConfigurationBuilder builder, string path, bool optional = false, ILoggerFactory loggerFactory = null)
{
    var source = new JsonConfigurationSource
    {
        Path = path,
        // Specify file provider to use so that JsonConfigurationSource can read the decrypted form of config file.
        // Note config file does not need to be XML, only the container that contains the config file should be XML.
        FileProvider = new EncryptedXmlContainerFileProvider(loggerFactory),
        Optional = optional
    };
    builder.Add(source);
    return builder;
}

After upgrading to .NET 5.0 this stopped working.

Workaround: return null from PhysicalFilePath in custom IFileInfo. https://github.com/dotnet/runtime/blob/9b9303678de0f5573fef580f3f52404ea0a20dd4/src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationProvider.cs#L87

Contributor Guide