许多.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. [zt] ROI (Region of Interest) 感兴趣区域 OpenCV

    在以前介绍IplImage结构的时候,有一个重要的参数——ROI.ROI全称是”Region Of Interest”,即感兴趣的区域.实际上,它是IPL/IPP(这两个是Inter的库)结构IplR ...

  2. JAVA中序列化和反序列化

    一般程序在运行时,产生对象,这些对象随着程序的停止运行而消失(java回收机制)但如果我们想把某些对象(因为是对象,所以有各自不同的特性)保存下来,在程序终止运行后,这些对象仍然存在,可以在程序再次运 ...

  3. parentNode,offsetParent

    元素.parentNode : 父节点   只读 属性 当前节点的父级节点 没有兼容性问题 可放心使用 <!DOCTYPE HTML> <html> <head> ...

  4. Log4J简单使用

    一.一般会将commons-logging和Log4j一起使用   原因:1.commons-logging功能较弱 2.log4j功能强大. 所需jar:       log4j-1.2.16.ja ...

  5. iOS Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UITableView.m:7962

      Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Cac ...

  6. 矩形的个数-nyoj206

    描述在一个3*2的矩形中,可以找到6个1*1的矩形,4个2*1的矩形3个1*2的矩形,2个2*2的矩形,2个3*1的矩形和1个3*2的矩形,总共18个矩形.给出A,B,计算可以从中找到多少个矩形.   ...

  7. PHP魔术方法使用总结

    魔术方法是PHP面向对象中特有的特性.它们在特定的情况下被触发,都是以双下划线开头,你可以把它们理解为钩子,利用模式方法可以轻松实现PHP面向对象中重载(Overloading即动态创建类属性和方法) ...

  8. 利用box-flex实现 dom元素位置页面底部

    问题: 总是有这样的需求,就是页面上某部分要位于页面的最底部,此“最底部”要求:(1)当页面上内容不足一屏的时候,在最底部显示(2)当页面上内容不止一屏的时候,也就是有垂直滚动条的时候,要在内容的最后 ...

  9. IOS第16天(5,Quartz2D雪花)

    *** #import "HMView.h" @interface HMView() { int count; } @property (nonatomic, assign) CG ...

  10. XHTML表单

    1.HTML表单标记提供一套元素和属性,用来创建表单,收集网页参观者信息. 2.创建表单结构标记为<form>和</form>. 3.在<form>标记中,我们必须 ...