Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft.Framework.ConfigurationModel assembly name changed to Microsoft.Framework.Configuration #25

Open
kirthik opened this issue May 21, 2015 · 19 comments

Comments

@kirthik
Copy link

kirthik commented May 21, 2015

We changed the assembly name and namespace of Microsoft.Framework.ConfigurationModel to Microsoft.Framework.Configuration.

Configuration class name is changed to ConfigurationSection.

@developer1998
Copy link

@davidfowl Due to this change how can I rewrite

public IConfiguration Configuration { get; set; }

public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
// Setup configuration sources.
Configuration = new Configuration(appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddEnvironmentVariables();
}

which you provided in #13

I tried

    public IConfiguration Configuration { get; set; }

    public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
    {
        Configuration = new ConfigurationSection(appEnv.ApplicationBasePath).
            .AddJsonFile("config.json")
            .AddEnvironmentVariables();
    }

but it does not work

@kirthik
Copy link
Author

kirthik commented May 28, 2015

    public IConfiguration Configuration { get; set; }

    public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
    {
         var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath).
            .AddJsonFile("config.json")
            .AddEnvironmentVariables();
         Configuration = configurationBuilder.Build();
    }

The idea is you add sources toConfigurationBuilder and then call Build on it which returns IConfiguration object

@developer1998
Copy link

Thank you. I've tried but it tells me that there is no definition for AddJsonFile. Is this method maybe not implemented yet, since I've seen AddIniFile?

@developer1998
Copy link

nvm, I've included "Microsoft.Framework.Configuration.Json": "1.0.0-*" dependency in project.json and it works now, thank you very much.

@nrkarthick
Copy link

Yesterday I have migrated from beta4 to beta6, I used the above change but I could not get the values(key) in controller

 var Configuration = new ConfigurationBuilder().AddJsonFile("myconfig.json");
 var jsonNodes = Configuration.Get("SomeJsonNode");

It throws exception in Configuration.Get as 'Iconfigurationbuilder does not contain a definition for Get.

@kirthik
Copy link
Author

kirthik commented Jun 26, 2015

You have to call Build() on ConfigurationBuilder to build the Configuration now. Here is what you need to do

         var configurationBuilder = new ConfigurationBuilder().AddJsonFile("myconfig.json");
         var configuration = configurationBuilder.Build();

@nrkarthick
Copy link

@kirthik , thank u. but it throws error
Unable to resolve path 'myconfig.json'; construct this IConfigurationBuilder with a non-null BasePath.

in my startup class I already defined the path
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
// Setup configuration sources.
var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
.AddJsonFile("config.json")
.AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);
}

I could not get the value of connection string from the config.json

@kirthik
Copy link
Author

kirthik commented Jun 26, 2015

Sorry should have been more explicit in my earlier comment. You should either provide full path of file to AddJsonFile or add a base path to ConfigurationBuilder. Pasting sample from my earlier comment

public IConfiguration Configuration { get; set; }

    public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
    {
         var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath).
            .AddJsonFile("config.json")
            .AddEnvironmentVariables();
         Configuration = configurationBuilder.Build();
    }

@nrkarthick
Copy link

thank u.. I have already using this in my startup class, please see my comment... I just want to get the connection string value in my Controller. I also included the IApplicationEnvironment appEnv in the below code without success. please help

 private string returnConnectionString()
          {
              IApplicationEnvironment appEnv;

            var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath).AddJsonFile("config.json");
            var configuration = configurationBuilder.Build();

            var Conn = configuration.Get("Data:ConnectionString");
            return Conn;
 }

But in beta4, I can easily grab the values stored in config.json using the following code:

 var configuration = new Configuration().AddJsonFile("config.json");
 var Conn = configuration.Get("Data:ConnectionString");
 return Conn;

@kirthik
Copy link
Author

kirthik commented Jun 26, 2015

Where are you instantiating appEnv?

@nrkarthick
Copy link

I am instantiating appEnv in my Startup Class... as follows :

 public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
{
     var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath).
        .AddJsonFile("config.json")
        .AddEnvironmentVariables();
     Configuration = configurationBuilder.Build();
}

I think the problem is with appEnv.. and still getting the same error

 Unable to resolve path 'myconfig.json'; construct this IConfigurationBuilder with a non-null BasePath.

@ngonzalezromero
Copy link

Try this

var path = app.ApplicationBasePath;
            Configuration = new ConfigurationBuilder()
            .AddJsonFile($"{path}/config.json")
            .AddEnvironmentVariables()
            .Build();

@nrkarthick
Copy link

This is ok in startup class(with path).. But how to get the connection string value/key which is defined in config.json in the controller. The following method returns error unable to resolve path.

 private string returnConnectionString()
      {
        var configurationBuilder = new ConfigurationBuilder().AddJsonFile("config.json");
        var configuration = configurationBuilder.Build();

        var Conn = configuration.Get("Data:ConnectionString");
        return Conn;
  }

@ngonzalezromero
Copy link

you can't create a ConfigurationBuilder each time the you need the connection string.
This is the way.

   public IConfiguration Configuration { get; set; }

    public Startup(IHostingEnvironment env, IApplicationEnvironment app)
    {
        var path = app.ApplicationBasePath;
        Configuration = new ConfigurationBuilder()
        .AddJsonFile($"{path}/config.json")
        .AddEnvironmentVariables()
        .Build();
    }

    public void ConfigureServices(IServiceCollection services)
    {
             var Conn = configuration.Get("Data:ConnectionString");
    }

@nrkarthick
Copy link

@ngonzalezromero u r right... I am getting the connection string in the ConfigureServices as per ur comment... Could u guide me how to get the same in my controller/class ...?..

@MichaCo
Copy link

MichaCo commented Jun 29, 2015

Usually you want to use DI to inject whatever you need to your controller's ctor.
You could use the Microsoft.Framework.OptionsModel to configure a strongly typed settings class from the IConfiguration object and let it bind the data for you automagically.

services.Configure<AppSettings>(Configuration.GetConfigurationSection("Data"));

and then inject it

public HomeController(IOptions<AppSettings> settings)
{
    var connectionString = settings.Options.ConnectionString;
}

How you structure your configuration data or settings object is totally up to you.

@nrkarthick
Copy link

Thank u Michaco, but when using this i am getting error:

 Cannot create an instance of an interface. 

I will try to Add IOptions Settings to the constructor of my MVC controller and let u know

@Eilon
Copy link
Member

Eilon commented Jun 29, 2015

@nrkarthick for general discussions and issues please log an item in https://github.com/aspnet/Home/issues. Please create a GitHub repo with a simplified app that demonstrates the issue.

@dnxit
Copy link

dnxit commented Aug 18, 2015

what if I need var serviceUrl = settings.Options.ServiceUrl; in a utility class instead of Controller how to get IOptions settings in the ctor ??????

public HomeController(IOptions settings)
{
var serviceUrl = settings.Options.ServiceUrl;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants