/// <summary>
        /// Import DAC from Windows Azure blob storage.
        /// </summary>
        /// <param name='serverName'>
        /// The name of the server being imported to.
        /// </param>
        /// <param name='parameters'>
        /// Import parameters.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// Response for an DAC Import/Export request.
        /// </returns>
        public async Task <DacImportExportResponse> ImportAsync(string serverName, DacImportParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (serverName == null)
            {
                throw new ArgumentNullException("serverName");
            }
            if (parameters != null)
            {
                if (parameters.BlobCredentials != null)
                {
                    if (parameters.BlobCredentials.StorageAccessKey == null)
                    {
                        throw new ArgumentNullException("parameters.BlobCredentials.StorageAccessKey");
                    }
                    if (parameters.BlobCredentials.Uri == null)
                    {
                        throw new ArgumentNullException("parameters.BlobCredentials.Uri");
                    }
                }
                if (parameters.ConnectionInfo != null)
                {
                    if (parameters.ConnectionInfo.DatabaseName == null)
                    {
                        throw new ArgumentNullException("parameters.ConnectionInfo.DatabaseName");
                    }
                    if (parameters.ConnectionInfo.Password == null)
                    {
                        throw new ArgumentNullException("parameters.ConnectionInfo.Password");
                    }
                    if (parameters.ConnectionInfo.ServerName == null)
                    {
                        throw new ArgumentNullException("parameters.ConnectionInfo.ServerName");
                    }
                    if (parameters.ConnectionInfo.UserName == null)
                    {
                        throw new ArgumentNullException("parameters.ConnectionInfo.UserName");
                    }
                }
            }

            // 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);
                tracingParameters.Add("parameters", parameters);
                Tracing.Enter(invocationId, this, "ImportAsync", tracingParameters);
            }

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

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Post;
                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);

                // Serialize Request
                string    requestContent = null;
                XDocument requestDoc     = new XDocument();

                if (parameters != null)
                {
                    XElement importInputElement = new XElement(XName.Get("ImportInput", "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes"));
                    requestDoc.Add(importInputElement);

                    if (parameters.AzureEdition != null)
                    {
                        XElement azureEditionElement = new XElement(XName.Get("AzureEdition", "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes"));
                        azureEditionElement.Value = parameters.AzureEdition;
                        importInputElement.Add(azureEditionElement);
                    }

                    if (parameters.BlobCredentials != null)
                    {
                        XElement blobCredentialsElement = new XElement(XName.Get("BlobCredentials", "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes"));
                        importInputElement.Add(blobCredentialsElement);

                        XAttribute typeAttribute = new XAttribute(XName.Get("type", "http://www.w3.org/2001/XMLSchema-instance"), "");
                        typeAttribute.Value = "BlobStorageAccessKeyCredentials";
                        blobCredentialsElement.Add(typeAttribute);

                        XElement uriElement = new XElement(XName.Get("Uri", "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes"));
                        uriElement.Value = parameters.BlobCredentials.Uri.ToString();
                        blobCredentialsElement.Add(uriElement);

                        XElement storageAccessKeyElement = new XElement(XName.Get("StorageAccessKey", "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes"));
                        storageAccessKeyElement.Value = parameters.BlobCredentials.StorageAccessKey;
                        blobCredentialsElement.Add(storageAccessKeyElement);
                    }

                    if (parameters.ConnectionInfo != null)
                    {
                        XElement connectionInfoElement = new XElement(XName.Get("ConnectionInfo", "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes"));
                        importInputElement.Add(connectionInfoElement);

                        XElement databaseNameElement = new XElement(XName.Get("DatabaseName", "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes"));
                        databaseNameElement.Value = parameters.ConnectionInfo.DatabaseName;
                        connectionInfoElement.Add(databaseNameElement);

                        XElement passwordElement = new XElement(XName.Get("Password", "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes"));
                        passwordElement.Value = parameters.ConnectionInfo.Password;
                        connectionInfoElement.Add(passwordElement);

                        XElement serverNameElement = new XElement(XName.Get("ServerName", "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes"));
                        serverNameElement.Value = parameters.ConnectionInfo.ServerName;
                        connectionInfoElement.Add(serverNameElement);

                        XElement userNameElement = new XElement(XName.Get("UserName", "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes"));
                        userNameElement.Value = parameters.ConnectionInfo.UserName;
                        connectionInfoElement.Add(userNameElement);
                    }

                    XElement databaseSizeInGBElement = new XElement(XName.Get("DatabaseSizeInGB", "http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes"));
                    databaseSizeInGBElement.Value = parameters.DatabaseSizeInGB.ToString();
                    importInputElement.Add(databaseSizeInGBElement);
                }

                requestContent      = requestDoc.ToString();
                httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
                httpRequest.Content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");

                // 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, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false), CloudExceptionType.Xml);
                        if (shouldTrace)
                        {
                            Tracing.Error(invocationId, ex);
                        }
                        throw ex;
                    }

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

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

                    XElement guidElement = responseDoc.Element(XName.Get("guid", "http://schemas.microsoft.com/2003/10/Serialization/"));
                    if (guidElement != null)
                    {
                        result.Guid = guidElement.Value;
                    }

                    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();
                }
            }
        }
Beispiel #2
0
 /// <summary>
 /// Initiates an Import of a DACPAC file from Azure Blob Storage into a
 /// Azure SQL Database.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Sql.IDacOperations.
 /// </param>
 /// <param name='serverName'>
 /// Required. The name of the Azure SQL Database Server into which the
 /// database is being imported.
 /// </param>
 /// <param name='parameters'>
 /// Optional. The parameters needed to initiated the Import request.
 /// </param>
 /// <returns>
 /// Represents the response that the service returns once an import or
 /// export operation has been initiated.
 /// </returns>
 public static DacImportExportResponse Import(this IDacOperations operations, string serverName, DacImportParameters parameters)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((IDacOperations)s).ImportAsync(serverName, parameters);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }
Beispiel #3
0
 /// <summary>
 /// Initiates an Import of a DACPAC file from Azure Blob Storage into a
 /// Azure SQL Database.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Sql.IDacOperations.
 /// </param>
 /// <param name='serverName'>
 /// Required. The name of the Azure SQL Database Server into which the
 /// database is being imported.
 /// </param>
 /// <param name='parameters'>
 /// Optional. The parameters needed to initiated the Import request.
 /// </param>
 /// <returns>
 /// Represents the response that the service returns once an import or
 /// export operation has been initiated.
 /// </returns>
 public static Task <DacImportExportResponse> ImportAsync(this IDacOperations operations, string serverName, DacImportParameters parameters)
 {
     return(operations.ImportAsync(serverName, parameters, CancellationToken.None));
 }
 /// <summary>
 /// Import DAC from Windows Azure blob storage.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Sql.IDacOperations.
 /// </param>
 /// <param name='serverName'>
 /// The name of the server being imported to.
 /// </param>
 /// <param name='parameters'>
 /// Import parameters.
 /// </param>
 /// <returns>
 /// Response for an DAC Import/Export request.
 /// </returns>
 public static DacImportExportResponse Import(this IDacOperations operations, string serverName, DacImportParameters parameters)
 {
     try
     {
         return(operations.ImportAsync(serverName, parameters).Result);
     }
     catch (AggregateException ex)
     {
         if (ex.InnerExceptions.Count > 1)
         {
             throw;
         }
         else
         {
             throw ex.InnerException;
         }
     }
 }