12

I used to specify the application base path for the ConfigurationBuilder like this:

public Startup(IApplicationEnvironment appEnv)
{
    var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
        .AddJsonFile("config.json")
        .AddEnvironmentVariables();

    Configuration = configurationBuilder.Build();
}

However, as of beta8, the constructor of ConfigurationBuilder does not take an application base path argument anymore and it throws an exception now.

How can I specify the base path?

Henk Mollema
  • 44,194
  • 12
  • 93
  • 104

1 Answers1

15

If we look at the source code of ConfigurationBuilder, we can see that the constructor no longer accepts a string representing the application base path. In stead, we have to use the SetBasePath() extension method on the IConfigurationBuilder interface to specify it:

public Startup(IApplicationEnvironment appEnv)
{
    var configurationBuilder = new ConfigurationBuilder()
        .SetBasePath(appEnv.ApplicationBasePath)
        .AddJsonFile("config.json")
        .AddEnvironmentVariables();

    Configuration = configurationBuilder.Build();
}

The particular commit can be found here.

Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
  • Sorry if the question is very late but how do you add the IApplicationEnvironment? I am searching for hours now and can't get it working.. I am pretty new to C#. If I want to inject the IApplicationEnvironment it says that it couldn't be found? – Tom el Safadi Dec 30 '15 at 02:31
  • As well I am following a tutorial. He is using the "Microsoft.Framework.Configuration" and if I include this into my project I can't apply the ".AddEnviromentVariables()" method. In order to do this I need the "using Microsoft.Extension.Configuration". But this doen't allow me to use the IApplicationEnviroment?? – Tom el Safadi Dec 30 '15 at 02:35
  • What version do you use @Anokrize? RC1 or RC2? The `IApplicationEnvironment` class lives in the `Microsoft.Extensions.PlatformAbstractions` namespace. – Henk Mollema Jan 02 '16 at 10:04
  • Why doesn't this appear in Core 2? – Ian Warburton Sep 29 '17 at 15:15