Changing .NET application settings without recompiling
Asked Answered
B

3

6

I have a windows service that is referencing another assembly (class library). In this other assembly I used Application Settings to store some values. When I copy all the required files to the server I can see these settings in AssemblyName.dll.config file.

However, when I change a setting in this config file and restart the service the change has no effect. Even if I uninstall/reinstall the service it still returns the old value at runtime.

Config file:

   <setting name="RecordLimit" serializeAs="String">
     <value>300</value>
   </setting>

Code:

if (recordCount > Settings.Default.RecordLimit) //always 300

So even if I change the value in the config file to 400 and restart or even reinstall the service, the value is always 300 making me think that this value is stored in and returned from the compiled code.

What am I doing wrong and is it possible that changes to application settings always require a recompile and reinstall (I understand that I can use Save() method to change the settings from code but this is a windows service so using this method doesn't seem to make sense)?

If these settings stored in the config file have no effect and the settings are stored in the compiled code, can I safely remove these config files?

If these settings cannot be changed without recompiling what other options I have to store setting that I can change without recompling?

EDIT: I just removed the AssemblyName.dll.config file and the code returned 300 so the config file is irrelevant apparently.

Blowpipe answered 5/7, 2012 at 20:57 Comment(0)
O
2

As far as I know how config files work, it has to be at the EXE level: appname.exe.config for services/apps and web.config for web sites/services.

See question/answers at Can someone provide a quick App.config/Web.config tutorial? for reference.

Osmond answered 5/7, 2012 at 21:29 Comment(14)
Using Application Settings, any assembly can have its own settings and the config file...Blowpipe
Does that mean a DLL being run as an executable will automatically ignore its config file?Helical
@DeanK. I have never seen such a thing. Yes, it has a config file, but it won't be used.Osmond
@Monkieboy what do you mean a DLL being run as an executable? No such thing. Executables run as executables. And sometimes load DLLs as they need to use their functionality. Despite having a settings page in VS, settings are loaded at the executable level.Osmond
@JesseC.Slicer. So where is the value 300 coming from? It is only stored in the application settings of the class library...Blowpipe
@DeanK. that bit becomes a chunk of automatically-generated code which is compiled with the app.Osmond
Here's another good link on why this is not recommended and how to do it: https://mcmap.net/q/127024/-c-dll-config-fileOsmond
DLL is an executable without an entry point so the code from a DLL can only be executed via a call from an executable with an entry point (typically an .exe file)...Blowpipe
@jesse I thought it was possible for DLL to be executed directly, I am almost definite I have seen them being run in the process manager..... Maybe I am using the wrong terminology?Helical
@JesseC.Slicer. The value is actually stored in AssemblyName.dll, not the executable. I just opened the compiled AssemblyName.dll dll file in Reflector and I found the value compiled in there in AssemblyName.Properties namespace. There is a Settings class in that namespace and it has all the settings that are in the config file as well.Blowpipe
@DeanK. yes, that's precisely what I'm saying. The value is compiled into the DLL, but to get it out of the .config file, it'll have to be renamed from BlahBlah.dll.config to Host.exe.config.Osmond
So what is the point of the config, is it possible to make the assembly without compiling the settings in?Helical
@Jesse - Aren't our answers the same in that case, in that the settings should be in the apps config, or are you suggesting something different?Helical
@Monkieboy Our answers are similar enough to be considered the same for this instance.Osmond
H
0

Can the exe that references the DLL manipulate the configuration settings in its own config?

You should try adding the settings to the exe.config as a test.

Helical answered 5/7, 2012 at 21:24 Comment(3)
It's a windows service so building a UI that would communicate with the windows service would require windows service to expose some type of public interface. But even if this was possible, it wouldn't make much sense to go through all that trouble to change simple settings... besides, this code is executed from the assembly referenced by the service executable, not from the service executable itself.Blowpipe
Sorry either I am misunderstanding you or I have not explained what I mean properly. What I mean is the appname.exe that the service is running has its own config, I thought (i may be wrong) the appname.exe.config could affect, via config settings, any of its referenced DLL's settings. I didnct mean for a UI to manipulate, just notepad on the appname.exe.config and service restart. Sorry if I am insulting your intelligence.Helical
Ahh I see, you mentioned it is the DLL being run as an executableHelical
H
0

I think it might be that the .config.dll file that is actually being used is somewhere in C:\Users\Username\AppData (in Win7). Try searching for AssemblyName.dll in that folder, and enough looking around should find it.

Huldahuldah answered 5/7, 2012 at 21:28 Comment(3)
The service is installed into C:\Services\ServiceName folder using installutil.exe. Do you think that even when installed like that, it would create a copy of the config file in the AppData folder and use that instead of the config file that is in the installation folder?Blowpipe
I did have a similar problem with this myself, and I'm pretty sure this is how I got it fixed. I know it creates some sort of config file in AppData.Huldahuldah
I just opened the compiled DLL in Reflector type tool and found that the values are compiled in there in the Settings class. Apparently these values are being used at runtime and the values from the config file are ignored. Hence, looks like the only way to manipulate these values is to change the settings in the code and call the Save() method.Blowpipe

© 2022 - 2024 — McMap. All rights reserved.