I want to maintain a staging as well as a production environment in azure. Each should have it\'s own blob storage and sql storage. What wod be the best way to achive this? setu
Here is a step by step guide.
1) You need the library: Microsoft.Samples.WindowsAzure.ServiceManagement There is a nuGet package entitled "Windows Azure Service Management Library" which contains this.
2) You need create a X509Certificate2 and follow the instructions laid out here. Make sure you upload the .CER file you create to the Subscription Certificate store. Make sure you upload a copy of the .PFX with the PRIVATE KEY to the actual cloud service certificate store.
Create and upload a certificate for Windows Azure Management
http://blogs.msdn.com/b/clouddeployments/archive/2010/05/12/making-calls-to-the-service-management-api-from-a-service-running-in-windows-azure.aspx
3) These tutorials also gloss over this: you need to have the service endpoint defined. I did that in my app.config file with the following
4) Once that is done, I created a static class named "GetServerInstance". Here is the code:
public static class GetServerInstance
{
const string SubId = "your azuresubscriptionid";
public static bool IsProductionEnvironment()
{
//get the current deploymentId
var currentInstance = RoleEnvironment.DeploymentId;
var mgmtChannnel = ServiceManagementHelper.CreateServiceManagementChannel("WindowsAzureEndPoint",GetCertifcate()); //make the endpoint.
var serviceDetails = mgmtChannnel.GetHostedServiceWithDetails(SubId, "your-cloud-service-name", true);
var currentDeploymentSlot = serviceDetails.Deployments.First(p => p.PrivateID == currentInstance).DeploymentSlot;
if (currentDeploymentSlot == DeploymentSlotType.Staging)
return false; //staging server
if (currentDeploymentSlot == DeploymentSlotType.Production)
return true; //production server
}
private static X509Certificate2 GetCertifcate()
{
string certificateThumbprint = RoleEnvironment.GetConfigurationSettingValue("CertificateThumbprint");
if (String.IsNullOrEmpty(certificateThumbprint))
{
return null; //I'd throw an exception here and log the error
}
var certificateStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
certificateStore.Open(OpenFlags.ReadOnly);
var certs = certificateStore.Certificates.Find(X509FindType.FindByThumbprint, certificateThumbprint, false);
if (certs.Count != 1)
{
return null; //I'd throw an exception here and log the error
}
return certs[0];
}
}
5) Now in my worker role, which I never want to run on Staging, because it will charge people twice. I call this:
if (GetServerInstance.IsProductionEnvironment())
{
//Do work! I'm in production
};