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 ...
随机推荐
- Java生成动态GIF图片
写selenium自动化时,为了查看运行效果,后给浏览器截图,想到可以生成gif图片来快速预览.看到已经有人实现了,直接拿过来. 共涉及到三个java文件,分别是NeuQuant.java,LZWEn ...
- jQuery延迟加载(懒加载)插件 – jquery.lazyload.js
引入:<script type="text/javascript" src="${base}/resources/shop/js/jquery.lazyload.j ...
- If A wants to use B
Find the place where B is used, and use C to call C.B RootFrame.Navigated += CheckForResetNavigation ...
- Android studio使用增量更新进行版本升级
今天将Android Studio更新了一下,特此记录一下升级过程,以后可能还会用得着. 首先通过菜单栏进入 Help --> Check for update 查看下当前版本是否需要更新.事实 ...
- 20个有用的jq弹窗
链接地址:http://www.ffpic.com/zixun/20-useful-jquery-modal-plugins.html 推荐:jQuery.bPopup.js
- 如何防御“神器”Mimikatz窃取系统密码?
Mimikatz是一款能够从Windows中获取内存,并且获取明文密码和NTLM哈希值的神器,本文将介绍如何防御这款软件获取密码. Mimikatz介绍 Mimikatz是一款能够从Windows认证 ...
- SSH框架中新建立实体类后的配置
strut 层 public class Sellauthentication extends ActionSupport { private SellauthenticationService ...
- java客户端调用c#的webservice服务
此处使用到了CXF框架,可以使用以下坐标从maven仓库中获取相关jar包: <dependency> <groupId>org.apache.cxf</groupId& ...
- 上传读取Excel文件数据
/// <summary> /// 上传读取Excel文件数据 /// 来自http://www.cnblogs.com/cielwater /// </summary> // ...
- 为什么Visual Studio的安装目录下有名为1033或2052的文件夹?
一直不清楚MS诸多产品的安装目录下为什么总有个名为1033或者2052的目录 搜索一下才知道,原来叫 LCID(Locale ID,区域性标识符) 常见的如:1033表示英语(美国),2052表示中文 ...