3

I have appsettings.json with build action set to Embedded Resource. I usually extract the file and make a new copy of it as follows.

string jsonFullPath = Path.Combine(FileSystem.AppDataDirectory, "namespace.directory.appsettings.json");
Assembly assembly = Assembly.GetExecutingAssembly();

using (Stream resourceFileStream = assembly.GetManifestResourceStream(resourceFilename))
{
    if (resourceFileStream != null)
    {
        using (FileStream fileStream = File.Create(jsonFullPath))
        {
            resourceFileStream.CopyTo(fileStream);
        }
    }
}

And add the copy to an instance of ConfigurationBuilder as follows.

Configuration = new ConfigurationBuilder()
    .AddJsonFile(jsonFullPath, optional: true, reloadOnChange: true)
    .Build();

Question

Is it possible to make ConfigurationBuilder use the embedded JSON file directly without creating a new temporary file as I did above? If yes, how to do so?

3 Answers 3

4

I would suggest that use AddJsonStream instead of AddJsonfile.

Useful resource here with sample code:

public static void AddEmbeddedJsonFile(this IConfigurationBuilder configuration, string resourceName)
{
    var host = Assembly.GetEntryAssembly();
    if (host == null)
        return;

    var fullFileName = $"{host.GetName().Name}.{resourceName}";
    using var input = host.GetManifestResourceStream(fullFileName);
    if (input != null)
    {
        configuration.AddJsonStream(input);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

In ASP.NET Core 6, streaming the appsettings.ini from an embedded resource is done as follows:

var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
    ContentRootPath = AppContext.BaseDirectory,//Path.GetFullPath(Directory.GetCurrentDirectory()),
    Args = args,
});

builder.Host.ConfigureAppConfiguration((hostingContext, config) =>
{    
    using (var resourceStream = this.GetType().Assembly.GetManifestResourceStream("Namespace.filename.ext"))
    {
        config.AddJsonStream(resourceStream);
    }
}
...

Comments

0

If you use a ManifestEmbeddedFileProvider, then you can continue using AddJsonFile without any other changes.

<!-- MyService.csproj -->

<PropertyGroup>
  <!-- https://learn.microsoft.com/en-us/aspnet/core/fundamentals/file-providers?view=aspnetcore-8.0#manifest-embedded-file-provider -->
  <GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
</PropertyGroup>

<ItemGroup>
  <!-- glob patterns are allowed here -->
  <EmbeddedResource Include="*appsettings.json" />
</ItemGroup>
// Program.cs

IConfigurationBuilder configBuilder = new ConfigurationBuilder();

// Get the default file provider.
IFileProvider defaultFileProvider = configBuilder.GetFileProvider();

// Initialize the embedded resource file provider.
var manifestEmbeddedFileProvider = new ManifestEmbeddedFileProvider(Assembly.GetExecutingAssembly());

// Create a composite file provider to retain the default behavior,
// but also enable loading files embedded in the assembly manifest.
var compositeFileProvider = new CompositeFileProvider(defaultFileProvider, manifestEmbeddedFileProvider);

configBuilder.SetFileProvider(compositeFileProvider);

// Add the JSON file embedded in the assembly.
configBuilder.AddJsonFile("namespace.directory.appsettings.json");

IConfiguration config = configBuilder.Build();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.