许多.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. 浅析-博客Ping服务

    简介:PING服务是博客站点向博客目标网站.搜索引擎等发出的博客内容更新通知服务,然后博客目标网站.搜索引擎就会及时的索引.收录以及传播您的博客内容. PING原理 PING 服务是博客站点向博客目标 ...

  2. struts2中用xml配置文件去验证填写信息

    xml名字是这样的 actionName-validation.xml 每个action 对应一个xml文件 xml文件和action放在同一个包下 后台验证用户输入是否符合格式要求,不符合,提交后返 ...

  3. HDU2045/*HDU2604/*HDU2501/HDU2190 递推

    不容易系列之(3)-- LELE的RPG难题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  4. linux笔记五-------编辑器

    1. 三种模式    命令(默认).尾行.编辑模式 2. 尾行模式    :    :q      退出vi编辑器    :w      保存修改    :wq     保存并退出编辑    :q!  ...

  5. 2016HUAS暑假集训训练2 F - A Simple Problem with Integers

    Description 给出了一个序列,你需要处理如下两种询问. "C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000). " ...

  6. javascript遍历数组最优写法

    var arr = [];//这样定义的数组,是null,等待开辟空间 var arr = new Array();//不建议使用,会占用一块内存空间 var i=0,len=arr.length; ...

  7. unicode可以通过编码(encode)成为特定编码的str

    1.原始字符串python中的原始字符串以r开头,使用原始字符串可以避免字符串中转义字符带来的问题,例如写路径时 path = 'c:\noway',此时用 print path,其结果为:c:owa ...

  8. BizTalk开发系列(三十五) TCP/IP 适配器

    BizTalk 的TCP/IP适配器最初是为英国的保健行业开发.该适配器属于BizTalk进程内适配器,将消息通过TCP/IP 套接字符串在BizTalk服务器与远程客户端间进行通讯. TCP/IP适 ...

  9. IOS第十天(1:QQ好友列表 ,自定义的headview,代理 ,通知 ,black的使用)

    *****HMViewController.m #import "HMViewController.h" #import "HMFriendsGroupModel.h&q ...

  10. Larbin初试

    前阵子找工作的时候经常会看到epoll多路复用的知识点,无奈自己一点都不懂.慌忙之际也只能去了解个大概.所以最近闲下来之后想要基于epoll机制实现一个比较有用的东西,刚好最近又想爬些东西,希望这次能 ...