Search code examples
azureapp-configazure-worker-roles

Update app.config on Azure worker role start up


When an Azure worker role starts up, I need to update one of the value in it's app.config with a dynamic value.

I tried:

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

But it throws UnauthorizedException during start up. Is there any recommended way of doing it?


Solution

  • If that code is in your OnStart method, then it is too late to update the app.config since the process is already running. You need to do this from a startup task. There is an example using Powershell at http://blogs.msdn.com/b/cie/archive/2013/11/14/enable-server-gc-mode-for-your-worker-role.aspx.