Windows Azure Management API 使用

Windows Auzre平臺可以通過後端代碼就行azure platform的相關屬性的修改, 這些API是基於REST service的,有GET,POST,PUT,DELETE幾種方式,你可以參考MSDN的這篇文章來設置具體的URI和相關參數,比如subscription id,hosted service name,certificate等等:

Windows Azure Service Management REST API Reference

http://msdn.microsoft.com/en-us/library/windowsazure/ee460799.aspx

參考這篇文章,你需要做的就是根據裏面的不同的section設置不同的Uri和參數。

例子,GET方式訪問所有hosted service

            string subscriptionID = "your azure subscription ID";
            string thrumbnail = "your certificate thumbprint";
            string hostedServiceName = "your hosted service name";
            Uri requestUri = new Uri("https://management.core.windows.net/" + subscriptionID + "/services/hostedservices/" + hostedServiceName);
            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;

                Stream stream = httpResponse.GetResponseStream();
                using (StreamReader reader = new StreamReader(stream))
                {
                    Response.Write("a");
                    string s = reader.ReadToEnd();
                    Response.Write(s.Replace("<", ""));


                }
                httpResponse.Close();
                stream.Close();


 

例子,POST方式修改hosted service配置信息

         string subscriptionID = "your azure subscription ID";
            string thrumbnail = "your certificate thumbprint";
            string hostedServiceName = "your hosted service name";
            string deployName = "staging";
            Uri reuqestUri = new Uri("https://management.core.windows.net/" + subscriptionID + "/services/hostedservices/" + hostedServiceName + "/deploymentslots/" + deployName + "/?comp=config");
            X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadOnly);
            X509Certificate2Collection collection = store.Certificates.Find(X509FindType.FindByThumbprint, thrumbnail, false);
            store.Close();

            if (collection.Count != 0)
            {
                X509Certificate2 certificate = collection[0];
                HttpWebRequest request = HttpWebRequest.Create(reuqestUri) as HttpWebRequest;
                request.ClientCertificates.Add(certificate);
                request.Method = "POST";

                request.ContentType = "application/xml";
                request.Headers.Add("x-ms-version", "2010-10-01");

                StringBuilder sb = new StringBuilder();
                sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                sb.Append("<UpdateDeploymentStatus xmlns=\"http://schemas.microsoft.com/windowsazure\">");
                sb.Append(String.Format("<Status>{0}</Status>", "Suspended"));
                sb.Append("</UpdateDeploymentStatuscc>");
                byte[] bytes = UTF8Encoding.UTF8.GetBytes(sb.ToString());
                request.ContentLength = bytes.Length;
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(bytes, 0, bytes.Length);
                }

                request.GetResponse();

            }


 

subscription id是你的windows azure賬號的ID, thumbprint是client端創建的自定義certificate的唯一標識符,同時你需要吧這個certificate上傳到Azure management portal上面來,如果你需要在local的雲模擬環境中測試的話,請在Role中配置好Certificate,如果只是運行web application或者Console application測試的話則不需要這一步,接着你就可以把Azure程序Publish到Management Portal上看效果了。
 

 

發佈了27 篇原創文章 · 獲贊 283 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章