A Workaround to Access the Connection String in a .NET Core application

Ray HuangEven with the Stay at Home order due to the COVID-19 virus pandemic, we’re still hard at work here at Everleap.

I haven’t had much time to personally explore development in .NET Core until recently now that I’m staying put.   I started about a week ago, ran through a couple of Microsoft tutorials, and delved right into it.  Immediately, I ran into a roadblock with the connection string because .NET Core doesn’t really make use of the web.config file which I’m used to in .NET Framework.  So, something as simple as assigning a connection string to a string using System.Configuration proved difficult. 

private static 
string connectionString = ConfigurationManager.AppSettings["ConnectionString"];

That’s because .NET Core allows you to pull configuration information from a variety of sources.  After researching the matter further on the web and looking at various solutions involving dependency injection and Microsoft.Extensions.Configuration, I couldn’t really find a working solution for what I wanted to do – which was to simply reference it in a class library project apart from the main project (in other words, I couldn’t get the code to work properly in a manner that I am used to), so I developed one of my own solution and am sharing it with you in this post. 

The idea is simple and only uses 2 libraries, Json.NET and System.IO.  Basically, you create a Settings class with a constructor that reads the settings from a file in json format, then parses and stores the settings in a json object (JObject).  You then define properties which return values from the json object containing the setting for easy access.  The complete code is found below. 

using Newtonsoft.Json.Linq; 
using System.IO; 
namespace Library.Framework 
    public class Settings 
        private JObject AppSettings; 
        public string ConnectionString 
                return AppSettings["ConnectionStrings"]["DefaultConnection"].ToString(); 
        public Settings() 
            string path = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json"); 
            string json = File.ReadAllText(path); 
            AppSettings = JObject.Parse(json); 

The appsettings.json file is a file in the root of my .NET Core project with my configuration settings.  Sample: 

  "ConnectionStrings": { 
    "DefaultConnection": "Data Source=tcp:s02.everleap.com;Initial Catalog=DB_27319_db;User ID=DB_27319_db_user;Password=myPassword;Integrated Security=False;" 

I’m treating it like the web.config file in .NET Framework.  (Note:  You don’t need to store the settings in appsettings.json.  It’s just the default one .NET Core creates for you.  You could easily use something like settings.json or settings.txt.  The important thing is that it must be a text file, and the contents within is in json format.

Now, accessing the connection string is simple.  You just instantiate the class and access the property.  For example, if I want to use it in a using statement: 

private static readonly Settings app = new Settings(); 
using (SqlConnection con = new SqlConnection(app.ConnectionString)) 

I hope this helps some developers out there.

(Disclaimer: This is just an example to help you get started for development/testing purposes.  There might be better or more secure ways to do this depending on the application you’re developing.

Visit Everleap to learn more about our .NET Core cloud hosting solutions.

Rate this article

One Response
  • Jazz Reply

    Thank you so much. Your blog helped me get through. I can’t thank you enough. Bless you.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

oui décor