{"id":1874,"date":"2023-10-26T20:55:07","date_gmt":"2023-10-26T09:55:07","guid":{"rendered":"https:\/\/ntsblog.homedev.com.au\/?p=1874"},"modified":"2023-10-26T20:59:15","modified_gmt":"2023-10-26T09:59:15","slug":"net-core-6-register-environment-appsettings-json-files","status":"publish","type":"post","link":"https:\/\/ntsblog.homedev.com.au\/index.php\/2023\/10\/26\/net-core-6-register-environment-appsettings-json-files\/","title":{"rendered":".Net Core 6 &#8211; register environment appsettings.json files"},"content":{"rendered":"<div id=\"ntsbl-2379094402\" class=\"ntsbl-before-content ntsbl-entity-placement\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6288941070289539\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:inline-block;width:728px;height:90px;\" \ndata-ad-client=\"ca-pub-6288941070289539\" \ndata-ad-slot=\"9356781486\"><\/ins> \n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div>\n<h2 class=\"wp-block-heading\">The problem<\/h2>\n\n\n\n<p>A .net core application comes with a framework that creates an appsettings.{env}.json file that can store environment specific settings. <\/p>\n\n\n\n<p>Generally settings in appsettings.json are overridden by appsettings.{env}.json, but it does not do this out of the box.<\/p>\n\n\n\n<p>You need to configure this as part of the setup of the application<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Preconditions<\/h2>\n\n\n\n<p>So the solution code sample makes sense, lets assume we have the following in a appsettings.prod.json file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"ApiSettings\": {\n     \"apiBaseUrl\" : \"https:\/\/api.website.com\/api\"\n  }\n}\n<\/code><\/pre>\n\n\n\n<p>Then you may have a class like <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class ApiSettings \n{\n   public const string SectionName = \"ApiSettings\";\n\n   public string ApiBaseUrl { get; set; }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">The solution<\/h2>\n\n\n\n<p>I like to add a ServiceCollectionExtension class where I create extensions that can be called to implement various startup functionality<\/p>\n\n\n\n<p>The following code block is an example of how to register your .env json files into the statup, so that environment specific attributes are used.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public static class ServiceCollectionExtensions\n{\n    public static void AddConfiguration(this IServiceCollection services, ConfigurationManager configuration, IWebHostEnvironment env)\n    {\n        configuration\n        .SetBasePath(env.ContentRootPath)\n        .AddJsonFile(\"appsettings.json\", optional: false, reloadOnChange: true)\n        .AddJsonFile($\"appsettings.{env.EnvironmentName}.json\", optional: true)\n        .AddEnvironmentVariables();\n\n        \/\/ register configuration options\n        services.Configure&lt;ApiSettings&gt;(configuration.GetSection(ApiSettings.SectionName));\n    }\n}<\/code><\/pre>\n\n\n\n<p>In .Net 6 which is minimal and does not have a startup.cs file, the setup is initiated in the Program.cs file.<\/p>\n\n\n\n<p>To call this Configuration extension you then call it like so.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>builder.Services.AddConfiguration(builder.Configuration, builder.Environment);<\/code><\/pre>\n\n\n\n<p>The point of the final line of the registration is to register a section into the application so the IOptions works with dependency injection to gain access to your configuration.<\/p>\n\n\n\n<p>The following code shows how this works.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class UserService \n{\n\n   private readonly ApiSettings _apiSettings;\n\n   public UserService(\n      IOptions&lt;ApiSettings&gt; apiOptions\n   ) \n   {\n      _apiSettings = apiOptions.value;\n   }\n\n}<\/code><\/pre>\n\n\n\n<p>This will inject the apiSettings class into your object populated with the values from the specific appsettings.json file and allows you to access _apiSettings.ApiBaseUrl anywhere within your class.<\/p>\n\n\n\n<p>Hope this boiler plate code helps.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The problem A .net core application comes with a framework that creates an appsettings.{env}.json file that can store environment specific settings. Generally settings in appsettings.json [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[37,5],"tags":[46,31],"class_list":["post-1874","post","type-post","status-publish","format-standard","hentry","category-net-core","category-c","tag-net-core","tag-c"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/1874","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/comments?post=1874"}],"version-history":[{"count":0,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/1874\/revisions"}],"wp:attachment":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/media?parent=1874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/categories?post=1874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/tags?post=1874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}