许多.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

Windows Azure Mangement API 之 更方便的使用Mangement API的更多相关文章

  1. 最佳实践:Windows Azure 网站 (WAWS)

     编辑人员注释:本文章由 Windows Azure 网站团队的项目经理Sunitha Muthukrishna 撰写. Windows Azure 网站 (WAWS) 允许您在 Windows ...

  2. Windows Azure系列公开课 - 第二课:为什么选择Windows Azure(上)

    Windows Azure是微软的云平台,可以提供广泛服务.您可以通过它搭建.部署并管理解决方案,用于实现您可以想象的几乎任何目标.换言之,WindowsAzure是拥有无限可能的世界.无论您是需要运 ...

  3. [转]探索 Windows Azure Storage

    本文转自:https://msdn.microsoft.com/zh-tw/jj573842 概觀 儲存服務 (Storage services) 在 Windows Azure 運算模擬器中提供了可 ...

  4. [转]Windows Azure安全概述

    本文转自:http://blogs.msdn.com/b/azchina/archive/2011/03/06/windows_5f00_azure_5f00_security_5f00_overvi ...

  5. 玩转Windows Azure存储服务——网盘

    存储服务是除了计算服务之外最重要的云服务之一.说到云存储,大家可以想到很多产品,例如:AWS S3,Google Drive,百度云盘...而在Windows Azure中,存储服务却是在默默无闻的工 ...

  6. Windows Azure Camp---漫步云端,创意无限

    不再需要一系列繁杂的网银密码,一键搞定所有的支付:与朋友约会时通过实时分享地理位置迅速找到对方,这些都可以在WindowsAzure平台得以实现.在刚刚结束的2013年微软学生夏令营中,来自全国30所 ...

  7. 进一步探索:Windows Azure 网站中解锁的配置选项

     编辑人员注释: 本文章由 Windows Azure 网站团队的项目经理 Erez Benari 撰写. 在 Windows Azure 网站 (WAWS) 中管理网站时,许多选项可使用 Azu ...

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

    在此之前,我曾经发过一篇文章讲叙了如何利用Azure power shell team 提供的class library. 而就在这篇文章发布之后不久,我又发现微软发布了一个preview 版本的Wi ...

  9. Windows Azure入门教学系列 (七):使用REST API访问Storage Service

    本文是Windows Azure入门教学的第七篇文章. 本文将会介绍如何使用REST API来直接访问Storage Service. 在前三篇教学中,我们已经学习了使用Windows Azure S ...

随机推荐

  1. 在springmvc中,获取Connection接口

    ServletContext context = request.getSession().getServletContext();WebApplicationContext wac = WebApp ...

  2. If A wants to use B

    Find the place where B is used, and use C to call C.B RootFrame.Navigated += CheckForResetNavigation ...

  3. [CareerCup] 15.2 Renting Apartment II 租房之二

    Write a SQL query to get a list of all buildings and the number of open requests (Requests in which ...

  4. Spring和Hibernate集成配置

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  5. Hibernate的延迟加载

    我们会分析load和get两种加载方式: 一.load加载方式 当使用load方法来得到一个对象时,此时hibernate会使用延迟加载的机制来加载这个对象,即:当我们使用session.load() ...

  6. level分层次输出内容添加leve

    代码如下:function getSubComments($parent = 0, $level = 0) { $db = &JFactory::getDBO(); $sql = " ...

  7. iphone6 帶回家”活動!

    十一小長假即將來臨,周向榮還準備窩在家裏坐等“鋒菲戀”的後續結果嗎?雖然宅男無罪,但是請不要繼續在論壇裏高呼“李亞鵬娶了張柏芝”等口號,放下你“不吐槽會死星人”的特質,走出家門去領略一下祖國的大好山河 ...

  8. 实现服务器端与客户端的实时通信 SignalR(1)

    一.本文出处:SignalR 实例介绍 (建议看原著里面有DEMO下载) 二.这篇文章介绍如何利用 VS2012 创建一个简单的实时聊天系统,建好后的样子如下(模拟三个在线用户):    三.Demo ...

  9. P4 前端编译器p4c-bm、后端编译器bmv2命令安装 make error问题

    参考:Github 安装p4c-bm: sudo pip install -r requirements.txt sudo pip install -r requirements_v1_1.txt / ...

  10. DS实验题 PlayGame Kruskal(UnionFindSet)

    题目: 思路: 有两种做法,一种是Prim算法,另外一种则是我所使用的Kruskal算法,Kruskal的算法实现可以参考:最小生成树-Prim算法和Kruskal算法,讲的已经是十分清楚了. 具体算 ...