Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core 3.0 executable doesn't read appsettings.json

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; }
}
like image 210
Matthew Avatar asked Sep 28 '19 16:09

Matthew


People also ask

Why can't I find the JSON file for my application?

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.

What is the targetframework and runtimeidentifier of Netcore APP2?

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:

What is the default configuration file in ASP NET Core?

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.

How to read configuration settings from appsettings using dependency injection?

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.


1 Answers

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;
}
like image 170
Nkosi Avatar answered Nov 15 '22 08:11

Nkosi