问题
Do you have to restart a windows service if you change the app.config?
回答1:
Yes, you do:
.Net framework will read the app.config once, and never touch the app.config again. That is why you have to restart the application to pick up the change.
Why does not .Net framework detect that app.config has changed, and refresh all the config data?
The reason is simple, this is not possible in general.
Let’s use Raymond’s “imagine this could be done” logic here...
回答2:
I've used this before. Essentially this reloads the specified section before retrieving the value. Quite possibly less efficient than a config file watcher that refreshes on demand, but certainly effective used carefully.
ConfigurationManager.RefreshSection("appSettings")
sValue = ConfigurationManager.AppSettings(name)
回答3:
No, you don't have to. But if you want for changes in app.config
to take effect, you might need to restart it. Or you might want to implement a custom configuration file watcher mechanism which would alter services' settings on the fly.
回答4:
Yes.
Presumably you mean the appname.exe.config file?
回答5:
Usually yes. However you could write a windows service that would put a file monitor on the configuration file and sense a modification and apply it without the need to restart.
For example the Log4Net logging framework offers such a mechanism for its configuration file.
回答6:
This goes way out of the scope of the question, but I would guess you want to make changes only in some "application-specific" section (appSettings, etc.).
You are better off writing your own config section handler, which monitors it's own config file - the approach nlog uses. Check the section "Automatic reconfiguration".
来源:https://stackoverflow.com/questions/590791/do-you-have-to-restart-a-windows-service-if-you-change-the-app-config