2017-03-12 49 views
1

実行中のアプリケーションのapp.configファイルを更新しようとしています。更新は機能します(つまり、ファイルが更新されます)。ただし、ファイルを再読み込みすると古い値が表示されます。 this質問への答えは、app.configがキャッシュされていることを意味しますが、RefreshSectionを呼び出すと再読み込みが強制されます。RefreshSectionが正常に動作しない

ここで(ストレートMSの例から)app.configをです:

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); 
    } 
} 

ファイル:

<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="[email protected]" /> 
       </identity> 
      </endpoint> 
     </client>  
    </system.serviceModel> 
</configuration> 

そして、ここでは、私はそれを更新するために使用しているコンソールアプリケーションのコードですプログラムが実行されている間にテキストエディタで見ることができるので更新されますが、コンソールには同じ値が2回読み込まれることが表示されます。私がRefreshSectionで見たことのほとんどは、appSettingsに関連していることを暗示しているようです。私がしようとしているようにapp.configでリフレッシュさせることは可能ですか?

答えて

3

あなたは追加する必要があります。RefreshSectionへ

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

あなたの呼び出しはSystem.Configuration.ConfigurationSectionから継承するすべてのものを参照する必要があります。

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); 
    } 
}