Exemple #1
0
        public async Task <string> DoExportAsync(string blobUri, IProgress <int> progress)
        {
            //Setup Web Request for Export Operation
            WebRequest webRequest = WebRequest.Create(EndPointUri + @"/Export");

            webRequest.Method      = WebRequestMethods.Http.Post;
            webRequest.ContentType = @"application/xml";

            //Create Web Request Inputs - Blob Storage Credentials and Server Connection Info
            var exportInputs = new ExportInput
            {
                BlobCredentials = new BlobStorageAccessKeyCredentials
                {
                    StorageAccessKey = StorageKey,
                    Uri = String.Format(blobUri, DatabaseName, DateTime.UtcNow.Ticks)
                },
                ConnectionInfo = new ConnectionInfo
                {
                    ServerName   = ServerName,
                    DatabaseName = DatabaseName,
                    UserName     = UserName,
                    Password     = Password
                }
            };

            //Perform Web Request
            Stream webRequestStream = await webRequest.GetRequestStreamAsync();

            progress.Report(20);
            var dataContractSerializer = new DataContractSerializer(exportInputs.GetType());

            dataContractSerializer.WriteObject(webRequestStream, exportInputs);
            webRequestStream.Close();

            return(await ExportAwaiterAsync(webRequest, progress));
        }
        public string DoExport(ExportDatabaseEventSource log, string blobUri, bool whatIf)
        {
            string requestGuid = null;

            //Setup Web Request for Export Operation
            WebRequest webRequest = WebRequest.Create(this.EndPointUri + @"/Export");
            webRequest.Method = WebRequestMethods.Http.Post;
            webRequest.ContentType = @"application/xml";

            //Create Web Request Inputs - Blob Storage Credentials and Server Connection Info
            ExportInput exportInputs = new ExportInput
            {
                BlobCredentials = new BlobStorageAccessKeyCredentials
                {
                    StorageAccessKey = this.StorageKey,
                    Uri = String.Format(blobUri, this.DatabaseName, DateTime.UtcNow.Ticks.ToString())
                },
                ConnectionInfo = new ConnectionInfo
                {
                    ServerName = this.ServerName,
                    DatabaseName = this.DatabaseName,
                    UserName = this.UserName,
                    Password = this.Password
                }
            };

            //Perform Web Request
            DataContractSerializer dataContractSerializer = new DataContractSerializer(exportInputs.GetType());
            log.RequestUri(webRequest.RequestUri.AbsoluteUri);
            if (whatIf)
            {
                using (var strm = new MemoryStream())
                {
                    dataContractSerializer.WriteObject(strm, exportInputs);
                    strm.Flush();
                    strm.Seek(0, SeekOrigin.Begin);
                    using (var reader = new StreamReader(strm))
                    {
                        log.WouldHaveSent(reader.ReadToEnd());
                    }
                }
                return null;
            }
            else
            {
                using (var strm = new MemoryStream())
                {
                    dataContractSerializer.WriteObject(strm, exportInputs);
                    strm.Flush();
                    strm.Seek(0, SeekOrigin.Begin);
                    using (var reader = new StreamReader(strm))
                    {
                        log.SendingRequest(reader.ReadToEnd());
                    }
                }

                Stream webRequestStream = webRequest.GetRequestStream();
                dataContractSerializer.WriteObject(webRequestStream, exportInputs);
                webRequestStream.Close();

                //Get Response and Extract Request Identifier
                WebResponse webResponse = null;
                XmlReader xmlStreamReader = null;

                try
                {
                    //Initialize the WebResponse to the response from the WebRequest
                    webResponse = webRequest.GetResponse();

                    xmlStreamReader = XmlReader.Create(webResponse.GetResponseStream());
                    xmlStreamReader.ReadToFollowing("guid");
                    requestGuid = xmlStreamReader.ReadElementContentAsString();
                    return requestGuid;
                }
                catch (WebException responseException)
                {
                    log.RequestFailed(responseException.Message);
                    if (responseException.Response != null)
                    {
                        log.ErrorStatusCode((int)(((HttpWebResponse)responseException.Response).StatusCode));
                        log.ErrorStatusDescription(((HttpWebResponse)responseException.Response).StatusDescription);
                    }
                    return null;
                }
            }
        }
        public string DoExport(string blobUri, bool whatIf)
        {
            _log.Info("Starting SQL DAC Export Operation");
            string requestGuid = null;
            bool exportComplete = false;
            string exportedBlobPath = null;

            //Setup Web Request for Export Operation
            WebRequest webRequest = WebRequest.Create(this.EndPointUri + @"/Export");
            webRequest.Method = WebRequestMethods.Http.Post;
            webRequest.ContentType = @"application/xml";

            //Create Web Request Inputs - Blob Storage Credentials and Server Connection Info
            ExportInput exportInputs = new ExportInput
            {
                BlobCredentials = new BlobStorageAccessKeyCredentials
                {
                    StorageAccessKey = this.StorageKey,
                    Uri = String.Format(blobUri, this.DatabaseName, DateTime.UtcNow.Ticks.ToString())
                },
                ConnectionInfo = new ConnectionInfo
                {
                    ServerName = this.ServerName,
                    DatabaseName = this.DatabaseName,
                    UserName = this.UserName,
                    Password = this.Password
                }
            };

            //Perform Web Request
            DataContractSerializer dataContractSerializer = new DataContractSerializer(exportInputs.GetType());
            _log.Info("http POST {0}", webRequest.RequestUri.AbsoluteUri);
            if (whatIf)
            {
                _log.Trace("Would have sent:");

                using (var strm = new MemoryStream())
                {
                    dataContractSerializer.WriteObject(strm, exportInputs);
                    strm.Flush();
                    strm.Seek(0, SeekOrigin.Begin);
                    using (var reader = new StreamReader(strm))
                    {
                        _log.Trace(reader.ReadToEnd());
                    }
                }
                return null;
            }
            else
            {
                _log.Info("Making Web Request For Export Operation...");
                Stream webRequestStream = webRequest.GetRequestStream();
                dataContractSerializer.WriteObject(webRequestStream, exportInputs);
                webRequestStream.Close();

                //Get Response and Extract Request Identifier
                WebResponse webResponse = null;
                XmlReader xmlStreamReader = null;

                try
                {
                    //Initialize the WebResponse to the response from the WebRequest
                    webResponse = webRequest.GetResponse();

                    xmlStreamReader = XmlReader.Create(webResponse.GetResponseStream());
                    xmlStreamReader.ReadToFollowing("guid");
                    requestGuid = xmlStreamReader.ReadElementContentAsString();
                    _log.Info($"Export Request '{requestGuid}' submitted");

                    //Get Export Operation Status
                    string last = null;
                    while (!exportComplete)
                    {
                        List<StatusInfo> statusInfoList = CheckRequestStatus(requestGuid);
                        var status = statusInfoList.FirstOrDefault().Status;
                        if (!String.Equals(last, status, StringComparison.OrdinalIgnoreCase))
                        {
                            _log.Info(status);
                        }
                        last = status;

                        if (statusInfoList.FirstOrDefault().Status == "Failed")
                        {
                            _log.Error("Database export failed: {0}", statusInfoList.FirstOrDefault().ErrorMessage);
                            exportComplete = true;
                        }

                        if (statusInfoList.FirstOrDefault().Status == "Completed")
                        {
                            exportedBlobPath = statusInfoList.FirstOrDefault().BlobUri;
                            _log.Info("Export Complete - Database exported to: {0}", exportedBlobPath);
                            exportComplete = true;
                        }
                        Thread.Sleep(5 * 1000);
                    }
                    return exportedBlobPath;
                }
                catch (WebException responseException)
                {
                    _log.Error("Request Falied:{0}", responseException.Message);
                    if (responseException.Response != null)
                    {
                        _log.Error("Status Code: {0}", ((HttpWebResponse)responseException.Response).StatusCode);
                        _log.Error("Status Description: {0}", ((HttpWebResponse)responseException.Response).StatusDescription);
                    }
                    return null;
                }
            }
        }
Exemple #4
0
        public string DoExport(string blobUri, bool whatIf)
        {
            _log.Info("Starting SQL DAC Export Operation");
            string requestGuid      = null;
            bool   exportComplete   = false;
            string exportedBlobPath = null;

            //Setup Web Request for Export Operation
            WebRequest webRequest = WebRequest.Create(this.EndPointUri + @"/Export");

            webRequest.Method      = WebRequestMethods.Http.Post;
            webRequest.ContentType = @"application/xml";

            //Create Web Request Inputs - Blob Storage Credentials and Server Connection Info
            ExportInput exportInputs = new ExportInput
            {
                BlobCredentials = new BlobStorageAccessKeyCredentials
                {
                    StorageAccessKey = this.StorageKey,
                    Uri = String.Format(blobUri, this.DatabaseName, DateTime.UtcNow.Ticks.ToString())
                },
                ConnectionInfo = new ConnectionInfo
                {
                    ServerName   = this.ServerName,
                    DatabaseName = this.DatabaseName,
                    UserName     = this.UserName,
                    Password     = this.Password
                }
            };

            //Perform Web Request
            DataContractSerializer dataContractSerializer = new DataContractSerializer(exportInputs.GetType());

            _log.Info("http POST {0}", webRequest.RequestUri.AbsoluteUri);
            if (whatIf)
            {
                _log.Trace("Would have sent:");

                using (var strm = new MemoryStream())
                {
                    dataContractSerializer.WriteObject(strm, exportInputs);
                    strm.Flush();
                    strm.Seek(0, SeekOrigin.Begin);
                    using (var reader = new StreamReader(strm))
                    {
                        _log.Trace(reader.ReadToEnd());
                    }
                }
                return(null);
            }
            else
            {
                _log.Info("Making Web Request For Export Operation...");
                Stream webRequestStream = webRequest.GetRequestStream();
                dataContractSerializer.WriteObject(webRequestStream, exportInputs);
                webRequestStream.Close();

                //Get Response and Extract Request Identifier
                WebResponse webResponse     = null;
                XmlReader   xmlStreamReader = null;

                try
                {
                    //Initialize the WebResponse to the response from the WebRequest
                    webResponse = webRequest.GetResponse();

                    // See CA3053. Default XmlReaderSettings contains an insecure XmlResolver. Best to specify null.
                    XmlReaderSettings xmlSettings = new XmlReaderSettings();
                    xmlSettings.XmlResolver = null;
                    xmlStreamReader         = XmlReader.Create(webResponse.GetResponseStream(), xmlSettings);
                    xmlStreamReader.ReadToFollowing("guid");
                    requestGuid = xmlStreamReader.ReadElementContentAsString();
                    _log.Info($"Export Request '{requestGuid}' submitted");

                    //Get Export Operation Status
                    string last = null;
                    while (!exportComplete)
                    {
                        List <StatusInfo> statusInfoList = CheckRequestStatus(requestGuid);
                        var status = statusInfoList.FirstOrDefault().Status;
                        if (!String.Equals(last, status, StringComparison.OrdinalIgnoreCase))
                        {
                            _log.Info(status);
                        }
                        last = status;

                        if (statusInfoList.FirstOrDefault().Status == "Failed")
                        {
                            _log.Error("Database export failed: {0}", statusInfoList.FirstOrDefault().ErrorMessage);
                            exportComplete = true;
                        }

                        if (statusInfoList.FirstOrDefault().Status == "Completed")
                        {
                            exportedBlobPath = statusInfoList.FirstOrDefault().BlobUri;
                            _log.Info("Export Complete - Database exported to: {0}", exportedBlobPath);
                            exportComplete = true;
                        }
                        Thread.Sleep(5 * 1000);
                    }
                    return(exportedBlobPath);
                }
                catch (WebException responseException)
                {
                    _log.Error("Request Failed:{0}", responseException.Message);
                    if (responseException.Response != null)
                    {
                        _log.Error("Status Code: {0}", ((HttpWebResponse)responseException.Response).StatusCode);
                        _log.Error("Status Description: {0}", ((HttpWebResponse)responseException.Response).StatusDescription);
                    }
                    return(null);
                }
            }
        }
        public string DoExport(string blobUri)
        {
            logger.Info(String.Format("Starting Export Operation - {0}\n\r", DateTime.Now));
            string requestGuid = null;
            bool exportComplete = false;
            string exportedBlobPath = null;

            //Setup Web Request for Export Operation
            WebRequest webRequest = WebRequest.Create(this.EndPointUri + @"/Export");
            webRequest.Method = WebRequestMethods.Http.Post;
            webRequest.ContentType = @"application/xml";

            //Create Web Request Inputs - Blob Storage Credentials and Server Connection Info
            ExportInput exportInputs = new ExportInput
            {
                BlobCredentials = new BlobStorageAccessKeyCredentials
                {
                    StorageAccessKey = this.StorageKey,
                    Uri = String.Format(blobUri, this.DatabaseName, DateTime.UtcNow.Ticks.ToString())
                },
                ConnectionInfo = new ConnectionInfo
                {
                    ServerName = this.ServerName,
                    DatabaseName = this.DatabaseName,
                    UserName = this.UserName,
                    Password = this.Password
                }
            };

            //Perform Web Request
            logger.Info("Making Web Request For Export Operation...");
            Stream webRequestStream = webRequest.GetRequestStream();
            DataContractSerializer dataContractSerializer = new DataContractSerializer(exportInputs.GetType());
            dataContractSerializer.WriteObject(webRequestStream, exportInputs);
            webRequestStream.Close();

            //Get Response and Extract Request Identifier
            logger.Info("Reading Response and extracting Export Request Identifier...");
            WebResponse webResponse = null;
            XmlReader xmlStreamReader = null;

            try
            {
                //Initialize the WebResponse to the response from the WebRequest
                webResponse = webRequest.GetResponse();

                xmlStreamReader = XmlReader.Create(webResponse.GetResponseStream());
                xmlStreamReader.ReadToFollowing("guid");
                requestGuid = xmlStreamReader.ReadElementContentAsString();
                logger.Info(String.Format("Your Export Request Guid is: {0}", requestGuid));

                //Get Export Operation Status
                while (!exportComplete)
                {
                    logger.Info("Checking export status...");
                    List<StatusInfo> statusInfoList = CheckRequestStatus(requestGuid);
                    StatusInfo statusInfo = statusInfoList.FirstOrDefault();
                    logger.Info(statusInfo.Status);

                    if (statusInfo.Status == "Failed")
                    {
                        logger.Info(String.Format("Database export failed: {0}", statusInfo.ErrorMessage));
                        exportComplete = true;
                    }

                    if (statusInfo.Status == "Completed")
                    {
                        exportedBlobPath = statusInfo.BlobUri;
                        logger.Info(String.Format("Export Complete - Database exported to: {0}\n\r", exportedBlobPath));
                        exportComplete = true;
                    }

                    if (!exportComplete)
                    {
                        Thread.Sleep(3000);
                    }
                }

                return exportedBlobPath;
            }
            catch (WebException responseException)
            {
                logger.ErrorFormat("Request Falied:{0}", responseException,responseException.Message);
                if (responseException.Response != null)
                {
                    logger.ErrorFormat("Status Code: {0}", ((HttpWebResponse)responseException.Response).StatusCode);
                    logger.ErrorFormat("Status Description: {0}\n\r", ((HttpWebResponse)responseException.Response).StatusDescription);
                }
                return null;
            }
        }
        public string DoExport(string blobUri)
        {
            logger.Info(String.Format("Starting Export Operation - {0}\n\r", DateTime.Now));
            string requestGuid      = null;
            bool   exportComplete   = false;
            string exportedBlobPath = null;

            //Setup Web Request for Export Operation
            WebRequest webRequest = WebRequest.Create(this.EndPointUri + @"/Export");

            webRequest.Method      = WebRequestMethods.Http.Post;
            webRequest.ContentType = @"application/xml";

            //Create Web Request Inputs - Blob Storage Credentials and Server Connection Info
            ExportInput exportInputs = new ExportInput
            {
                BlobCredentials = new BlobStorageAccessKeyCredentials
                {
                    StorageAccessKey = this.StorageKey,
                    Uri = String.Format(blobUri, this.DatabaseName, DateTime.UtcNow.Ticks.ToString())
                },
                ConnectionInfo = new ConnectionInfo
                {
                    ServerName   = this.ServerName,
                    DatabaseName = this.DatabaseName,
                    UserName     = this.UserName,
                    Password     = this.Password
                }
            };

            //Perform Web Request
            logger.Info("Making Web Request For Export Operation...");
            Stream webRequestStream = webRequest.GetRequestStream();
            DataContractSerializer dataContractSerializer = new DataContractSerializer(exportInputs.GetType());

            dataContractSerializer.WriteObject(webRequestStream, exportInputs);
            webRequestStream.Close();

            //Get Response and Extract Request Identifier
            logger.Info("Reading Response and extracting Export Request Identifier...");
            WebResponse webResponse     = null;
            XmlReader   xmlStreamReader = null;

            try
            {
                //Initialize the WebResponse to the response from the WebRequest
                webResponse = webRequest.GetResponse();

                xmlStreamReader = XmlReader.Create(webResponse.GetResponseStream());
                xmlStreamReader.ReadToFollowing("guid");
                requestGuid = xmlStreamReader.ReadElementContentAsString();
                logger.Info(String.Format("Your Export Request Guid is: {0}", requestGuid));

                //Get Export Operation Status
                while (!exportComplete)
                {
                    logger.Info("Checking export status...");
                    List <StatusInfo> statusInfoList = CheckRequestStatus(requestGuid);
                    StatusInfo        statusInfo     = statusInfoList.FirstOrDefault();
                    logger.Info(statusInfo.Status);

                    if (statusInfo.Status == "Failed")
                    {
                        logger.Info(String.Format("Database export failed: {0}", statusInfo.ErrorMessage));
                        exportComplete = true;
                    }

                    if (statusInfo.Status == "Completed")
                    {
                        exportedBlobPath = statusInfo.BlobUri;
                        logger.Info(String.Format("Export Complete - Database exported to: {0}\n\r", exportedBlobPath));
                        exportComplete = true;
                    }

                    if (!exportComplete)
                    {
                        Thread.Sleep(3000);
                    }
                }

                return(exportedBlobPath);
            }
            catch (WebException responseException)
            {
                logger.ErrorFormat("Request Falied:{0}", responseException, responseException.Message);
                if (responseException.Response != null)
                {
                    logger.ErrorFormat("Status Code: {0}", ((HttpWebResponse)responseException.Response).StatusCode);
                    logger.ErrorFormat("Status Description: {0}\n\r", ((HttpWebResponse)responseException.Response).StatusDescription);
                }
                return(null);
            }
        }
        public string DoExport(ExportDatabaseEventSource log, string blobUri, bool whatIf)
        {
            string requestGuid = null;

            //Setup Web Request for Export Operation
            WebRequest webRequest = WebRequest.Create(this.EndPointUri + @"/Export");

            webRequest.Method      = WebRequestMethods.Http.Post;
            webRequest.ContentType = @"application/xml";

            //Create Web Request Inputs - Blob Storage Credentials and Server Connection Info
            ExportInput exportInputs = new ExportInput
            {
                BlobCredentials = new BlobStorageAccessKeyCredentials
                {
                    StorageAccessKey = this.StorageKey,
                    Uri = String.Format(blobUri, this.DatabaseName, DateTime.UtcNow.Ticks.ToString())
                },
                ConnectionInfo = new ConnectionInfo
                {
                    ServerName   = this.ServerName,
                    DatabaseName = this.DatabaseName,
                    UserName     = this.UserName,
                    Password     = this.Password
                }
            };

            //Perform Web Request
            DataContractSerializer dataContractSerializer = new DataContractSerializer(exportInputs.GetType());

            log.RequestUri(webRequest.RequestUri.AbsoluteUri);
            if (whatIf)
            {
                using (var strm = new MemoryStream())
                {
                    dataContractSerializer.WriteObject(strm, exportInputs);
                    strm.Flush();
                    strm.Seek(0, SeekOrigin.Begin);
                    using (var reader = new StreamReader(strm))
                    {
                        log.WouldHaveSent(reader.ReadToEnd());
                    }
                }
                return(null);
            }
            else
            {
                using (var strm = new MemoryStream())
                {
                    dataContractSerializer.WriteObject(strm, exportInputs);
                    strm.Flush();
                    strm.Seek(0, SeekOrigin.Begin);
                    using (var reader = new StreamReader(strm))
                    {
                        log.SendingRequest(reader.ReadToEnd());
                    }
                }

                Stream webRequestStream = webRequest.GetRequestStream();
                dataContractSerializer.WriteObject(webRequestStream, exportInputs);
                webRequestStream.Close();

                //Get Response and Extract Request Identifier
                WebResponse webResponse     = null;
                XmlReader   xmlStreamReader = null;

                try
                {
                    //Initialize the WebResponse to the response from the WebRequest
                    webResponse = webRequest.GetResponse();

                    xmlStreamReader = XmlReader.Create(webResponse.GetResponseStream());
                    xmlStreamReader.ReadToFollowing("guid");
                    requestGuid = xmlStreamReader.ReadElementContentAsString();
                    return(requestGuid);
                }
                catch (WebException responseException)
                {
                    log.RequestFailed(responseException.Message);
                    if (responseException.Response != null)
                    {
                        log.ErrorStatusCode((int)(((HttpWebResponse)responseException.Response).StatusCode));
                        log.ErrorStatusDescription(((HttpWebResponse)responseException.Response).StatusDescription);
                    }
                    return(null);
                }
            }
        }