public void Apply(ApplicationConfiguration configuration, ApplicationConfigurationSettings settings, string targetFile)
{
XDocument csdef;
XNamespace ns = CsdefNamespace;
using (StreamReader reader = new StreamReader(targetFile))
{
csdef = XDocument.Load(reader);
}
XElement[] workerRoles = csdef.Root.Elements(ns + "WorkerRole").ToArray();
foreach (XElement workerRole in workerRoles)
{
XElement configurationSettings = workerRole.Element(ns + "ConfigurationSettings");
if (configurationSettings == null)
{
configurationSettings = new XElement(ns + "ConfigurationSettings");
workerRole.Add(configurationSettings);
}
foreach (ApplicationComponent component in configuration.ApplicationComponents)
{
IComponentIdentity componentIdentity = new ComponentIdentity(component.Fqn);
if (!string.IsNullOrWhiteSpace(component.SqlServerConnectionString))
{
string key = _nameProvider.SqlConnectionString(componentIdentity);
ApplyConfigSetting(configurationSettings, key);
}
if (!string.IsNullOrWhiteSpace(component.StorageAccountConnectionString))
{
string key = _nameProvider.StorageAccountConnectionString(componentIdentity);
ApplyConfigSetting(configurationSettings, key);
}
if (!string.IsNullOrWhiteSpace(component.ServiceBusConnectionString))
{
string key = _nameProvider.ServiceBusConnectionString(componentIdentity);
ApplyConfigSetting(configurationSettings, key);
}
if (!string.IsNullOrWhiteSpace(component.DbContextType))
{
string key = _nameProvider.SqlContextType(componentIdentity);
ApplyConfigSetting(configurationSettings, key);
}
if (!string.IsNullOrWhiteSpace(component.DefaultQueueName))
{
string key = _nameProvider.DefaultQueueName(componentIdentity);
ApplyConfigSetting(configurationSettings, key);
}
if (!string.IsNullOrWhiteSpace(component.DefaultBlobContainerName))
{
string key = _nameProvider.DefaultBlobContainerName(componentIdentity);
ApplyConfigSetting(configurationSettings, key);
}
if (!string.IsNullOrWhiteSpace(component.DefaultTableName))
{
string key = _nameProvider.DefaultTableName(componentIdentity);
ApplyConfigSetting(configurationSettings, key);
}
if (!string.IsNullOrWhiteSpace(component.DefaultLeaseBlockName))
{
string key = _nameProvider.DefaultLeaseBlockName(componentIdentity);
ApplyConfigSetting(configurationSettings, key);
}
if (!string.IsNullOrWhiteSpace(component.DefaultSubscriptionName))
{
string key = _nameProvider.DefaultSubscriptionName(componentIdentity);
ApplyConfigSetting(configurationSettings, key);
}
if (!string.IsNullOrWhiteSpace(component.DefaultTopicName))
{
string key = _nameProvider.DefaultTopicName(componentIdentity);
ApplyConfigSetting(configurationSettings, key);
}
if (!string.IsNullOrWhiteSpace(component.DefaultBrokeredMessageQueueName))
{
string key = _nameProvider.DefaultBrokeredMessageQueueName(componentIdentity);
ApplyConfigSetting(configurationSettings, key);
}
foreach (ApplicationComponentSetting setting in component.Settings)
{
string key = _nameProvider.SettingName(componentIdentity, setting.Key);
ApplyConfigSetting(configurationSettings, key);
}
}
}
using (FileStream outputStream = new FileStream(targetFile, FileMode.Create))
{
csdef.Save(outputStream);
}
}