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!
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.
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:
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.
None
0 Points
12 Posts
System.Configuration.ApplicationSettingsBase default/configuration in .NET Core 2
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!
All-Star
52221 Points
23295 Posts
Re: System.Configuration.ApplicationSettingsBase default/configuration in .NET Core 2
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.
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.0
Section of the appsettings.json file.
Controller
Results of /config/index
All-Star
57874 Points
15507 Posts
Re: System.Configuration.ApplicationSettingsBase default/configuration in .NET Core 2
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");
....
services.AddSingleton<AppSettings>(appSettings);
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.