
There are times when it is important for Azure Cloud Service application to know which deployment slot it is operating in. Worker Roles in particular need to know if certain functionality should be disabled when running in the Staging slot. This article will provide a step-by-step way to find that either you are in a Staging or Production environment when testing out your application.
This is adapted from a code sample from MSDN Samples library. In short, you need to get deploymentId
from RoleEnvironment.DeploymentId
Then use Management API with a proper X509 certificate
to get at the Azure structure of your Service
and call the GetDeployments
method (it’s rest api but there is an abstraction library).
Now to use the code from Microsoft, you must have installed Windows Azure SDK 1.6
Step 1 – Open the CSAzureDeploymentSlot.sln file with Visual Studio in elevated (administrator) mode
Step 2- Set CSAzureDeploymentSlot Azure application as the startup application.
Step 3- Replace the following variables with your Azure application’s information.
C#
string subscriptionID = “”;
string thrumbnail = “”;
string hostedServiceName = “”;
Step 4- Upload a server certificate in Azure Management Portal, also add the same thumbnail print client certificate in Web role. Right Click your role => select “Certificates” => “Add Certificate”.
Step 5- This sample code cannot execute on the local, because local deploymentID is different from cloud deploymentID, local’s ID like “Deployment(XXX)”, but cloud’ID is a GUID. So if you run the application in local, it displays “Do not find this id”.
Step 6- . After deploying the application, click the DNS name of the role, if your application is in the Staging slot, it will display:

If your application is in the Production slot, it will display:

Additionally,
- Create a Cloud Application Project in Visual Studio 2010, name it as “CSAzureDeploymentSlot”, please also create a Web Role application and name it as “CSAzureDeploymentSlot_WebRole”.
- Delete all unnecessary files of the Web role application, and create a new Default web form page. Add a button control on this page, this page is used to send GET request to Azure management api to get deployment slot from Azure application. Please add the following code snippets in Default.aspx.cs file.
Here is the actual code that determines Windows Azure application’s current instance deployment slot:
C#
protected void Page_Load(object sender, EventArgs e)
{
// You basic information of the Deployment of Azure application.
string deploymentId = RoleEnvironment.DeploymentId;
string subscriptionID = “”;
string thrumbnail = “”;
string hostedServiceName = “”;
string productionString = string.Format(“https://management.core.windows.net/{0}/services/hostedservices/{1}/deploymentslots/{2}”,
subscriptionID, hostedServiceName, “Production”);
Uri requestUri = new Uri(productionString);
// Add client certificate.
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly);
X509Certificate2Collection collection = store.Certificates.Find(X509FindType.FindByThumbprint, thrumbnail, false);
store.Close();
if (collection.Count != 0)
{
X509Certificate2 certificate = collection[0];
HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(requestUri);
httpRequest.ClientCertificates.Add(certificate);
httpRequest.Headers.Add(“x-ms-version”, “2011-10-01”);
httpRequest.KeepAlive = false;
HttpWebResponse httpResponse = httpRequest.GetResponse() as HttpWebResponse;
// Get response stream from Management API.
Stream stream = httpResponse.GetResponseStream();
string result = string.Empty;
using (StreamReader reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
if (result == null || result.Trim() == string.Empty)
return;
XDocument document = XDocument.Parse(result);
string serverID = string.Empty;
var list = from item in document.Descendants(XName.Get(“PrivateID”, “http://schemas.microsoft.com/windowsazure”))
select item;
serverID = list.First().Value;
Response.Write(“Check Production: “);
Response.Write(“DeploymentID : ” + deploymentId + ” ServerID :” + serverID);
if (deploymentId.Equals(serverID))
lbStatus.Text = “Production”;
else
{
// If the application not in Production slot, try to check Staging slot.
string stagingString = string.Format(“https://management.core.windows.net/{0}/services/hostedservices/{1}
/deploymentslots/{2}”, subscriptionID, hostedServiceName, “Staging”);
Uri stagingUri = new Uri(stagingString);
httpRequest = (HttpWebRequest)HttpWebRequest.Create(stagingUri);
httpRequest.ClientCertificates.Add(certificate);
httpRequest.Headers.Add(“x-ms-version”, “2011-10-01”);
httpRequest.KeepAlive = false;
httpResponse = httpRequest.GetResponse() as HttpWebResponse;
stream = httpResponse.GetResponseStream();
result = string.Empty;
using (StreamReader reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
if (result == null || result.Trim() == string.Empty)
return;
document = XDocument.Parse(result);
serverID = string.Empty;
list = from item in document.Descendants(XName.Get(“PrivateID”, “http://schemas.microsoft.com/windowsazure”))
select item;
serverID = list.First().Value;
Response.Write(” Check Staging:”);
Response.Write(” DeploymentID : ” + deploymentId + ” ServerID :” + serverID);
if (deploymentId.Equals(serverID))
lbStatus.Text = “Staging”;
else
lbStatus.Text = “Do not find this id”;
}
httpResponse.Close();
stream.Close();
}
}
Netreo can help you with all your Azure monitoring needs. Request a Demo!