首页 > 技术文章 > Windows Azure Mangement API 之 更方便的使用Mangement API

he-yuan 2013-10-24 21:31 原文

许多.Net 程序员在使用Azure Management API的时候都选择参考微软官方示例,通过创建HttpWebRequest来创建。

或者自己创建类库来封装这些API,使之调用起来更加方便。

其实微软已经存在着这么一个已经封装好的类库了,不过由于这个类库并没有任何官方文档来对其进行说明,所以一直没有太多程序员想到去用它,这就是WindowsAzure.ServiceManagement.Client 类库。

要想使用这个类库,首先我们需要获取它, 目前我知道的获取它最新版本的方法是

1, 打开Web platform installer, 搜索windows azure powershell 并选择安装。

2, 安装完成后Powershell下面会多一个Azure文件夹,这个类库就在里面! 默认文件路径:C:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell\Azure

将该dll拷贝到你的项目中并添加引用你就可以来使用他啦!

下面我们就来通过来实现Create storage Account 这个功能来看看,究竟该如何使用这个类库吧!

class Program
   {
       static void Main(string[] args)
       {
           try
           {
               Uri azrueManagementUri = new Uri("https://management.core.windows.net");
               string subscriptionid = "Your subscriptionid";
               X509Certificate2 cer = GetCertificate("Your certificate thumbprint");


               var management = new ServiceManagementClient(azrueManagementUri,
                   subscriptionid,
                   cer,
                   new ServiceManagementClientOptions());

               CreateStorageServiceInput createStorageServiceInput = new CreateStorageServiceInput
               {
                   Label=EncodeTo64("dinohestorage1"),
                   GeoReplicationEnabled=false,
                   Location = "East Asia",
                   ServiceName=" dinohestorage1"

               };
               management.CreateStorageService(createStorageServiceInput);
               Console.WriteLine("Success!");
           }
           catch (ServiceManagementClientException e)
           {
               
               throw e;
           }
           Console.ReadLine();            
       }
       static public string EncodeTo64(string toEncode)
       {
           byte[] toEncodeAsBytes
                 = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
           string returnValue
                 = System.Convert.ToBase64String(toEncodeAsBytes);
           return returnValue;
       }
       public static X509Certificate2 GetCertificate(string cerThumbprint)
       {
           string certificateThumbprint = cerThumbprint;
           string errorString;
           if (String.IsNullOrEmpty(certificateThumbprint))
           {
               errorString = "CertificateThumbprint cannot be found. Please check the config file. ";
               throw new Exception(errorString);
           }

           X509Store certificateStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
           certificateStore.Open(OpenFlags.ReadOnly);
           X509Certificate2Collection certs = certificateStore.Certificates.Find(X509FindType.FindByThumbprint, certificateThumbprint, false);
           if (certs.Count != 1)
           {
               errorString = "Client certificate cannot be found. Please check the config file.";
               return null;
           }
           return certs[0];
       }
   }

  

通过以上代码可以发现, 这个类库中主要通过先实例化一个ServiceManagementClient类,然后调用该类中的方法来操作Azure Platform, 这正是我们需要的:

这样实现代码还有一点好处在于即使出现了错误,抛出的错误信息也非常准确。

我们可以将这个代码运行两遍来看看抛出异常的信息,当我们运行第二遍的 时候就会出现以下信息:

An exception occurred when calling the ServiceMangement API. HTTP Status code:409. Service Management Error codee: confilictError. Message:A storage account named'storage1' already exists in the subscrption

推荐阅读