示例#1
0
 private DatabaseOperation CreateDatabaseOperationsFromResponse(DatabaseOperationListResponse.DatabaseOperation response)
 {
     return(CreateDatabaseOperation(
                response.Name,
                response.State,
                response.Id,
                response.StateId,
                response.SessionActivityId,
                response.DatabaseName,
                response.PercentComplete,
                response.ErrorCode,
                response.Error,
                response.ErrorSeverity,
                response.ErrorState,
                response.StartTime,
                response.LastModifyTime
                ));
 }
        /// <summary>
        /// Returns the list database operations for a given server.
        /// </summary>
        /// <param name='serverName'>
        /// The name of the SQL Server to be queried.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Response containing the list of database operations for a given
        /// server or database.
        /// </returns>
        public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.Sql.Models.DatabaseOperationListResponse> ListByServerAsync(string serverName, CancellationToken cancellationToken)
        {
            // Validate
            if (serverName == null)
            {
                throw new ArgumentNullException("serverName");
            }

            // Tracing
            bool   shouldTrace  = CloudContext.Configuration.Tracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = Tracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("serverName", serverName);
                Tracing.Enter(invocationId, this, "ListByServerAsync", tracingParameters);
            }

            // Construct URL
            string url = new Uri(this.Client.BaseUri, this.Client.Credentials.SubscriptionId).ToString() + "/services/sqlservers/servers/" + serverName + "/databaseoperations?contentview=generic";

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2012-03-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        Tracing.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        Tracing.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false), CloudExceptionType.Xml);
                        if (shouldTrace)
                        {
                            Tracing.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    DatabaseOperationListResponse result = null;
                    // Deserialize Response
                    cancellationToken.ThrowIfCancellationRequested();
                    string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    result = new DatabaseOperationListResponse();
                    XDocument responseDoc = XDocument.Parse(responseContent);

                    XElement serviceResourcesSequenceElement = responseDoc.Element(XName.Get("ServiceResources", "http://schemas.microsoft.com/windowsazure"));
                    if (serviceResourcesSequenceElement != null)
                    {
                        foreach (XElement serviceResourcesElement in serviceResourcesSequenceElement.Elements(XName.Get("ServiceResource", "http://schemas.microsoft.com/windowsazure")))
                        {
                            DatabaseOperationListResponse.DatabaseOperation serviceResourceInstance = new DatabaseOperationListResponse.DatabaseOperation();
                            result.DatabaseOperations.Add(serviceResourceInstance);

                            XElement nameElement = serviceResourcesElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure"));
                            if (nameElement != null)
                            {
                                string nameInstance = nameElement.Value;
                                serviceResourceInstance.Name = nameInstance;
                            }

                            XElement typeElement = serviceResourcesElement.Element(XName.Get("Type", "http://schemas.microsoft.com/windowsazure"));
                            if (typeElement != null)
                            {
                                string typeInstance = typeElement.Value;
                                serviceResourceInstance.Type = typeInstance;
                            }

                            XElement stateElement = serviceResourcesElement.Element(XName.Get("State", "http://schemas.microsoft.com/windowsazure"));
                            if (stateElement != null)
                            {
                                string stateInstance = stateElement.Value;
                                serviceResourceInstance.State = stateInstance;
                            }

                            XElement selfLinkElement = serviceResourcesElement.Element(XName.Get("SelfLink", "http://schemas.microsoft.com/windowsazure"));
                            if (selfLinkElement != null)
                            {
                                string selfLinkInstance = selfLinkElement.Value;
                                serviceResourceInstance.SelfLink = selfLinkInstance;
                            }

                            XElement parentLinkElement = serviceResourcesElement.Element(XName.Get("ParentLink", "http://schemas.microsoft.com/windowsazure"));
                            if (parentLinkElement != null)
                            {
                                string parentLinkInstance = parentLinkElement.Value;
                                serviceResourceInstance.ParentLink = parentLinkInstance;
                            }

                            XElement idElement = serviceResourcesElement.Element(XName.Get("Id", "http://schemas.microsoft.com/windowsazure"));
                            if (idElement != null)
                            {
                                string idInstance = idElement.Value;
                                serviceResourceInstance.Id = idInstance;
                            }

                            XElement stateIdElement = serviceResourcesElement.Element(XName.Get("StateId", "http://schemas.microsoft.com/windowsazure"));
                            if (stateIdElement != null)
                            {
                                int stateIdInstance = int.Parse(stateIdElement.Value, CultureInfo.InvariantCulture);
                                serviceResourceInstance.StateId = stateIdInstance;
                            }

                            XElement sessionActivityIdElement = serviceResourcesElement.Element(XName.Get("SessionActivityId", "http://schemas.microsoft.com/windowsazure"));
                            if (sessionActivityIdElement != null)
                            {
                                string sessionActivityIdInstance = sessionActivityIdElement.Value;
                                serviceResourceInstance.SessionActivityId = sessionActivityIdInstance;
                            }

                            XElement databaseNameElement = serviceResourcesElement.Element(XName.Get("DatabaseName", "http://schemas.microsoft.com/windowsazure"));
                            if (databaseNameElement != null)
                            {
                                string databaseNameInstance = databaseNameElement.Value;
                                serviceResourceInstance.DatabaseName = databaseNameInstance;
                            }

                            XElement percentCompleteElement = serviceResourcesElement.Element(XName.Get("PercentComplete", "http://schemas.microsoft.com/windowsazure"));
                            if (percentCompleteElement != null)
                            {
                                int percentCompleteInstance = int.Parse(percentCompleteElement.Value, CultureInfo.InvariantCulture);
                                serviceResourceInstance.PercentComplete = percentCompleteInstance;
                            }

                            XElement errorCodeElement = serviceResourcesElement.Element(XName.Get("ErrorCode", "http://schemas.microsoft.com/windowsazure"));
                            if (errorCodeElement != null)
                            {
                                int errorCodeInstance = int.Parse(errorCodeElement.Value, CultureInfo.InvariantCulture);
                                serviceResourceInstance.ErrorCode = errorCodeInstance;
                            }

                            XElement errorElement = serviceResourcesElement.Element(XName.Get("Error", "http://schemas.microsoft.com/windowsazure"));
                            if (errorElement != null)
                            {
                                string errorInstance = errorElement.Value;
                                serviceResourceInstance.Error = errorInstance;
                            }

                            XElement errorSeverityElement = serviceResourcesElement.Element(XName.Get("ErrorSeverity", "http://schemas.microsoft.com/windowsazure"));
                            if (errorSeverityElement != null)
                            {
                                int errorSeverityInstance = int.Parse(errorSeverityElement.Value, CultureInfo.InvariantCulture);
                                serviceResourceInstance.ErrorSeverity = errorSeverityInstance;
                            }

                            XElement errorStateElement = serviceResourcesElement.Element(XName.Get("ErrorState", "http://schemas.microsoft.com/windowsazure"));
                            if (errorStateElement != null)
                            {
                                int errorStateInstance = int.Parse(errorStateElement.Value, CultureInfo.InvariantCulture);
                                serviceResourceInstance.ErrorState = errorStateInstance;
                            }

                            XElement startTimeElement = serviceResourcesElement.Element(XName.Get("StartTime", "http://schemas.microsoft.com/windowsazure"));
                            if (startTimeElement != null)
                            {
                                DateTime startTimeInstance = DateTime.Parse(startTimeElement.Value, CultureInfo.InvariantCulture);
                                serviceResourceInstance.StartTime = startTimeInstance;
                            }

                            XElement lastModifyTimeElement = serviceResourcesElement.Element(XName.Get("LastModifyTime", "http://schemas.microsoft.com/windowsazure"));
                            if (lastModifyTimeElement != null)
                            {
                                DateTime lastModifyTimeInstance = DateTime.Parse(lastModifyTimeElement.Value, CultureInfo.InvariantCulture);
                                serviceResourceInstance.LastModifyTime = lastModifyTimeInstance;
                            }
                        }
                    }

                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        Tracing.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }