2008-10-07 84 views
36

有没有人得到这个在Web应用程序中的工作?重新加载配置,而无需使用ConfigurationManager重新启动应用程序。刷新段

无论我做什么,似乎我的appSettings部分(从web.config重定向使用appSettings文件=“。\ Site \ site.config”)不会重新加载。

我注定不得不重新启动应用程序的情况吗?我希望这种方法能够让我获得更高性能的解决方案。

更新:

通过“重装”我的意思是清爽ConfigurationManager.AppSettings而无需完全重新启动我的ASP.NET应用程序,并且承担平时的启动延迟。

回答

45

确保您传递正确的case sensitive值RefreshSection,即

ConfigurationManager.RefreshSection("appSettings"); 
-1

当应用程序启动时,App.Config设置被缓存在内存中。出于这个原因,我认为你不用重新启动你的应用程序就可以改变这些设置。另一个应该非常简单的方法是创建一个单独的简单XML配置文件,并自行处理加载/缓存/重新加载。

+0

那么什么是http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.refreshsection.aspx? – 2008-10-07 16:43:57

+0

好问题 - 我不知道:) – Seibar 2008-10-07 17:00:05

+0

@Terrapin:请尝试按照KieranBenton建议的RefreshSection方法,它的工作原理。在这个线程中看到我的答案:http://stackoverflow.com/questions/272097/net-dynamically-refresh-app-config – dotnetguy 2012-07-11 06:08:01

13

当你的appSettings使用外部配置文件时,这似乎是一个缺陷(也许是一个bug)。我已经尝试过使用configSource属性和RefreshSection根本无法工作,我假设这是使用文件属性时相同。 如果您将appSettings移回到您的web.config中,RefreshSection将完美运行,否则恐怕您注定要失败。

+3

我也注意到了。任何解决方案 – Martin 2016-01-08 11:29:56

-1

要编写,按此方法调用:

昏暗配置作为System.Configuration.Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration( “〜”)

返回AddOrUpdateAppSetting(配置,“YourSettingKey ”,‘YourValueForTheKey’)

要阅读,并确保你得到的值文件,而不是那些在高速缓存中,读这样说:

Dim config As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration("~") 
    Return config.AppSettings.Settings("TheKeyYouWantTheValue").Value 

完整的示例:

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 
+0

标签说C#不是vb.net – 2015-07-06 14:57:35

0

是的。你坚持iis重启。

有一个与asp.net 4.0和iis 7.5中的初始启动被删除的功能。

0

我不确定这是否可以在网络应用程序中使用,但它适用于桌面应用程序。尝试使用ConfigurationSettings而不是ConfigurationManager(它会对您使用过时的类进行大肆宣传......),然后将所有数据读入一个类。当你想刷新时,只需创建一个新实例并删除对旧实例的所有引用。我的理论是为什么这个工作原理(可能是错误的):当你在运行的整个过程中没有直接访问app.config文件时,文件锁被应用程序删除。然后,可以在您未访问文件时进行编辑。

3

作为替代方案,您可以编写自己的ConfigSection并设置restartOnExternalChanges="false"

然后,当阅读ConfigurationManager.GetSection("yourSection")部分时,设置将为自动刷新,无需重新启动应用程序

你可以实现你的设置强类型或NameValueCollection。

4

由于某种原因ConfigurationManager.RefreshSection("appSettings")不适合我。将Web.Config重新加载到配置对象似乎正常工作。以下代码假定Web.Config文件是执行(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); 

,并使用类似:

string webConfigVariable = config.AppSettings.Settings["webConfigVariable"].Value; 
1

.RefreshSection()时的appSettings是外部不工作。

但是,您可以使用以下方法来改变值:

ConfigurationManager.AppSettings.Set(key, value) 

这不会在内存中改变文件中的设置,只加载的值。

因此,而不是使用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); 
} 

到AppSettings.Get任何后续调用将包含更新后的值。

appSettings将被更新,无需重新启动应用程序。

相关问题