在 Azure 辅助角色启动时更新 app.config

Update app.config on Azure worker role start up

当 Azure 工作者角色启动时,我需要使用动态值更新其中的一个值 app.config。

我试过了:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["sampleName"].Value = "sampleValue";
config.Save(ConfigurationSaveMode.Minimal);
ConfigurationManager.RefreshSection("appSettings");

但是它在启动过程中抛出了 UnauthorizedException。有什么推荐的方法吗?

如果该代码在您的 OnStart 方法中,那么更新 app.config 为时已晚,因为进程已经 运行。您需要从启动任务中执行此操作。在 http://blogs.msdn.com/b/cie/archive/2013/11/14/enable-server-gc-mode-for-your-worker-role.aspx.

有一个使用 Powershell 的例子