标签:
在对Azure 进行二次开发过程中,首当其冲就是明白Azure的具体的”交互逻辑“,”消息机制“,”数据管理“,在说问题前,我想你对于Rest API 的使用,至少有了初步的了解。这里,我就不多说,微软给我最巨大的印象就是它的文档,如果,你还有些不了解,请参考“Microsoft Developer Network”的“服务管理Rest API参考”,链接是https://msdn.microsoft.com/zh-cn/library/ee460799.aspx;
那么下面我们就详细的讨论下,资源必须要独占访问的缘由:
打开IE ,访问Windows Azure 世纪互联的门户网站https://manage.windowsazure.cn/,先在Azure上创建一台虚机,因为下面我将会利用虚机的端口配置,去说明这个问题,让大家可以动手去操作端口,加深理解;
点击“虚机名称”,找到 ”端点“Tab页
下面我们就对当前虚机,进行端口的配置:
开始创建端口,这里,我就简单的配置下HTTP 80端口;
点击提交按钮后,你会有所发现,页面工具栏显示灰色,且不能点击
这里,细心的开发人员,会发现当前资源在被执行某个功能时,其他的按钮都无法操作,当然,除了”虚机端点“外,很多地方都是这样!
在利用Rest API 去发送请求后,它返回标准HTTP的状态代码,就好像页面视图执行操作后,返回”500“错误码一样。等同于,我利用API发送请求执行 某个虚机”新增端口”,这个过程,你可以通过Windows Azure 门户网站看到,正在新增端口,可这个时候,突然出现一次意外,我在第一次发送的新增端口请求,可还没有新增完成后,我又再次发送了一次“新增端口” 的请求。这时,我通过try catch 捕获到一条错误信息,提示“******某某资源必须要独占访问**********”(这里,我只是写个大概提示);
这时,我必须在代码中添加 ”判断资源是否被独占访问中“ 逻辑,下面我就在代码中实现这个逻辑:
/// 返回部署的配置信息、状态和系统属性
/// </summary>
/// <param name="SubscriptionID"></param>
/// <param name="ServiceName"></param>
/// <param name="DeploymentName"></param>
/// <returns></returns>
public AzureVMDeploymentInfo GetAzureDeploymentInfo(string SubscriptionID, string ServiceName, string DeploymentName)
{
AzureVMDeploymentInfo rtn = null;
RestClient client = GetAzureRestClient(this.ApiAddress, this.CertificatePath);
var req = new RestRequest();
req.AddHeader("Content-Type", "application/xml");
req.Resource = SubscriptionID + "/" + "services/hostedservices/" + ServiceName + "/deployments/" + DeploymentName;
var rep = client.Execute(req);
rtn = GetAzureDeploymentInfo(rep);
return rtn;
}
/// 获取部署属性
/// </summary>
/// <param name="DeployResponse"></param>
/// <returns></returns>
private AzureVMDeploymentInfo GetAzureDeploymentInfo(IRestResponse DeployResponse)
{
AzureVMDeploymentInfo rtn = null;
if (DeployResponse.StatusCode == HttpStatusCode.OK)
{
#region
rtn = new AzureVMDeploymentInfo();
var xmlContentDoc = System.Xml.Linq.XDocument.Parse(DeployResponse.Content);
var rootNode = xmlContentDoc.Root;
rtn = new AzureVMDeploymentInfo();
rtn.DeploymentName = rootNode.Element(rootNode.Name.Namespace + "Name").Value;
rtn.DeploymentSlot = rootNode.Element(rootNode.Name.Namespace + "DeploymentSlot").Value;
rtn.Status = rootNode.Element(rootNode.Name.Namespace + "Status") != null ?
rootNode.Element(rootNode.Name.Namespace + "Status").Value : "";
rtn.PrivateID = rootNode.Element(rootNode.Name.Namespace + "PrivateID") != null ?
rootNode.Element(rootNode.Name.Namespace + "PrivateID").Value : "";
rtn.DeploymentLabel = rootNode.Element(rootNode.Name.Namespace + "Label") != null ?
rootNode.Element(rootNode.Name.Namespace + "Label").Value : "";
rtn.Url = rootNode.Element(rootNode.Name.Namespace + "Url") != null ?
rootNode.Element(rootNode.Name.Namespace + "Url").Value : "";
rtn.Configuration = rootNode.Element(rootNode.Name.Namespace + "Configuration") != null ?
rootNode.Element(rootNode.Name.Namespace + "Configuration").Value : "";
rtn.SdkVersion = rootNode.Element(rootNode.Name.Namespace + "SdkVersion") != null ?
rootNode.Element(rootNode.Name.Namespace + "SdkVersion").Value : "";
rtn.Locked = rootNode.Element(rootNode.Name.Namespace + "Locked") != null ?
bool.Parse(rootNode.Element(rootNode.Name.Namespace + "Locked").Value) : false;
rtn.RollbackAllowed = rootNode.Element(rootNode.Name.Namespace + "RollbackAllowed") != null ?
bool.Parse(rootNode.Element(rootNode.Name.Namespace + "RollbackAllowed").Value) : false;
rtn.VirtualNetworkName = rootNode.Element(rootNode.Name.Namespace + "VirtualNetworkName") != null ?
rootNode.Element(rootNode.Name.Namespace + "VirtualNetworkName").Value : "";
rtn.ReservedIPName = rootNode.Element(rootNode.Name.Namespace + "ReservedIPName") != null ?
rootNode.Element(rootNode.Name.Namespace + "ReservedIPName").Value : "";
rtn.InternalDnsSuffix = rootNode.Element(rootNode.Name.Namespace + "InternalDnsSuffix") != null ?
rootNode.Element(rootNode.Name.Namespace + "InternalDnsSuffix").Value : "";
#endregion
}
else
{
HttpErrorCodeHandler(DeployResponse);
}
return rtn;
}
/// 处理RestAPI返回的异常信息
/// </summary>
/// <param name="rep"></param>
private void HttpErrorCodeHandler(IRestResponse rep)
{
if (rep.StatusCode == HttpStatusCode.Accepted
|| rep.StatusCode == HttpStatusCode.Created
|| rep.StatusCode == HttpStatusCode.OK
|| rep.StatusCode == HttpStatusCode.NotFound)
{
return;
}
var ex = new AzureException();
var error = GetError(rep);
ex.ErrorMessage = error;
ex.API_Http_Code = rep.StatusCode;
throw ex;
}
标签:
原文地址:http://www.cnblogs.com/finehappy/p/4291050.html