Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am in the process of learning ASP.NET vNext. I need to store two connection strings in the config.json file. How do I store those? Can I do this:

config.json

{
  "connectionStrings" : {
    "connection-1" : "server=...",
    "connection-2" : "server=..."
  }
}

I have not been able to find a schema for config.json. For that reason, I wasn't sure how to do it. I saw the use of IConfiguration here. Still, I wasn't sure how Configuration was available in the app.

share|improve this question
up vote 6 down vote accepted

This is the official Configuration repo and this is a great blog post explaining the configuration system.

Long story short:

  1. You need one or more Microsoft.Framework.ConfigurationModel.* NuGet packages in your application. The list of available packages is here.
  2. Add the configuration sources that you need
  3. Build the config file(s)
  4. Read the configuration sources
share|improve this answer
1  
Thank you for sharing. How would you use this model on a class library though? From what I can see, I would have to re-instantiate an IConfiguration object in every single instance of a class in my class library. This seems inefficient. Please correct me if I'm wrong. – xam developer Jan 9 '15 at 13:45
    
You can inject IConfiguration or even hold a static reference to it somewhere... – Victor Hurdugaci Jan 9 '15 at 14:17
    
Thank you for your patience. Do you know of an example of IConfiguration getting injected? I'm slowly learning this stuff :) – xam developer Jan 9 '15 at 15:23
    
I wrote this article a while ago and except a few packages being renamed, it should be a good place to start with dependency injection in ASP.NET vNext blogs.msdn.com/b/webdev/archive/2014/06/17/… – Victor Hurdugaci Jan 9 '15 at 17:59

Here is how you can do it:

 {   
      "Data": {
        "connection-1": {
          "ConnectionString": "Server=..."
        },
        "connection-2": {
          "ConnectionString": "Server=..."
        }   
}

}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.