Skip to content

Instantly share code, notes, and snippets.

@nickfloyd
Created October 13, 2014 14:30
Show Gist options
  • Save nickfloyd/9a31ecae42150dd8d74c to your computer and use it in GitHub Desktop.
Save nickfloyd/9a31ecae42150dd8d74c to your computer and use it in GitHub Desktop.
Transforming Web.config appSettings for Microsoft Azure Cloud service
using System.Linq;
using Microsoft.Web.Administration;
using Microsoft.WindowsAzure.ServiceRuntime;
namespace Microsoft.Portal.Extensions.NewRelic_AzurePortal_APM
{
// Credit goes to Andy Cross, azurecoder, and Beth Martin from the Elastacloud blog and Steven Kuhn:
// http://blog.elastacloud.com/2011/05/13/azure-howto-programmatically-modify-web-config-on-webrole-startup/
/*
In your ServiceConfiguration.csdef you'll need to have two things:
1. A setting defined inside the WebRole element
<ConfigurationSettings>
<Setting name="NewRelic.AppName" />
</ConfigurationSettings>
2. <Runtime executionContext="elevated" /> for executing the transform
In any *.cscfg you need to specify a setting with the same name you did in the WebRole.cs
<ConfigurationSettings>
<Setting name="MySetting" value="MyValue" />
</ConfigurationSettings>
*/
public class WebRole : RoleEntryPoint
{
public override bool OnStart()
{
ConfigureNewRelic();
return base.OnStart();
}
private static void ConfigureNewRelic()
{
if (RoleEnvironment.IsAvailable && !RoleEnvironment.IsEmulated)
{
string settingValue;
try
{
settingValue = RoleEnvironment.GetConfigurationSettingValue("[SETTING_NAME]");
}
catch (RoleEnvironmentException)
{
/*nothing we can do so just return*/
return;
}
if (string.IsNullOrWhiteSpace(settingValue))
return;
using (var server = new ServerManager())
{
// get the site's web configuration
const string siteNameFromServiceModel = "Web";
var siteName = string.Format("{0}_{1}", RoleEnvironment.CurrentRoleInstance.Id, siteNameFromServiceModel);
var siteConfig = server.Sites[siteName].GetWebConfiguration();
// get the appSettings section
var appSettings = siteConfig.GetSection("appSettings").GetCollection();
AddConfigElement(appSettings, "[APP_SETTING_NAME]", settingValue);
server.CommitChanges();
}
}
}
private static void AddConfigElement(ConfigurationElementCollection appSettings, string key, string value)
{
if (appSettings.Any(t => t.GetAttributeValue("key").ToString() == key))
{
appSettings.Remove(appSettings.First(t => t.GetAttributeValue("key").ToString() == key));
}
var addElement = appSettings.CreateElement("add");
addElement["key"] = key;
addElement["value"] = value;
appSettings.Add(addElement);
}
}
}
@bkrauska
Copy link

By "siteNameFromServiceModel" you mean what? The name of the cloud service, The name of the cloud project, the name of the web role, the name of the service definition?

@nickfloyd
Copy link
Author

@bkrauska

Sorry to be so obscure there. A web role will always have a suffix of "_web" so the actual value coming out from the formatter will look something like: MyWebRoleName_IN_0_web - it's a convention used by Microsoft to segment settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment