Windows Azure Mangement API 之 更方便的使用Mangement API
许多.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的更多相关文章
- 最佳实践:Windows Azure 网站 (WAWS)
编辑人员注释:本文章由 Windows Azure 网站团队的项目经理Sunitha Muthukrishna 撰写. Windows Azure 网站 (WAWS) 允许您在 Windows ...
- Windows Azure系列公开课 - 第二课:为什么选择Windows Azure(上)
Windows Azure是微软的云平台,可以提供广泛服务.您可以通过它搭建.部署并管理解决方案,用于实现您可以想象的几乎任何目标.换言之,WindowsAzure是拥有无限可能的世界.无论您是需要运 ...
- [转]探索 Windows Azure Storage
本文转自:https://msdn.microsoft.com/zh-tw/jj573842 概觀 儲存服務 (Storage services) 在 Windows Azure 運算模擬器中提供了可 ...
- [转]Windows Azure安全概述
本文转自:http://blogs.msdn.com/b/azchina/archive/2011/03/06/windows_5f00_azure_5f00_security_5f00_overvi ...
- 玩转Windows Azure存储服务——网盘
存储服务是除了计算服务之外最重要的云服务之一.说到云存储,大家可以想到很多产品,例如:AWS S3,Google Drive,百度云盘...而在Windows Azure中,存储服务却是在默默无闻的工 ...
- Windows Azure Camp---漫步云端,创意无限
不再需要一系列繁杂的网银密码,一键搞定所有的支付:与朋友约会时通过实时分享地理位置迅速找到对方,这些都可以在WindowsAzure平台得以实现.在刚刚结束的2013年微软学生夏令营中,来自全国30所 ...
- 进一步探索:Windows Azure 网站中解锁的配置选项
编辑人员注释: 本文章由 Windows Azure 网站团队的项目经理 Erez Benari 撰写. 在 Windows Azure 网站 (WAWS) 中管理网站时,许多选项可使用 Azu ...
- Azure Management API 之 利用 Windows Azure Management Libraries 来控制Azure platform
在此之前,我曾经发过一篇文章讲叙了如何利用Azure power shell team 提供的class library. 而就在这篇文章发布之后不久,我又发现微软发布了一个preview 版本的Wi ...
- Windows Azure入门教学系列 (七):使用REST API访问Storage Service
本文是Windows Azure入门教学的第七篇文章. 本文将会介绍如何使用REST API来直接访问Storage Service. 在前三篇教学中,我们已经学习了使用Windows Azure S ...
随机推荐
- MetaHandler.js:移动端适配各种屏幕
MetaHandler.js !function () { var opt = function() { var ua = navigator.userAgent, android = ua.matc ...
- hdu-acm steps 命运
/*表示刚刚接触dp.这是我接触到的第3道dp题,刚开始以为是要用dfs+dp,后来栈溢出...仔细想想, 其实这道题和数塔差不多,只要每步都得到最优子结构,最后结果一定是最优的.题目的初始化要做好, ...
- openoffice从word转pdf问题
http://www.oschina.net/question/227511_87517
- Linux关闭休眠和屏保模式
本人因为特殊需求,想让某台Linux主机始终显示某个程序,显示器不能关机或者休眠或进入屏保模式. 环境:Ubuntu 11.10 最小化模式安装并安装有轻量级桌面openbox(非gnome).因为X ...
- zju(2)vivi的配置编译和固化
1.实验目的 熟悉vivi的知识和应用并使用交叉编译平台vivi引导并烧写到目标板. 二.实验内容 1. 在Ubuntu下配置vivi并进行交叉编译: 2. 将编译好的vivi烧写到目标板上. 三.主 ...
- ASP.NET MVC中在Action获取提交的表单数据方法总结 (4种方法,转载备忘)
有Index视图如下: 视图代码如下: <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Mas ...
- php课程---练习(发布新闻)
做一个发布新闻的页面,实现发布新闻,查看新闻,修改新闻与删除等功能 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/ ...
- IOS第一天
第一天(hello world) 1>UIView所有的控件都继承UIView,倒位置,宽度和高度..UIButton UILable 2>UIViewController .h 是声明属 ...
- 内省操作javabean的属性
import java.beans.BeanInfo; import java.beans.IntrospectionException; import java.beans.Introspector ...
- Python的正则表达式笔记
1. "先抓大再抓小": 遇到一个正则表达式无法一次性筛选出所需内容时, 可以先在一个范围内筛选第一次, 再在小范围中筛选第二次. 2. pattern = re.compile( ...