Windows Azure Management API 使用

来源:互联网 发布:java循环流程图 编辑:程序博客网 时间:2024/04/26 21:40

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上看效果了。
 

 

原创粉丝点击