你好,游客 登录 注册 搜索
背景:
阅读新闻

Windows Azure Management API 使用

[日期:2012-07-27] 来源:Linux社区  作者:aa466564931 [字体:大 中 小]

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

  1. 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配置信息

    1. string subscriptionID = "your azure subscription ID";  
    2.    string thrumbnail = "your certificate thumbprint";  
    3.    string hostedServiceName = "your hosted service name";  
    4.    string deployName = "staging";  
    5.    Uri reuqestUri = new Uri("https://management.core.windows.net/" + subscriptionID + "/services/hostedservices/" + hostedServiceName + "/deploymentslots/" + deployName + "/?comp=config");  
    6.    X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);  
    7.    store.Open(OpenFlags.ReadOnly);  
    8.    X509Certificate2Collection collection = store.Certificates.Find(X509FindType.FindByThumbprint, thrumbnail, false);  
    9.    store.Close();  
    10.   
    11.    if (collection.Count != 0)  
    12.    {  
    13.        X509Certificate2 certificate = collection[0];  
    14.        HttpWebRequest request = HttpWebRequest.Create(reuqestUri) as HttpWebRequest;  
    15.        request.ClientCertificates.Add(certificate);  
    16.        request.Method = "POST";  
    17.   
    18.        request.ContentType = "application/xml";  
    19.        request.Headers.Add("x-ms-version", "2010-10-01");  
    20.   
    21.        StringBuilder sb = new StringBuilder();  
    22.        sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");  
    23.        sb.Append("<UpdateDeploymentStatus xmlns=\"http://schemas.microsoft.com/windowsazure\">");  
    24.        sb.Append(String.Format("<Status>{0}</Status>", "Suspended"));  
    25.        sb.Append("</UpdateDeploymentStatuscc>");  
    26.        byte[] bytes = UTF8Encoding.UTF8.GetBytes(sb.ToString());  
    27.        request.ContentLength = bytes.Length;  
    28.        HttpWebResponse response = request.GetResponse() as HttpWebResponse;  
    29.        using (Stream stream = request.GetRequestStream())  
    30.        {  
    31.            stream.Write(bytes, 0, bytes.Length);  
    32.        }  
    33.   
    34.        request.GetResponse();  
    35.   
    36.    }  

    subscription id是你的windows azure账号的ID, thumbprint是client端创建的自定义certificate的唯一标识符,同时你需要吧这个certificate上传到Azure management portal上面来,如果你需要在local的云模拟环境中测试的话,请在Role中配置好Certificate,如果只是运行web application或者Console application测试的话则不需要这一步,接着你就可以把Azure程序Publish到Management Portal上看效果了。

    linux
    本文评论   查看全部评论 (0)
    表情: 表情 姓名: 字数

           

    评论声明
    • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
    • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
    • 本站管理人员有权保留或删除其管辖留言中的任意内容
    • 本站有权在网站内转载或引用您的评论
    • 参与本评论即表明您已经阅读并接受上述条款