Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Worker Service looking for appsettings.json in System32 folder instead of local folder

I have a .Net core worker service, which I am running as a windows service. The service is using appsettings.json file for the config information. After installing the service using SC CREATE command, the service was failing.

In the event viewer I found the error it cannot find the file C:\Windows\System32\appsettings.json. My service files are placed in a different folder c:\Services\, instead of looking at that location, the service is looking for a the file in System32 folder.

The configuration registration is as below.

 var configBuilder = new ConfigurationBuilder()
                   .SetBasePath(Directory.GetCurrentDirectory())
                   .AddJsonFile("appsettings.json");
var configuration = configBuilder.Build();
            services.AddSingleton(configuration);

How can I make the service to look at the local folder?

like image 963
Ajith Avatar asked Dec 04 '19 09:12

Ajith


People also ask

Where is Appsettings json located?

appsettings. json is one of the several ways, in which we can provide the configuration values to ASP.NET core application. You will find this file in the root folder of our project.

Can I move Appsettings json out of the app directory?

Yes, I'm leaving appsettings. json in the project and it's being deployed - for documentation and non-sensitive settings. And, yes, I could leave sensitive data encrypted in Visual Studio Online but for now, the simplest thing to do is hide an overriding version elsewhere in the file system, as I'm doing.

What should I store in Appsettings json?

The appsettings. json file is generally used to store the application configuration settings such as database connection strings, any application scope global variables, and much other information.

How do I run Appsettings json at startup?

In order to add AppSettings. json file, right click on the Project in Solution Explorer. Then click Add, then New Item and then choose App Settings File option (shown below) and click Add button. Once the File is created, it will have a DefaultConnection, below that a new AppSettings entry is added.


2 Answers

That's because the current directory is changed to C:\Windows\System32 at runtime. You could get a relative path by Assembly.GetExecutingAssembly(). For example:

var configBuilder = new ConfigurationBuilder()
    .SetBasePath( Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location))
    .AddJsonFile("appsettings.json");
var configuration = configBuilder.Build();
like image 175
itminus Avatar answered Sep 17 '22 23:09

itminus


I´ve successfully used the docs in Host ASP.NET Core in a Windows Service

In a nutshell, you should just add .UseWindowsService() in your configuration builder step, like in the following:

Host.CreateDefaultBuilder(args)
    .UseWindowsService()
    .ConfigureAppConfiguration((hostContext, configuration) => 
        {
            //...set your configurations here
        })
        .ConfigureServices((hostContext, services) =>
        {
            //...configure you services here
        }
like image 34
vieira42 Avatar answered Sep 16 '22 23:09

vieira42