Last post Sep 30, 2019 10:22 PM by bruce (sqlwork.com)
Sep 30, 2019 07:47 PM|plettb|LINK
When using ApplicationSettingsBase in a .NET Core 2 app, where does it read properties from by default? Is it possible to change the default, and, if so, how? I'd really like to use it to read from something like "AppSettings.json",but haven't figured
out how. I've been playing around with the "Providers" property a little, but with no success. I must admit that I'd have thought this would be trivial and/or documented, but I've been searching for several hours and have yet to find the answer. Another
thing that would be truly wonderful is to have a working example!
Thanks a lot!
Sep 30, 2019 08:35 PM|mgebhard|LINK
Configuration is super easy use to use in ASP.NET Core. Like all services in ASP.NET Core, configuration is injected into any class you like using the constructor injection. The standard docs cover the basics.
Section of the appsettings.json file.
public class ConfigController : Controller
public readonly IConfiguration _configuration;
public ConfigController(IConfiguration configuration)
_configuration = configuration;
// GET: /Config/Index
public IActionResult Index()
Results of /config/index
Sep 30, 2019 10:22 PM|bruce (sqlwork.com)|LINK
it depends on the configuration reader you are using. most support a watcher, so an update in the file causes a reload, but of course the environment variable reader does not.
in general configuration is handled in ConfigureServices() in startup.cs. In ConfigureServices() your code injects a configuration object, or as in the example above a configuration reader.
You normally pass the configuration object (or the reader) to the constructor of the object needing it. It is generally more convenient to inject this objects in ConfigureServices(), because then you calling does not need to deal with getting the settings.
Normally you'd inject dbcontexts, loggers, email handlers, etc in ConfigureServices().
I always define a application configuration object that I inject:
appSettings = configuration.GetSettings<AppSettings>("ApplicationName");
while doable, what is really a no, no, in asp.net core is code directly reading a configuration file or calling a static method that reads a config file from library code.