0

I've a simple C# windows service with an app config which contains a custom section mapped to my class. Also, this class is located in a different assembly from my executable, however the assembly is in the same folder of my executable. What I'm trying to do is to install the service through installutil utility, then manage OnAfterInstall event in order to do some manipulation to my app config at runtime.

However, I always get an exception when I try to get section of my class:

ConfigurationManager.GetSection("myCustomSection");

The exception says me the application is unable to find type definition of my class (reference missing or else file not found exeception (which is in an another assembly, but it's in the same folder as my service).

What I note is that in OnAfterInstall (called by installutil) the environment.currentDirectory is C:\windows\microsoft.NET\framework\v40.. and I think that's is the cause of the problem. The app domain is different from what I expected and most likely that's the issue.

In short, it seems impossible read and write section of my app config if I try to do that in OnAfterInstall event of service installer. Is there any workaround?

Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105
bit
  • 934
  • 1
  • 11
  • 32
  • The app.config is common in many scenario such as windows service, console app, wpf app and so on. so, my custom config class is in another assembly. – bit Nov 03 '14 at 19:31
  • I've solved the problem with following stackoverflow post: http://stackoverflow.com/questions/1682681/custom-config-section-could-not-load-file-or-assembly – bit Nov 03 '14 at 20:15

1 Answers1

-1

I know you should have already considered this, but as writing configuration information for my Windows services, I never write on files. Specially considering it shares information with another app/assembly. I rather always writing configurations on windows registry and then loading it at app/service startup.

Also, during install, you could set these settings. This would be placed in an area which most users don't change it, and also would make it safer depending on where you place the keys (only users with installation permissions would be able to write / delete entries).

A second option, and the second one I like the best, is creating another class (xml serializable) as your configuration information. You then can read/write this file, and would also be very readable to tech users. You could set where this appconfig.xml is saved at a user/system windows registry entry.

rodrigogq
  • 1,943
  • 1
  • 16
  • 25