{"id":22365,"date":"2018-04-26T20:00:00","date_gmt":"2018-04-26T20:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/premier_developer\/?p=22365"},"modified":"2019-02-21T09:22:06","modified_gmt":"2019-02-21T16:22:06","slug":"setting-up-net-core-configuration-providers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/premier-developer\/setting-up-net-core-configuration-providers\/","title":{"rendered":"Setting up .NET Core Configuration Providers"},"content":{"rendered":"<p>In this blog post, Premier Developer Consultant <a href=\"https:\/\/www.linkedin.com\/in\/randyrpatterson\/\" target=\"_blank\" rel=\"noopener\">Randy Patterson<\/a> teaches us how to set up .NET Core configuration providers in Visual Studio. He shows how to look at NuGet packages to access the Configuration API and then setup the providers as ASP .NET Core web applications.<\/p>\n<hr \/>\n<p>ASP Core uses the convenient <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ms972319.aspx\">Provider Pattern<\/a> to load configuration key\/value pairs from various sources and expose those to you as a single Configuration object. This allows you to grab a configuration key like <em>ConnectionString<\/em> with one line of code regardless of where it was sourced from. However, the configuration API is not available by default in .NET Core Console applications.<\/p>\n<p>In this article we will look at the Nuget packages necessary to access the Configuration API and setup the same providers as ASP Core web applications.<\/p>\n<h2>Create a .NET Core Console Application<\/h2>\n<p>In <em>Visual Studio 2017<\/em> go to <em>.NET Core<\/em> Then <em>Console Application<\/em> and give it a name like \u201cMyCoreConsoleApp\u201d<\/p>\n<p>Next, add a <a href=\"http:\/\/json.org\/\">JSON<\/a> configuration file with the name of <strong><em>appsettings.json<\/em><\/strong> as the first source of configuration keys.<\/p>\n<p><img decoding=\"async\" width=\"941\" height=\"653\" class=\"wp-image-35253\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image.png 941w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-300x208.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-768x533.png 768w\" sizes=\"(max-width: 941px) 100vw, 941px\" \/><\/p>\n<p>Be sure the property \u201c<em>Copy to Output Directory\u201d<\/em> is set to \u201c<em>Copy Always\u201d<\/em> for the newly added JSON file. This will ensure that the file is published with the application.<\/p>\n<p><img decoding=\"async\" width=\"400\" height=\"526\" class=\"wp-image-35254\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/e-systemtemp-snaghtml198df66e-png.png\" alt=\"E:\\SystemTemp\\SNAGHTML198df66e.PNG\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/e-systemtemp-snaghtml198df66e-png.png 400w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/e-systemtemp-snaghtml198df66e-png-228x300.png 228w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>Replace the contents of the <em>appsettings.json<\/em> file with the following content:<\/p>\n<p><code>{<\/code><\/p>\n<p><code>\"Message\":\"Hello from appsettings.json\"<\/code><\/p>\n<p><code>}<\/code><\/p>\n<p>This will set the property \u201cMessage\u201d to the value of \u201cHello\u2026\u2026\u201d<\/p>\n<h2>Write Some Code<\/h2>\n<p>Before we update the application, we need to add the Nuget package that contains the provider needed to load JSON configuration files.<\/p>\n<p><code>install-package Microsoft.Extensions.Configuration.Json<\/code><\/p>\n<p>Next, replace the contents of the Main method with the following code.<\/p>\n<p><code>static void Main(string[] args)<\/code><\/p>\n<p><code>{<\/code><\/p>\n<p><code>IConfiguration config = new ConfigurationBuilder()<\/code><\/p>\n<p><code>.AddJsonFile(\"appsettings.json\", optional:true, reloadOnChange:true)<\/code><\/p>\n<p><code>.Build();<\/code><\/p>\n<p><code>Console.WriteLine($\"Contents of Message Property: {message}\");<\/code><\/p>\n<p><code>Console.ReadKey();<\/code><\/p>\n<p><code>}<\/code><\/p>\n<p>This will load the appsettings.json file into the configuration object extracting each property and value. In our case we have one property defined named <em>Message<\/em> set to the value of \u201c<em>Hello from appsettings.json<\/em>\u201d. The <em>WriteLine <\/em>method will display the value of the <em>Message<\/em> property to the console window.<\/p>\n<p>Running the application yields the following output.<\/p>\n<p><img decoding=\"async\" width=\"918\" height=\"175\" class=\"wp-image-35255\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-1.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-1.png 918w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-1-300x57.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-1-768x146.png 768w\" sizes=\"(max-width: 918px) 100vw, 918px\" \/><\/p>\n<p>You\u2019ve successfully loaded configuration data from an appsettings.json file.<\/p>\n<h2>Add Environment Variables<\/h2>\n<p>Next, add the Environment Variables Provider by installing the following Nuget Package<\/p>\n<p><code>install-package Microsoft.Extensions.Configuration.EnvironmentVariables<\/code><\/p>\n<p>and update the Main method to include the provider<\/p>\n<p><code>IConfiguration config = new ConfigurationBuilder()<\/code><\/p>\n<p><code>.AddJsonFile(\"appsettings.json\", optional:true, reloadOnChange:true)<\/code><\/p>\n<p><code>.AddEnvironmentVariables()<\/code><\/p>\n<p><code>.Build();<\/code><\/p>\n<p>This will load environments variables into the configuration object and is useful for cross-platform or container deployments. Furthermore, because Environment Variables are loaded <strong>after<\/strong> the appsettings.json file, any duplicate keys will replace the values from the <em>appsettings.json<\/em> file.<\/p>\n<p>Next, add an environment variable named \u201cMessage\u201d to override the Message property in appsettings.json from the Project Properties Page<\/p>\n<p><img decoding=\"async\" width=\"1236\" height=\"490\" class=\"wp-image-35256\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-2.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-2.png 1236w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-2-300x119.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-2-768x304.png 768w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-2-1024x406.png 1024w\" sizes=\"(max-width: 1236px) 100vw, 1236px\" \/><\/p>\n<p>Looking at the output displayed below you can see that the environment variables provider replaced the <em>Message<\/em> key that was initially set in the appsettings.json file with the contents of the environment variable.<\/p>\n<p><img decoding=\"async\" width=\"927\" height=\"228\" class=\"wp-image-35257\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-3.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-3.png 927w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-3-300x74.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-3-768x189.png 768w\" sizes=\"(max-width: 927px) 100vw, 927px\" \/><\/p>\n<h2>Add Command Line Arguments<\/h2>\n<p>Finally, we\u2019ll add the Command Line Arguments configuration provider. Similar to the previous providers, the first step is to install the proper Nuget package:<\/p>\n<p><code>Install-Package Microsoft.Extensions.Configuration.CommandLine<\/code><\/p>\n<p>Next, update the Main method to load the new provider.<\/p>\n<p><code>IConfiguration config = new ConfigurationBuilder()<\/code><\/p>\n<p><code>.AddJsonFile(\"appsettings.json\", optional:true, reloadOnChange:true)<\/code><\/p>\n<p><code>.AddEnvironmentVariables()<\/code><\/p>\n<p><code>.AddCommandLine(args)<\/code><\/p>\n<p><code>.Build();<\/code><\/p>\n<p>Finally, update the project properties to add a command line argument to replace the Message property with the string \u201cHello from args\u201d.<\/p>\n<p><img decoding=\"async\" width=\"815\" height=\"466\" class=\"wp-image-35258\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-4.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-4.png 815w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-4-300x172.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-4-768x439.png 768w\" sizes=\"(max-width: 815px) 100vw, 815px\" \/><\/p>\n<p>Run the application and the output should look like the following<\/p>\n<p><img decoding=\"async\" width=\"802\" height=\"209\" class=\"wp-image-35259\" src=\"http:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-5.png\" srcset=\"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-5.png 802w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-5-300x78.png 300w, https:\/\/devblogs.microsoft.com\/premier-developer\/wp-content\/uploads\/sites\/31\/2018\/04\/word-image-5-768x200.png 768w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><\/p>\n<p>The property \u201cMessage\u201d was set three times. Once in appsettings.json file, then as an environment variable and finally as a command line argument. The last provider loaded wins any conflicts.<\/p>\n<h2>Conclusion<\/h2>\n<p>.NET Core added a powerful and flexible configuration API that is available by default in ASP Core application. Simply adding a few Nuget Packages to any .NET Core application will give you the same flexibility. The order the providers are added to the configuration builder is critical as the last loaded overrides any previous values. By applying the providers in the order specified in this article (Json, Environment Variables and Command Line Arguments) you will have a similar configuration environment found in ASP Core applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ASP Core uses the convenient Provider Pattern to load configuration key\/value pairs from various sources and expose those to you as a single Configuration object. This allows you to grab a configuration key like ConnectionString with one line of code regardless of where it was sourced from. However, the configuration API is not available by default in .NET Core Console applications.<\/p>\n","protected":false},"author":582,"featured_media":37840,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[113],"tags":[83,61,314,46],"class_list":["post-22365","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-visual-studio","tag-net-core","tag-asp-net-core","tag-randy-patterson","tag-visual-studio"],"acf":[],"blog_post_summary":"<p>ASP Core uses the convenient Provider Pattern to load configuration key\/value pairs from various sources and expose those to you as a single Configuration object. This allows you to grab a configuration key like ConnectionString with one line of code regardless of where it was sourced from. However, the configuration API is not available by default in .NET Core Console applications.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/22365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/users\/582"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/comments?post=22365"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/posts\/22365\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media\/37840"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/media?parent=22365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/categories?post=22365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/premier-developer\/wp-json\/wp\/v2\/tags?post=22365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}