RefreshSection无法按预期工作

时间:2017-03-12 13:49:59

标签: c# app-config configurationmanager

我正在尝试更新正在运行的应用程序的app.config文件。更新确实有效(即文件更新),但是当我重新读取文件时,它会显示旧值。 this问题的答案确实意味着app.config被缓存,但调用RefreshSection会强制重新读取。

这是app.config(直接来自MS示例):

<configuration>      
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <system.serviceModel>  
        <bindings>  
            <wsHttpBinding>  
                <binding name="WSHttpBinding_ICalculator" />  
            </wsHttpBinding>  
        </bindings>  
        <client>  
            <endpoint address="http://localhost:8000/ServiceModelSamples/Service/CalculatorService"  
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICalculator"  
                contract="ServiceReference1.ICalculator" name="WSHttpBinding_ICalculator">  
                <identity>  
                    <userPrincipalName value="migree@redmond.corp.microsoft.com" />  
                </identity>  
            </endpoint>  
        </client>      
    </system.serviceModel>  
</configuration>

以下是我用来更新控制台应用的代码:

static void Main(string[] args)
{
    Console.WriteLine("Before change");            
    ShowConfig();

    Console.WriteLine("Change");
    ChangeConfig();

    Console.WriteLine("After change");
    ShowConfig();            

    Console.ReadLine();
}

private static void ChangeConfig()
{            

    Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    string appConfig = File.ReadAllText(configuration.FilePath);
    appConfig = appConfig.Replace("localhost:8000", "myAddress.this.com:8080");
    File.WriteAllText(configuration.FilePath, appConfig);
    configuration.Save(ConfigurationSaveMode.Modified);

    ConfigurationManager.RefreshSection("endpoint");
    ConfigurationManager.RefreshSection("client");
    ConfigurationManager.RefreshSection("system.serviceModel");
    ConfigurationManager.RefreshSection("configuration");

}

private static void ShowConfig()
{
    ClientSection clientSection =
        ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;

    ChannelEndpointElementCollection endpointCollection =
        clientSection.ElementInformation.Properties[string.Empty].Value as ChannelEndpointElementCollection;

    foreach (ChannelEndpointElement endpointElement in endpointCollection)
    {
        Console.WriteLine(endpointElement.Address);
    }
}

文件确实会更新,因为我可以在程序运行时在文本编辑器中看到...但控制台显示相同的值读取两次。我在RefreshSection上看到的大多数内容似乎暗示它更多地与appSettings有关,尽管我还没有看到任何直截了当的东西。是否有可能在我尝试的时候在app.config上刷新?

2 个答案:

答案 0 :(得分:3)

您需要添加:

ConfigurationManager.RefreshSection("system.serviceModel/client");

您对RefreshSection的调用需要引用从System.Configuration.ConfigurationSection继承的所有内容。

答案 1 :(得分:1)

此解决方案取代了自己编写文件并使用Configuration对象代替在指定部分进行更改:

private static void ChangeConfig()
{
    Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    var section = configuration.GetSection("system.serviceModel/client");

    if(section != null)
    {
        var xmlString = section.SectionInformation.GetRawXml();
        xmlString = xmlString.Replace("localhost:8000", "myAddress.this.com:8080");
        section.SectionInformation.SetRawXml(xmlString);

        configuration.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection(section.SectionInformation.SectionName);
    }
}