Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core 3.1 adding additional config.json file to configuration argument in Startup

I need to add another configuration file to my .Net Core Blazor project. Other sources (such as this) mention using configuration builder in .Net core 2.2, but since in 3.1 the configuration is passed as a parameter into Startup, I have no way of appending another config onto it as far as I can tell. Maybe I'm looking at it wrong, but I'm assuming that the configuration passed in as a param has important configuration properties, therefor building it my self with just the additional config file seems like it leave those config properties out.

Ex: Doesn't work but should paint a picture of what I'm trying to do.

public Startup(IConfiguration configuration)
{
      Configuration = configuration;
      var builder = new ConfigurationBuilder(configuration)
              .AddJsonFile("accountconstants.json", optional: true, reloadOnChange: true);
      });
}
like image 252
fr-Dmli Avatar asked Mar 04 '20 18:03

fr-Dmli


People also ask

Can I add more than two Appsettings json files in dotnet core?

Can I add more than two appsettings. json files in dotnet core? Of course, we can add and use multiple appsettings. json files in ASP.NET Core project.

How do I run Appsettings json at startup?

In order to add AppSettings. json file, right click on the Project in Solution Explorer. Then click Add, then New Item and then choose App Settings File option (shown below) and click Add button. Once the File is created, it will have a DefaultConnection, below that a new AppSettings entry is added.

What is the JSON configuration provider used for?

For example, the JSON configuration provider can be used to map appsettings.json files to .NET objects and is used with dependency injection. This enables the options pattern, the options pattern uses classes to provide strongly typed access to groups of related settings.

How do I configure ASP NET Core configuration?

Configuration in ASP.NET Core is performed using one or more configuration providers. Configuration providers read configuration data from key-value pairs using a variety of configuration sources: Settings files, such as appsettings.json. Environment variables.

How do I add configuration to a dotNET console application?

Settings files, such as appsettings.json New .NET console applications created using dotnet new or Visual Studio by default do not expose configuration capabilities. To add configuration in a new .NET console application, add a package reference to Microsoft.Extensions.Hosting. Modify the Program.cs file to match the following code:

Where do I put runtime configuration in a JSON file?

When a project is built, an [appname].runtimeconfig.json file is generated in the output directory. If a runtimeconfig.template.json file exists in the same folder as the project file, any configuration options it contains are inserted into the [appname].runtimeconfig.json file.


2 Answers

You can do it in Program.cs i.e. earlier in the pipeline rather than in Startup.cs.

Example:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((hostContext, config) =>
            {
                var env = hostContext.HostingEnvironment;

                config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}
like image 57
brk Avatar answered Oct 13 '22 20:10

brk


Your approach should work, but needs a little bit of tweaking.

The configuration that you create needs to be added to the DI Container. The documentation for this is here.

I've achieved it as well in Azure Functions and other projects through the following:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IConfiguration>(provider => new ConfigurationBuilder()
            .AddEnvironmentVariables()
            .AddJsonFile("accountconstants.json", optional: true, reloadOnChange: true)
            .Build());
}
like image 32
peinearydevelopment Avatar answered Oct 13 '22 19:10

peinearydevelopment