Reputation: 371
I have a CSharp windows service running on windows 7. It instantiates a dll from a framework for devices management. This dll has some settings stored in a app.config called 'pm.config'.
I copied all the assemblies and the pm.config files to a folder called 'bin' at the same level of the winsvc project folder.
I added references to the dll framework and compiled the project. The pm.config file exists in the 'bin\debug' and 'bin\release' subfolders. I installed the winsvc using installutil and started the winsvc. Everything went fine except that when the winsvc calls the 'Initialize' method from framework dll, this method tries to open the pm.config file and generates the following exception:
InnerException = {"The machine.config file 'pm.config' was not found.\r\nParameter name: machineConfigFilename"}
I've inspected the assembly location using Assembly.GetExecutingAssembly().Location
and it points to the correct folder of the winsvc project, I mean 'mysvcproject\bin\debug' folder.
I pretty sure this is something related to security context of the winsvc, but I have no idea. I'm using LocalService account to start the winsvc and the 'bin\debug' doesnt have right for this account. Instead I gave full control to the everyone account on this folder, but still got the error.
I'm completely lost, can anybody help me?
Thanks in advance
Upvotes: 3
Views: 5833
Reputation: 9893
I had success combining Daniel's solution with this solution: https://stackoverflow.com/a/7262937/771473
Configuration cfg = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
String MyValue = cfg.AppSettings.Settings["MyKey"].Value;
Upvotes: 1
Reputation: 4363
The executing application uses its own app.config, not the one in your referenced dll. You should create an app.config in the executing assembly and put your settings there.
Upvotes: 0
Reputation: 2233
I've had similar problems not long ago when deploying a Windows service. The problem then was that the Windows service expected the file to be in c:\windows\system32. Instead of using Assembly.GetExecutingAssembly().Location to inspect, try Directory.GetCurrentDirectory() and see if that yields the same result. If not, try putting the pm.config file in the current directory.
Upvotes: 3
Reputation: 7473
The App.config filename must match that of your Windows Service executable, with ".config" apptended. So if your service is "pm.exe", your config file should be "pm.exe.config".
Upvotes: 1