Azure Management API 之 利用 Windows Azure Management Libraries 来控制Azure platform

在此以前,我曾经发过一篇文章讲叙了如何利用Azure power shell team 提供的class libraryhtml

而就在这篇文章发布以后不久,我又发现微软发布了一个preview 版本的Windows Azure Management Libraries For .NET Nuget package来帮助.NET 开发人员来更好的控制Auzre Platform。程序员

相比power shell team使用的library, Windows Azure Management Libraries For .NET 将业务逻辑更好的划分了开来,同时也使用了最新的Async programing来替代.net 4.5以前很是流行的异步委托编程方式。web

很明显,这个class library从此将融入Azure SDK 之中,成为替代.NET 程序员直接调用Azure Management REST API的最佳选择。shell

那么就让咱们来了解一下如何使用这个libararies吧。编程

1、添加Nuget Packages到项目中windows

新建一个Console应用程序,打开Tools->Library pPackage Manager->Package Manager Console.异步

而后输入如下命令行来安装该package:spa

Install-Package Microsoft.WindowsAzure.Management.Libraries -Pre.net

接下来咱们将经过几个示例来了解如何使用这个library,首先让咱们来获取Azure portal下全部Host service 的名字吧!命令行

2、利用Compute Management Client 获取Azure platform下全部Azure cloud service host Name

首先,咱们须要登陆如下连接来获取与Azure 平台交互所需的publishsettings file

https://manage.windowsazure.com/publishsettings/index?client=vs&schemaversion=2.0

 

打开Console程序建立以下代码

using Microsoft.WindowsAzure.Management.Compute;
using Microsoft.WindowsAzure.Management;
using Microsoft.WindowsAzure;
using System.Security.Cryptography.X509Certificates;

namespace ListCloudServiceName
{
    class Program
    {
        public const string base64EncodedCertificate = "<Your PublishSettings file ManagementCertificate property's Value>";
        public const string subscriptionId = "<You Azure subscription id>";

        static void Main(string[] args)
        {
            getAllCloudServicesName();
            Console.ReadLine();
        }

        public static void getAllCloudServicesName()
        {
            ComputeManagementClient client = new ComputeManagementClient(getCredentials());
            var cloudServiceList=client.HostedServices.List();
            foreach (var cloudService in cloudServiceList)
            {
                Console.WriteLine(cloudService.ServiceName);
            }
        }

        static SubscriptionCloudCredentials getCredentials()
        {
            return new CertificateCloudCredentials(subscriptionId,new X509Certificate2(Convert.FromBase64String(base64EncodedCertificate)));
        }
    }
}

  

将publishsetting中的 ManagementCertificate 属性的值与id属性的值分别填入上面代码之中。

这样一个简单的获取全部cloud Service name的程序就完成了。

这里调用的是client.HostedServices.List()方法, 这个方法是一个extension method。

微软把与Azure Management REST API对应的一些方法都写成了extension method方便咱们的调用。

并且微软将不一样的技术都作了层层划分,首先是dll分红了5个,而后再在dll内将不一样的技术划分开来方便了不一样的.net 开发人员来进行调用,更具备针对性了。

因为目前这个package 刚刚推出,并无多少的文档来详细阐述如何使用这个它, 我会在以后的blog中,针对我平常经常使用的一些操做来进行阐述,但愿更多.net 开发人员可以使用上这个很是不错的类, 从而结束不停的send http web request。。。

相关文章
相关标签/搜索