public void Test_CreateSetParametersXml()
        {
            MSDeployDeploymentParameters parameters = new MSDeployDeploymentParameters();
            parameters.Parameters.Add(@"IIS Web Application Name", @"Default Web Site/Foobar");
            parameters.Parameters.Add(@"ApplicationServices-Web.config Connection String",@"data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true");

            MSDeployHandler handler = new MSDeployHandler();
            string filePath = handler.CreateSetParametersXml(parameters);

            Assert.IsNotNull(filePath);
            // load the xml file and make sure the parameters made it
            XDocument doc = XDocument.Load(filePath);

            Assert.IsNotNull(doc);
            var xmlParams = from e in doc.Root.Descendants("setParameter")
                            select e;

            Assert.AreEqual(2, xmlParams.Count());
            Assert.IsTrue(
                string.Compare(@"Default Web Site/Foobar",
                GetSingleValuedStringFromXPath(doc,@"/parameters/setParameter[@name='IIS Web Application Name']/@value"), StringComparison.OrdinalIgnoreCase) == 0);
            Assert.IsTrue(
                string.Compare(@"data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true",
                GetSingleValuedStringFromXPath(doc, @"/parameters/setParameter[@name='ApplicationServices-Web.config Connection String']/@value"), StringComparison.OrdinalIgnoreCase) == 0);
        }
        internal string CreateSetParametersXml(MSDeployDeploymentParameters parmeters)
        {
            if (parmeters == null) { throw new ArgumentNullException("parmeters"); }
            //<?xml version="1.0" encoding="utf-8"?>
            //<parameters>
            //  <setParameter name="IIS Web Application Name"
            //                value="Default Web Site/WebApplication6_deploy" />
            //  <setParameter name="ApplicationServices-Web.config Connection String"
            //                value="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" />
            //</parameters>

            XDocument doc = new XDocument(
                new XElement("parameters",
                    from p in parmeters.Parameters
                    select new XElement("setParameter",
                        new XAttribute("name", p.Key),
                        new XAttribute("value", p.Value))));

            string fileToCreate = PathExtensions.GetTempFileWithExtension(".xml");
            log.Debug(string.Format("Creating setparameters.xml at [{0}]", fileToCreate));
            doc.Save(fileToCreate);

            return fileToCreate;
        }
        public void HandleDeployment(Package package, string deployParameters)
        {
            if (package == null) { throw new ArgumentNullException("package"); }

            MSDeployDeploymentParameters param = new MSDeployDeploymentParameters();
            if (!string.IsNullOrWhiteSpace(deployParameters)) {
                param = this.Searlizer.Desearlize<MSDeployDeploymentParameters>(deployParameters);
            }

            string pathToFile = null;
            // for now this only hanldes file://
            string pkgScheme = package.PackageLocation.Scheme;
            if (string.Compare(pkgScheme, KnownUriSchemeTypes.file.ToString(), StringComparison.OrdinalIgnoreCase) == 0) {
                pathToFile = package.PackageLocation.AbsolutePath;
            }
            else if (string.Compare(pkgScheme, KnownUriSchemeTypes.http.ToString(), StringComparison.OrdinalIgnoreCase) == 0 ||
                    string.Compare(pkgScheme, KnownUriSchemeTypes.https.ToString(), StringComparison.OrdinalIgnoreCase) == 0) {
                // pathToFile = await this.DownloadFileToLocalFile(package.PackageLocation);
                pathToFile = this.DownloadFileToLocalFile(package.PackageLocation);
            }
            else {
                string message = string.Format("Unknown URI scheme: [{0}]", package.PackageLocation.Scheme);
                throw new UnknownPackageUriSchemeException(message);
            }

            // we have to create the setparameters.xml file
            string parametersFile = this.CreateSetParametersXml(param);

            // now let's construct the command which needs to be executed
            string pathToMsdeployExe = this.GetPathToMsdeploy();
            if (string.IsNullOrWhiteSpace(pathToMsdeployExe)) {
                string message = string.Format("Unable to locate msdeploy.exe");
                throw new RequiredValueNotFoundException(message);
            }

            // msdeploy.exe -verb:sync -source:package={path-to-package} -dest:auto -setParamFile={path-to-setParam.xml}
            string args = string.Format(
                @"-verb:sync -source:package=""{0}"" -dest:auto -setParamFile=""{1}"" ",
                pathToFile,
                parametersFile
            );

            log.DebugFormat("Calling msdeploy.exe with: [{0}] {1}", args, Environment.NewLine);

            ProcessStartInfo psi = new ProcessStartInfo {
                CreateNoWindow = true,
                UseShellExecute = false,
                RedirectStandardError = true,
                RedirectStandardInput = true,

                Arguments = args,
                FileName = pathToMsdeployExe,
                LoadUserProfile = true,
            };

            Process process = Process.Start(psi);
            bool processHasExited = process.WaitForExit(param.MsdeployTimeout);
            if (!processHasExited) {
                string message = string.Format("msdeploy process has not exited in the given timeout [{0}]. Kiling the process", param.MsdeployTimeout);
                process.Kill();
                throw new TimeoutExceededException(message);
            }

            if (process.ExitCode != 0) {
                string message = string.Format("There was an error inovking msdeploy.exe. ExitCode = [{0}]",process.ExitCode);
                log.Error(message);
                throw new PublishException(message);
            }

            log.Debug("msdeploy.exe has exited");
        }