How to access IConfigurationRoot in startup on .net core 2?

13,074

Solution 1

Thanks to Dealdiane's comment.

We can downcast the IConfiguration:

public Startup(IConfiguration configuration)
{
    Configuration = (IConfigurationRoot)configuration;
}

public IConfigurationRoot Configuration { get; }

I am still not sure, if this is the intended way, since IConfiguration does not make any guarantees about IConfigurationRoot.

Solution 2

Or you can inject it before initialization of the Startup:

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

    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
        IConfigurationRoot configurationRoot = null;
        return WebHost.CreateDefaultBuilder(args)
                 .ConfigureAppConfiguration((context, builder) =>
                  {
                      configurationRoot = builder.Build();
                  })
                  .ConfigureServices(services =>
                  {
                      services.AddSingleton<IConfigurationRoot>(configurationRoot);
                      services.AddSingleton<IConfiguration>(configurationRoot);
                  })
                 .UseStartup<Startup>();
    }
}
Share:
13,074
Christian Gollhardt
Author by

Christian Gollhardt

Stack Overflow Inc. doesn't care anymore about it's core user base. So I don't care about Stack Overflow anymore, and mostly stopped contributing. Currently there is a new project which is under development by veteran Stack Overflow users, called Codidact. Im realy looking forward to this Open Source community-oriented Q&amp;A site. I have developed my first application when I was 13 years old. So yes, I have made my hobby a profession. I love writing frameworks more than using it, because with every solved problem you have learned something more. If you don't understand the basics, you don't understand the framework. In my early days I have written my own forum with php, css &amp; xhtml. That was early 2000. The time where every developer said: Don't use javascript! Nowadays we all know it better. Hell yeah, we have technologies like ajax &amp; json. I have earned some experience in wpf and mvvm. I realy love how the databinding works. Sadly today everything needs to be mobilefirst. It's rare to have the opportunity to do a project based on this. In current days I am fallen in love with c#, asp.net-mvc, asp.net-core entity-framework, and twitter-bootstrap. I am excited about every new technology I am able to meet. Next things I want to learn about: uwp angular Gamedevelopment in general If you want to provoke me, simple write Yoda-Code:

Updated on June 05, 2022

Comments

  • Christian Gollhardt
    Christian Gollhardt almost 2 years

    I have written a custom ConfigurationProvider with the entity framework. Since I also want to make it updateable during runtime, I have created a IWritableableOption.

    I need to refresh the configuration after the update. This can be done via IConfigurationRoot.Reload.

    However, how can I get the IConfigurationRoot in .net core 2?

    What I have found, is that in previous versions the IConfigurationRoot was part of startup. In .net core 2 however, we have only the simpler type IConfiguration:

    public Startup(IConfiguration configuration)
    {
        // I tried to change this to IConfigurationRoot,
        // but this results in an unresolved dependency error
        Configuration = configuration;
    }
    
    public IConfiguration Configuration { get; }
    

    I also have found out, I can get my own instance using

    WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration(context, builder) => {
        var configurationRoot = builder.build()
    })
    

    But I want to update the configuration used by Startup.

    So how can I get the IConfigurationRoot used by Startup to inject it into my service collection?