Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allowing a Windows Service to be configured

I have a Windows Service that I'm creating and I'm wondering what options are available in order for me let developers configure the service.

The service is part of an over all larger open source project and hence the service is going to be installed on lots of different machines.

Normal I would use a web/app.config for this but I'm not sure if this is possible.

Hence I am looking to so how others handle this case.

like image 430
vdh_ant Avatar asked Apr 25 '12 10:04

vdh_ant


People also ask

How do I give permission to Windows services?

To configure permissions for a new user or group, click Add. In the Select Users, Computers, or Groups dialog box, type the name of the user or group that you want to set permissions for, and then click OK. In the Permissions for User or Group list, configure the permissions that you want for the user or group.

How do I configure Windows services?

Press the keys Windows logo+R to open the “Run” dialog. Enter services. msc to open the Window services configuration tool. Configure each new Windows service following the steps of the section above.

How do I check Windows service permissions?

To see the Service permissions you can use the "sc" command from a Windows command-line prompt. To compare permissions for a particular Service, run it on two systems. See the outputs and compare each line in a notepad/wordpad session.


2 Answers

you do as you expect. You use the app.config, which will be renamed to <exeName>.configwhen the project is built and then <exeName>.config will be read by the service called <exeName>.

Settings are applied in a layered way and may come from other configuration files on the machine, such as machine.config. You can read about how configuration is handled on MSDN

EDIT

In response to comment: A service will only read the config when it starts (for perf reasons). If you want to reload the config file later, you need to handle that yourself I think.

You could read the last modified date/time of the config file to determine if the file has been changed, or setup a file system watcher and then tell the configuration manager to reload that section again next time it is read, by calling ConfigurationManager.RefreshSection("appSettings") and that section will be reloaded from disk when you next access it. See the ConfigurationManager MSDN docs

like image 135
Sam Holder Avatar answered Oct 10 '22 02:10

Sam Holder


You can just use a .config file with the same name as the exe that is the service.

If your service runs as MyService.exe, it's config file would be MyService.exe.config.

In Visual Studio, just add an Application Configuration file. This will add an app.config file to the project.

You can then access things like AppSettings and ConnectionStrings using the ConfigurationManager class, just like you do with ASP.Net applications.

like image 26
Tomas McGuinness Avatar answered Oct 10 '22 01:10

Tomas McGuinness