By: Chris Dunn
It's always easier to work with typed information in a program than trying to remember string keys in a configuration file. So when working with the appsettings.json file in .net core, I advise you load the configuration file into a class object and work with the object directly, instead of GetSection() repeatedly throughout your code.
It's also a good idea to create the class object as a service so you can inject it into controllers for ease of use while still maintaining test-ability. I'll do my best to walk you through doing just that.
First, we'll start of with the appsettings.json file. I put all of my settings for the application under the AppSettings section of the json file. The file will contain other sections like Logging which is used to configure the LoggingFactory. I'm not going to worry about including any section other than the AppSettings section in my class definition. Below is an example, which contains two key/values under the AppSettings section. Company and Message.
{ "AppSettings": { "Company": "My Company", "Message":"Hello world!" }, "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } } }
Next, we'll define a class which contains both of those keys as properties in our class. It's just a simple POCO with no fancy attributes or decorators.
public class AppSettings { public string CompanyName{get;set;} public string Message{get;set;} }
Now that we have the raw data (json) and the AppSettings class, we need to load files ( default and environment specific) in the startup method of our startup class. Rather than use the default configuration, we're going to tweak things to use the configuration builder to use our own configuration. Finally we assign the resulting configuration from the builder to the class level Configuration so we can use it in the ConfigureServices method up next.
private IConfiguration Configuration { get; } private IHostingEnvironment Environment { get; } public Startup(IConfiguration configuration, IHostingEnvironment environment) { //load appsettings from json file(s) var builder = new ConfigurationBuilder() .SetBasePath(environment.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile("appsettings.{environment.EnvironmentName}.json", optional: true) .AddEnvironmentVariables(); //build configuration Configuration = builder.Build(); Environment = environment; }
Here is where we bind our key/value pairs in the json file(s) to the object and create our service. The "AppSettings" key is whatever value you use in the json file to identify the AppSettings section. We only need a single instance of the AppSettings since the settings are application wide. So we use the AddSingleton method of IServiceCollection.
public void ConfigureServices(IServiceCollection services) { //load typed appsettings as singleton service services.AddSingleton(Configuration.GetSection("AppSettings").Get<AppSettings>()); ... services.AddMvc(); }
To use our AppSettings ,inject the AppSettings into one of our page controllers in the constructor (or action). The values in AppSettings are now available for use in our View Actions as seen in the Index action method.
public class HomeController : Controller { private readonly AppSettings _appSettings; public HomeController(AppSettings appSettings) { _appSettings= appSettings; } public IActionResult Index() { ViewBag.Company = _appSettings.Company; return View(); } }Tags: c# .net core configuration services dependency injection
Copyright 2023 Cidean, LLC. All rights reserved.
Proudly running Umbraco 7. This site is responsive with the help of Foundation 5.