I'm writing a simple .Net Core 3.0 console application and publishing as a single executable.
I figured out how to exclude the appsettings.json
file, but when I run the application it uses the original settings and doesn't honor changes I make to the `appsettings.json file.
Perhaps when I run the executable it is copying the original appsettings.json
file to a temp folder somewhere and reading that rather than the file at the original location?
If so, where is that temp location when running on Debian Linux?
static void Main(string[] args)
{
private static MySettings settings = new MySettings();
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", false, true)
.Build();
config.GetSection("Settings").Bind(settings);
rootPath = settings.RootPath;
}
public class MySettings
{
public int Buffer { get; set; }
public string RootPath { get; set; }
public int FrequencySeconds { get; set; }
}
In your example, you're running the application from outside of its own directory, which means the .json files are not being found. To resolve this, you can first cd into path/To/My and then run App.exe from there.
The TargetFramework is netcoreapp2.2. The RuntimeIdentifier is win-x64. And the Environment is Staging. When running the app through the command line for some testing, it doesn't seem to be reading the appsettings.staging.json or any of the appsettings.json files. For testing purposes I set the Configure Method of Startup.cs as follows:
ASP.NET Core allows for a configuration file to be set up that can be read through the application. By default, the appsettings.json file allows for app settings to be configured for a web application. We will have a look at the different ways to read the app configuration file into an ASP.NET Core application.
Now you are all set to read configuration settings from appsettings.js through dependency injection where ever you need it. You can inject the dependency of IGeekConfigManager through the HomeController constructor. Open HomeController and add constructor as shown.
Try setting the base path to the working directory
.SetBasePath(Directory.GetCurrentDirectory())
Also, the binding can be done using .Get<T>()
on the section
static void Main(string[] args) {
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory()) //<--
.AddJsonFile("appsettings.json", false, true)
.Build();
MySettings settings = config.GetSection("Settings").Get<MySettings>();
rootPath = settings.RootPath;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With