Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to reconfigure App configuration after "CreateDefaultBuilder" call

In CreateDefaultBuilder a call is made to ConfigureAppConfiguration with some defaults. I'd like to override (or append) to these defaults. I've tried copying CreateDefaultBuilder into my own function and editing the appropriate lines, however it can't resolve a type used in the function because the type is internal to AspNetCore namespaces (specifically the HostFilteringStartupFilter type).

Is there some cleaner way for me to modify the CreateDefaultBuilder functionality?

EDIT: So I just looked at the source of HostFilteringStartupFilter and it just calls UseHostFiltering on the IApplicationBuilder, so I just added that to my Startup.Configure but I would still like a cleaner approach if possible..

like image 226
Barak Gall Avatar asked Jun 07 '18 23:06

Barak Gall


People also ask

What does webhost CreateDefaultBuilder () do?

CreateDefaultBuilder()Initializes a new instance of the WebHostBuilder class with pre-configured defaults.

Can I use app config in .NET Core?

Application 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.

How do I connect to Azure app configuration?

In the upper-left corner of the home page, select Create a resource. In the Search services and marketplace box, enter App Configuration and select Enter. Select App Configuration from the search results, and then select Create. Select the Azure subscription that you want to use to test App Configuration.


1 Answers

CreateDefaultBuilder sets up reasonable defaults that you want to preserve except the configuration part. CreateDefaultBuilder performs the following configuration setup:

  1. appsettings.json
  2. appsettings.{Environment}.json
  3. Secret Manager (if Development environment)
  4. Environment variables.
  5. Command-line arguments.

Knowing this default layering, the fact that every layer can potentially override values in previous ones and the fact that you can only add configuration providers to the list (while not being able to remove them), you can now solve your problem.

Overriding configuration on top of CreateDefaultBuilder is done using ConfigureAppConfiguration :

    WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            // Override here specifying new or previous layers to make sure 
            // you get the desired outcome.
            // e.g. If you specify your appsettings.json you basically start from scratch
            ...

        })...
like image 138
Cosmin Sontu Avatar answered Oct 20 '22 13:10

Cosmin Sontu