私は、クラウドサービスを削除するIPアドレスを取得し、それが構成ファイルに含まれているものと一致するかどうかを調べる、ビルド中に呼び出されるコンソールアプリケーションを作成しました。
そうでない場合は、更新します。かなり簡単。ここで
は、ビルドコマンドです:
$(SolutionDir)<MyProjectName>\$(OutDir)$(ConfigurationName)\MyExeName Update-FrontEnd-IPAddress-For-Azure-MicroService "$(SolutionDir)<AzureDeploymentProjectName>\ServiceConfiguration.Cloud.cscfg"
コンソールアプリケーションの処理が行われます。ここでは
private static void HandleCheckRoleEnvironment(string[] args)
{
if (args[0] == "Check-Role-Environment")
{
Console.WriteLine("Found Command: Check-Role-Environment");
if (RoleEnvironment.IsAvailable && !RoleEnvironment.IsEmulated)
{
Console.WriteLine("Running in Azure Cloud Environment");
Environment.Exit(0);
return;
}
else
{
Console.WriteLine("NOT Running in Azure Cloud Environment");
Environment.Exit(1);
return;
}
}
}
は、設定ファイルを更新するためのコードです:
private static void ExecuteUpdateFrontEndIPAddressForAzureMicroService(string configFilePath)
{
if (!File.Exists(configFilePath))
{
return;
}
var ipAddressList = Dns.GetHostAddresses("MyDomainName");
Console.WriteLine($"The IP address for MyDomainName is {ipAddressList[0].ToString()}");
var correctValue = $"{ipAddressList[0].ToString()}/32";
var document = new XmlDocument();
document.Load(configFilePath);
//Rule nodes
var rules = document.ChildNodes[1].LastChild.FirstChild.FirstChild.ChildNodes;
var rule = (from XmlNode p in rules
where p.Attributes["description"].Value == "Allow access from MyDomainName"
select p).FirstOrDefault();
var ipAddressValue = rule.Attributes["remoteSubnet"].Value;
Console.WriteLine($"The IP address in the config file is {ipAddressValue}");
if (correctValue != ipAddressValue)
{
rule.Attributes["remoteSubnet"].Value = correctValue;
document.Save(configFilePath);
Console.WriteLine("The config file has been updated with the correct IP address.");
}
else
{
Console.WriteLine("The config file is upto date and will not be updated.");
}
}