在此以前,我曾經發過一篇文章講敘了如何利用Azure power shell team 提供的class library。html
而就在這篇文章發佈以後不久,我又發現微軟發佈了一個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。。。