How to update appSetting for a Background Service (Service Worker) (.NET 6)

tma9 21 Reputation points
2023-03-28T12:34:00.33+00:00

Hello, I came to ask you for your help, please.

I have created a Worker Service which periodically deletes files in a folder. My path to the folder is stored in an App.config file, I read the value via ConfigurationManager. It works, but I want to be able to create the Service, run it in a background, and change the settings without stopping the service, or at least without the need to create the service again.

Is it possible?

I have tried the RefreshSection function, but it´s not working.

Should I be updating the file in the StartAsync()?

I want to change the file manually, it´s possible that I am not updating the right file, but where can the right file be found?

The service is working OK and logging the information, so I can see that it is using the (wrong) path I gave it in the beginning.

Worker.cs/StartAsync

  System.Configuration.ConfigurationManager.RefreshSection("appSettings");
System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings["sourceDirectory"].Value = "newPath";
            config.AppSettings.SectionInformation.ForceSave = true;
            config.Save(ConfigurationSaveMode.Modified);
            System.Configuration.ConfigurationManager.RefreshSection("appSettings");

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<appSettings>
		<add key="sourceDirectory" value="C:\Users\User\Folder"/>
	</appSettings>
</configuration>

I create the service like this:

SC CREATE "MyService" binPath= "C:\Users\User\source\repos\MyService\MyService\bin\Release\net6.0\publish\MyService.exe" start= auto displayname= "My Service"

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,202 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rena Ni - MSFT 2,061 Reputation points
    2023-03-29T05:23:55.5033333+00:00

    Hi @tma9 ,

    MyService.exe is the name of the file. MyService.dll.config is the runtime app.config for the service. It should be deployed with the MyService.exe. MyService.dll.config is the file you should be updating. When you built that service successfully, the MyService.dll.config should be in the bin/debug folder with the exe. You can use Notepad and view the config file. The dll.config file should be deployed to the same location as the MyService.exe is located(in your scenraio should be C:\Users\User\source\repos\MyService\MyService\bin\Release\net6.0\publish) so .NET can find it. And when you modify the key value by RefreshSection, the MyService.dll.config will be updated.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,

    Rena

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful