Has anyone got this working in a web application?
有人在web应用程序中使用过这个吗?
No matter what I do it seems that my appSettings section (redirected from web.config using appSettings file=".\Site\site.config") does not get reloaded.
无论我做什么,似乎我的appSettings部分(从web重定向)。使用appSettings file=“.\Site\ Site .config”的配置不会被重新加载。
Am I doomed to the case of having to just restart the application? I was hoping this method would lead me to a more performant solution.
我注定要重新启动应用程序吗?我希望这种方法能使我找到一种更有效的解决方案。
Update:
更新:
By 'reloading' I mean refreshing ConfigurationManager.AppSettings without having to completely restart my ASP.NET application and having to incur the usual startup latency.
“重载”的意思是刷新ConfigurationManager。无需重新启动ASP的AppSettings。NET应用程序和通常的启动延迟。
10 个解决方案
#1
46
Make sure you are passing the correct case sensitive value to RefreshSection, i.e.
确保您将正确的案例敏感值传递给RefreshSection,即:
ConfigurationManager.RefreshSection("appSettings");
#2
13
This seems to be a flaw (maybe a bug) when using an external config file for your appSettings. I've tried it using the configSource attribute and RefreshSection simply never works, I'm assuming this is the same when using the file attribute. If you move your appSettings back inside your web.config RefreshSection will work perfectly but otherwise I'm afraid you're doomed.
这似乎是一个缺陷(可能是一个bug),当您在应用程序设置中使用外部配置文件时。我已经尝试过使用configSource属性和RefreshSection,它根本不工作,我假设使用file属性时这是一样的。如果你将你的应用程序设置移回到你的web中。config RefreshSection可以很好地工作,否则我恐怕你就完蛋了。
#3
4
For some reason ConfigurationManager.RefreshSection("appSettings")
wasn't working for me. Reloading the Web.Config into a Configuration object seems to work correctly. The following code assumes the Web.Config file is one directory below the executing (bin) folder.
出于某种原因,ConfigurationManager.RefreshSection(“appSettings”)对我来说行不通。重新加载网页。配置到配置对象中似乎可以正常工作。下面的代码假设是Web。配置文件是执行(bin)文件夹下的一个目录。
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
Uri uriAssemblyFolder = new Uri(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase));
string appPath = uriAssemblyFolder.LocalPath;
configMap.ExeConfigFilename = appPath + @"\..\" + "Web.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
And is used like:
和使用:
string webConfigVariable = config.AppSettings.Settings["webConfigVariable"].Value;
#4
3
As an alternative you could write your own ConfigSection
and set restartOnExternalChanges="false"
.
作为替代方案,您可以编写自己的ConfigSection并设置restartOnExternalChanges="false"。
Then, when reading the section with ConfigurationManager.GetSection("yourSection")
the settings will be auto-refreshed without an application restart.
然后,在使用ConfigurationManager.GetSection(“yourSection”)方法读取这一节时,在不重启应用程序的情况下,将自动刷新这些设置。
And you could implement your settings strongly typed or as NameValueCollection.
可以实现强类型设置或NameValueCollection。
#5
1
.RefreshSection() does not work when the appSettings is external.
. refreshsection()在appSettings是外部时不能工作。
You can however use the following to change a value:
但是,您可以使用以下内容来更改一个值:
ConfigurationManager.AppSettings.Set(key, value)
This will NOT change the setting on file, only the loaded value in memory.
这不会更改文件上的设置,只会更改内存中已加载的值。
So instead of using RefreshSection I did the following:
所以我没有使用RefreshSection,我做了以下的事情:
string configFile="path to your config file";
XmlDocument xml = new XmlDocument();
xml.Load(configFile);
foreach (XmlNode node in xml.SelectNodes("/appSettings/add"))
{
string key = node.Attributes["key"].Value;
string value= node.Attributes["value"].Value;
ConfigurationManager.AppSettings.Set(key, value);
}
Any subsequent calls to AppSettings.Get will contain the updated value.
对AppSettings的任何后续调用。Get将包含更新后的值。
The appSettings will then be updated without needing to restart the application.
然后将更新appSettings,而不需要重新启动应用程序。
#6
0
Yes. you are stuck with iis restarting.
是的。您被困在iis重启中。
There is a feature with asp.net 4.0 and iis 7.5 where the initial startup is removed.
在asp.net 4.0和iis 7.5中有一个特性,可以删除初始启动。
#7
0
I am not sure if this is possible in a web app, but it works in a desktop app. Try using ConfigurationSettings rather than ConfigurationManager (it will yell at you for using outdated classes...), then reading all the data into a class. When you wish to refresh, simply create a new instance and drop all references to the old instance. My theory for why this works (might be wrong): when you don't directly access the app.config file the entire time you are running, the file lock is dropped by the application. Then, edits can be made when you are not accessing the file.
我不确定这在web应用程序中是否可行,但在桌面应用程序中是可行的。当您希望刷新时,只需创建一个新实例并删除对旧实例的所有引用。我对其工作原理的解释(可能是错误的):当您在整个运行过程中不直接访问app.config文件时,应用程序将删除文件锁。然后,可以在不访问文件时进行编辑。
#8
-1
The App.Config settings are cached in memory when the application starts. For this reason, I don't think you'll be able to change those settings without restarting your application. One alternative that should be pretty straight forward would be to create a separate, simple XML configuration file, and handle loading/caching/reloading it yourself.
当应用程序启动时,App.Config设置缓存在内存中。由于这个原因,我认为您无法在不重新启动应用程序的情况下更改这些设置。可以直接创建一个单独的、简单的XML配置文件,并自己处理加载/缓存/重载。
#9
-1
To write, call it this way:
可以这样写:
Dim config As System.Configuration.Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~")
System.Configuration昏暗的配置。配置= System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(“~”)
Return AddOrUpdateAppSetting(config, "YourSettingKey", "YourValueForTheKey")
返回AddOrUpdateAppSetting(配置、“YourSettingKey”、“YourValueForTheKey”)
To read and be sure you get the values in file, instead of those in cache, read it this way:
要读取并确保在文件中而不是缓存中获取值,请按以下方式读取:
Dim config As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration("~")
Return config.AppSettings.Settings("TheKeyYouWantTheValue").Value
Full example:
完整的例子:
Protected Shared Function AddOrUpdateAppSetting( _
ByVal Config As System.Configuration.Configuration _
, ByVal TheKey As String _
, ByVal TheValue As String _
) As Boolean</p>
Dim retval As Boolean = True
Dim Itm As System.Configuration.KeyValueConfigurationElement = _
Config.AppSettings.Settings.Item(TheKey)
If Itm Is Nothing Then
If Config.AppSettings.Settings.IsReadOnly Then
retval = False
Else
Config.AppSettings.Settings.Add(TheKey, TheValue)
End If
Else
' config.AppSettings.Settings(thekey).Value = thevalue
If Itm.IsReadOnly Then
retval = False
Else
Itm.Value = TheValue
End If
End If
If retval Then
Try
Config.Save(ConfigurationSaveMode.Modified)
Catch ex As Exception
retval = False
End Try
End If
Return retval
End Function
#10
-2
Have you tried storing your AppSettings in its own external file?
您是否尝试过将应用程序设置存储在它自己的外部文件中?
From app.config/web.config:
从app.config / web . config:
<appSettings configSource="appSettings.config"></appSettings>
appSettings.config:
appSettings.config:
<?xml version="1.0"?>
<appSettings>
<add key="SomeKey" value="SomeValue" />
</appSettings>
Changes made to appSettings.config should be reflected instantly. More info: http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource.aspx
appSettings更改。配置应该立即反映。更多信息:http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource.aspx。
#1
46
Make sure you are passing the correct case sensitive value to RefreshSection, i.e.
确保您将正确的案例敏感值传递给RefreshSection,即:
ConfigurationManager.RefreshSection("appSettings");
#2
13
This seems to be a flaw (maybe a bug) when using an external config file for your appSettings. I've tried it using the configSource attribute and RefreshSection simply never works, I'm assuming this is the same when using the file attribute. If you move your appSettings back inside your web.config RefreshSection will work perfectly but otherwise I'm afraid you're doomed.
这似乎是一个缺陷(可能是一个bug),当您在应用程序设置中使用外部配置文件时。我已经尝试过使用configSource属性和RefreshSection,它根本不工作,我假设使用file属性时这是一样的。如果你将你的应用程序设置移回到你的web中。config RefreshSection可以很好地工作,否则我恐怕你就完蛋了。
#3
4
For some reason ConfigurationManager.RefreshSection("appSettings")
wasn't working for me. Reloading the Web.Config into a Configuration object seems to work correctly. The following code assumes the Web.Config file is one directory below the executing (bin) folder.
出于某种原因,ConfigurationManager.RefreshSection(“appSettings”)对我来说行不通。重新加载网页。配置到配置对象中似乎可以正常工作。下面的代码假设是Web。配置文件是执行(bin)文件夹下的一个目录。
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
Uri uriAssemblyFolder = new Uri(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase));
string appPath = uriAssemblyFolder.LocalPath;
configMap.ExeConfigFilename = appPath + @"\..\" + "Web.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
And is used like:
和使用:
string webConfigVariable = config.AppSettings.Settings["webConfigVariable"].Value;
#4
3
As an alternative you could write your own ConfigSection
and set restartOnExternalChanges="false"
.
作为替代方案,您可以编写自己的ConfigSection并设置restartOnExternalChanges="false"。
Then, when reading the section with ConfigurationManager.GetSection("yourSection")
the settings will be auto-refreshed without an application restart.
然后,在使用ConfigurationManager.GetSection(“yourSection”)方法读取这一节时,在不重启应用程序的情况下,将自动刷新这些设置。
And you could implement your settings strongly typed or as NameValueCollection.
可以实现强类型设置或NameValueCollection。
#5
1
.RefreshSection() does not work when the appSettings is external.
. refreshsection()在appSettings是外部时不能工作。
You can however use the following to change a value:
但是,您可以使用以下内容来更改一个值:
ConfigurationManager.AppSettings.Set(key, value)
This will NOT change the setting on file, only the loaded value in memory.
这不会更改文件上的设置,只会更改内存中已加载的值。
So instead of using RefreshSection I did the following:
所以我没有使用RefreshSection,我做了以下的事情:
string configFile="path to your config file";
XmlDocument xml = new XmlDocument();
xml.Load(configFile);
foreach (XmlNode node in xml.SelectNodes("/appSettings/add"))
{
string key = node.Attributes["key"].Value;
string value= node.Attributes["value"].Value;
ConfigurationManager.AppSettings.Set(key, value);
}
Any subsequent calls to AppSettings.Get will contain the updated value.
对AppSettings的任何后续调用。Get将包含更新后的值。
The appSettings will then be updated without needing to restart the application.
然后将更新appSettings,而不需要重新启动应用程序。
#6
0
Yes. you are stuck with iis restarting.
是的。您被困在iis重启中。
There is a feature with asp.net 4.0 and iis 7.5 where the initial startup is removed.
在asp.net 4.0和iis 7.5中有一个特性,可以删除初始启动。
#7
0
I am not sure if this is possible in a web app, but it works in a desktop app. Try using ConfigurationSettings rather than ConfigurationManager (it will yell at you for using outdated classes...), then reading all the data into a class. When you wish to refresh, simply create a new instance and drop all references to the old instance. My theory for why this works (might be wrong): when you don't directly access the app.config file the entire time you are running, the file lock is dropped by the application. Then, edits can be made when you are not accessing the file.
我不确定这在web应用程序中是否可行,但在桌面应用程序中是可行的。当您希望刷新时,只需创建一个新实例并删除对旧实例的所有引用。我对其工作原理的解释(可能是错误的):当您在整个运行过程中不直接访问app.config文件时,应用程序将删除文件锁。然后,可以在不访问文件时进行编辑。
#8
-1
The App.Config settings are cached in memory when the application starts. For this reason, I don't think you'll be able to change those settings without restarting your application. One alternative that should be pretty straight forward would be to create a separate, simple XML configuration file, and handle loading/caching/reloading it yourself.
当应用程序启动时,App.Config设置缓存在内存中。由于这个原因,我认为您无法在不重新启动应用程序的情况下更改这些设置。可以直接创建一个单独的、简单的XML配置文件,并自己处理加载/缓存/重载。
#9
-1
To write, call it this way:
可以这样写:
Dim config As System.Configuration.Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~")
System.Configuration昏暗的配置。配置= System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(“~”)
Return AddOrUpdateAppSetting(config, "YourSettingKey", "YourValueForTheKey")
返回AddOrUpdateAppSetting(配置、“YourSettingKey”、“YourValueForTheKey”)
To read and be sure you get the values in file, instead of those in cache, read it this way:
要读取并确保在文件中而不是缓存中获取值,请按以下方式读取:
Dim config As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration("~")
Return config.AppSettings.Settings("TheKeyYouWantTheValue").Value
Full example:
完整的例子:
Protected Shared Function AddOrUpdateAppSetting( _
ByVal Config As System.Configuration.Configuration _
, ByVal TheKey As String _
, ByVal TheValue As String _
) As Boolean</p>
Dim retval As Boolean = True
Dim Itm As System.Configuration.KeyValueConfigurationElement = _
Config.AppSettings.Settings.Item(TheKey)
If Itm Is Nothing Then
If Config.AppSettings.Settings.IsReadOnly Then
retval = False
Else
Config.AppSettings.Settings.Add(TheKey, TheValue)
End If
Else
' config.AppSettings.Settings(thekey).Value = thevalue
If Itm.IsReadOnly Then
retval = False
Else
Itm.Value = TheValue
End If
End If
If retval Then
Try
Config.Save(ConfigurationSaveMode.Modified)
Catch ex As Exception
retval = False
End Try
End If
Return retval
End Function
#10
-2
Have you tried storing your AppSettings in its own external file?
您是否尝试过将应用程序设置存储在它自己的外部文件中?
From app.config/web.config:
从app.config / web . config:
<appSettings configSource="appSettings.config"></appSettings>
appSettings.config:
appSettings.config:
<?xml version="1.0"?>
<appSettings>
<add key="SomeKey" value="SomeValue" />
</appSettings>
Changes made to appSettings.config should be reflected instantly. More info: http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource.aspx
appSettings更改。配置应该立即反映。更多信息:http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource.aspx。