Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

windows service not reading app.config

Tags:

c#

iis

service

I know this maybe a duplicate, however all the posts so far cannot solve my issue.

I have a windows service, and I want this service to read from web.config. However, it keeps reading from machine.config. I have tried a lot of settings suggestions from many sites including SO, such as <appSettings file="location of web.config".

Do I need to reinstall after each change of the app.config? I have service.exe.config in the same folder as service.exe. What else I might have missed?

Another note, I have 2 websites in the IIS. How do I make sure that the window service takes the web.config that I want?

Some codes here to show how I did it:

private string uDir = ConfigurationManager.AppSettings["dir1"] + "\\dir1";
private string fDir= ConfigurationManager.AppSettings["dir2"] + "\\dir2";
private static string cs= ConfigurationManager.ConnectionStrings["cSqlServer"].ConnectionString.ToString();
private SqlConnection con = null;

The all the ConfigurationManager returns nothing, except ConnectionStrings which returns the value from machine.config.

like image 959
Innovit Avatar asked Jun 02 '26 09:06

Innovit


1 Answers

Try rerunning Installutil tool on the service.

Unless your service periodically checks for changes in the app.exe.config file, it will not reload the config file until the service is reinstalled.

If you replace the installed app.exe.config file in the installation location and restart the service, it should pick up the new version too.

like image 85
jordanhill123 Avatar answered Jun 03 '26 23:06

jordanhill123