Пример #1
0
        internal string MakeRequest()
        {
            var currentDeployment = AzureRequest(RequestType.Get, null,
                                                 "https://management.core.windows.net/{0}/services/hostedservices/{1}/deploymentslots/{2}",
                                                 this.ServiceName, this.SlotName);

            if (string.IsNullOrEmpty((string)currentDeployment.Document.Root.Element(ns + "Name")))
            {
                this.LogInformation("There is currently nothing deployed to the {0} deployment slot.", this.SlotName);
                return(null);
            }

            AzureResponse resp = null;

            if (string.IsNullOrEmpty(this.DeploymentName))
            {
                resp = AzureRequest(RequestType.Delete, null, "https://management.core.windows.net/{0}/services/hostedservices/{1}/deploymentslots/{2}",
                                    this.ServiceName, this.SlotName);
            }
            else
            {
                resp = AzureRequest(RequestType.Delete, null, "https://management.core.windows.net/{0}/services/hostedservices/{1}/deployments/{2}",
                                    this.ServiceName, this.DeploymentName);
            }
            if (HttpStatusCode.Accepted != resp.StatusCode)
            {
                LogError("Error deleting deployment named {0}. Error code is: {1}, error description: {2}", this.ServiceName, resp.ErrorCode, resp.ErrorMessage);
                return(null);
            }
            return(resp.Headers.Get("x-ms-request-id"));
        }
Пример #2
0
        internal AzureResponse AzureRequest(RequestType requestType, string payload, string uriFormat, params object[] args)
        {
            var azureResponse = new AzureResponse();
            var uri           = new Uri(string.Format(uriFormat, new object[] { this.Credentials.SubscriptionID }.Concat(args).ToArray()));

            this.LogDebug("Sending Azure API {0} request to \"{1}\"...", requestType.ToString().ToUpper(), uri);
            var req = (HttpWebRequest)HttpWebRequest.Create(uri);

            if (requestType == RequestType.Post)
            {
                req.Method = "POST";
            }
            else if (requestType == RequestType.Delete)
            {
                req.Method = "DELETE";
            }
            else
            {
                req.Method = "GET";
            }

            req.Headers.Add("x-ms-version", OperationVersion);
            req.ClientCertificates.Add(this.Credentials.Certificate);
            req.ContentType = "application/xml";
            if (!string.IsNullOrEmpty(payload))
            {
                this.LogDebug("Writing request data...");
                var buffer = Encoding.UTF8.GetBytes(payload);
                req.ContentLength = buffer.Length;
                Stream reqStream = req.GetRequestStream();
                reqStream.Write(buffer, 0, buffer.Length);
                reqStream.Close();
            }
            HttpWebResponse resp;

            try
            {
                resp = (HttpWebResponse)req.GetResponse();
            }
            catch (WebException ex)
            {
                resp = (HttpWebResponse)ex.Response;
            }
            azureResponse.StatusCode = resp.StatusCode;
            azureResponse.Headers    = resp.Headers;
            if (resp.ContentLength > 0)
            {
                using (XmlReader reader = XmlReader.Create(resp.GetResponseStream()))
                {
                    this.LogDebug("Parsing Azure API XML response...");

                    azureResponse.Document     = XDocument.Load(reader);
                    azureResponse.ErrorMessage = (string)azureResponse.Document.Descendants(ns + "Message").FirstOrDefault();
                    azureResponse.ErrorCode    = (string)azureResponse.Document.Descendants(ns + "Code").FirstOrDefault();
                    AzureResponse.OperationStatusResult status;
                    var statusElement = azureResponse.Document.Root.Element(ns + "Status");
                    if (statusElement != null && Enum.TryParse <AzureResponse.OperationStatusResult>(statusElement.Value, true, out status))
                    {
                        azureResponse.OperationStatus = status;
                    }

                    this.LogDebug("Azure API XML response parsed.");
                }
            }

            return(azureResponse);
        }
Пример #3
0
        internal AzureResponse AzureRequest(RequestType requestType, string payload, string uriFormat, params object[] args)
        {
            var azureResponse = new AzureResponse();
            var uri = new Uri(string.Format(uriFormat, new object[] { this.Credentials.SubscriptionID }.Concat(args).ToArray()));
            this.LogDebug("Sending Azure API {0} request to \"{1}\"...", requestType.ToString().ToUpper(), uri);
            var req = (HttpWebRequest)HttpWebRequest.Create(uri);
            if (requestType == RequestType.Post)
                req.Method = "POST";
            else if (requestType == RequestType.Delete)
                req.Method = "DELETE";
            else
                req.Method = "GET";

            req.Headers.Add("x-ms-version", OperationVersion);
            req.ClientCertificates.Add(this.Credentials.Certificate);
            req.ContentType = "application/xml";
            if (!string.IsNullOrEmpty(payload))
            {
                this.LogDebug("Writing request data...");
                var buffer = Encoding.UTF8.GetBytes(payload);
                req.ContentLength = buffer.Length;
                Stream reqStream = req.GetRequestStream();
                reqStream.Write(buffer, 0, buffer.Length);
                reqStream.Close();
            }
            HttpWebResponse resp;
            try
            {
                resp = (HttpWebResponse)req.GetResponse();
            }
            catch (WebException ex)
            {
                resp = (HttpWebResponse)ex.Response;
            }
            azureResponse.StatusCode = resp.StatusCode;
            azureResponse.Headers = resp.Headers;
            if (resp.ContentLength > 0)
            {
                using (XmlReader reader = XmlReader.Create(resp.GetResponseStream()))
                {
                    this.LogDebug("Parsing Azure API XML response...");

                    azureResponse.Document = XDocument.Load(reader);
                    azureResponse.ErrorMessage = (string)azureResponse.Document.Descendants(ns + "Message").FirstOrDefault();
                    azureResponse.ErrorCode = (string)azureResponse.Document.Descendants(ns + "Code").FirstOrDefault();
                    AzureResponse.OperationStatusResult status;
                    var statusElement = azureResponse.Document.Root.Element(ns + "Status");
                    if (statusElement != null && Enum.TryParse<AzureResponse.OperationStatusResult>(statusElement.Value, true, out status))
                        azureResponse.OperationStatus = status;

                    this.LogDebug("Azure API XML response parsed.");
                }
            }

            return azureResponse;
        }