无需重新启动应用程序即可更改应用程序配置

发布于 2024-11-02 23:43:14 字数 1417 浏览 0 评论 0原文

我有以下问题:我正在将新功能引入应用程序(作为 Windows 服务运行),并且我希望使用某种配置文件条目(myKey)来控制(开/关)该新功能)。我可以将配置条目存储在 app.config 中,但如果我想从开->关状态更改它,否则需要重新启动 Windows 服务,我想避免它。我希望我的应用程序能够运行并获取配置中的更改。

问题是:.NET 中是否有内置机制可以解决该问题?我想我可以创建自己的配置文件,然后使用 FileSystemWatcher 等...但也许 .NET 允许使用外部配置文件并会重新加载值?

ConfigurationManager.AppSettings["myKey"]

谢谢,Pawel

编辑1:感谢您的回复。但是我写了以下代码片段,但它不起作用(我尝试在两个地方创建 appSettingSection:循环之前和内部):

static void Main(string[] args)
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    // AppSettingsSection appSettingSection = (AppSettingsSection)config.GetSection("appSettings");
    for (int i = 0; i < 10; i++)
    {
        ConfigurationManager.RefreshSection("appSettings");
        AppSettingsSection appSettingSection = (AppSettingsSection)config.GetSection("appSettings");
        string myConfigData = appSettingSection.Settings["myConfigData"].Value; // still the same value, doesn't get updated
        Console.WriteLine();
        Console.WriteLine("Using GetSection(string).");
        Console.WriteLine("AppSettings section:");
        Console.WriteLine(
          appSettingSection.SectionInformation.GetRawXml()); // also XML is still the same
        Console.ReadLine();
    }
}

当应用程序在 Console.ReadLine() 上停止时,我手动编辑配置文件

I have following issue: I'm introducing new feature into application (run as Windows Service) and I would like to have that new feature controlled (on/off) using some kind of configuration file entry (myKey). I can store config entry in app.config but if I want to change it from on->off or otherwise it would require to restart Windows Service, and I want to avoid it. I want my application to run and pick up changes in configuration.

The question is: is there a build in mechanism in .NET that addresses that problem ? I guess I could create my own config file, then use FileSystemWatcher etc... But perhaps .NET allows to use external config files and will reload value ??

ConfigurationManager.AppSettings["myKey"]

Thanks, Pawel

EDIT 1: Thanks for replies. However I wrote following snippet and it doesn't work (I tried creating appSettingSection in both places: before and inside loop):

static void Main(string[] args)
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    // AppSettingsSection appSettingSection = (AppSettingsSection)config.GetSection("appSettings");
    for (int i = 0; i < 10; i++)
    {
        ConfigurationManager.RefreshSection("appSettings");
        AppSettingsSection appSettingSection = (AppSettingsSection)config.GetSection("appSettings");
        string myConfigData = appSettingSection.Settings["myConfigData"].Value; // still the same value, doesn't get updated
        Console.WriteLine();
        Console.WriteLine("Using GetSection(string).");
        Console.WriteLine("AppSettings section:");
        Console.WriteLine(
          appSettingSection.SectionInformation.GetRawXml()); // also XML is still the same
        Console.ReadLine();
    }
}

I manually edit config file when application stops on Console.ReadLine().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜扑 2024-11-09 23:43:14

一旦加载了原始的 app.config 文件,它的值就会被缓存,所以如您所知,您必须重新启动应用程序。解决这个问题的方法是创建一个新的配置对象并手动读取密钥,如下所示:

var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
string myConfigData = appConfig.AppSettings.Settings["myConfigData"].Value;

Once the original app.config file is loaded, it's values are cached so as you know, you'll have to restart the app. The way around this is to create a new config object and read the keys manually like this:

var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
string myConfigData = appConfig.AppSettings.Settings["myConfigData"].Value;
爱殇璃 2024-11-09 23:43:14

如果您手动处理配置(可能甚至不在 app.config 文件中),那么您可以定期检查该文件是否有更新。

FileSystemWatcher 是...可能有点矫枉过正,并且不能保证在所有情况下都有效。就我个人而言,我只是每(比如)30 秒轮询一次文件。

If you process the configuration manually (perhaps not even in the app.config file), then you can periodically check that file for updates.

FileSystemWatcher is... probably overkill, and isn't guaranteed in all scenarios. Personally, I'd just poll the file every (say) 30 seconds.

时光暖心i 2024-11-09 23:43:14

在读取值之前,只需刷新当前配置文件中的“appSettings”即可:

ConfigurationManager.RefreshSection("appSettings"); // Reload app settings from config file
ConfigurationManager.AppSettings["myKey"];          // Read the value as usually

无需通过创建另一个配置或配置或您的问题或其他答案中所建议的内容来使其变得过于复杂!

Simply refresh the "appSettings" from current config file before you read the value:

ConfigurationManager.RefreshSection("appSettings"); // Reload app settings from config file
ConfigurationManager.AppSettings["myKey"];          // Read the value as usually

No need to over complicate it by creating another config or configuration or what so ever suggested in your question or other answers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文