Staging or Production Instance?

后端 未结 6 396
广开言路
广开言路 2020-11-29 18:45

Is there anywhere in the service runtime that would tell me if I\'m currently running on \'Staging\' or \'Production\'? Manually modifying the config to and from production

6条回答
  •  情书的邮戳
    2020-11-29 19:19

    Sometimes I wish people would just answer the question.. not explain ethics or best practices...

    Microsoft has posted a code sample doing exactly this here: https://code.msdn.microsoft.com/windowsazure/CSAzureDeploymentSlot-1ce0e3b5

    image showing Staging instance

    image showing Production instance

    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(); 
        } 
    }
    

提交回复
热议问题