Esempio n. 1
0
        internal override async Task StartAsync(Site site)
        {
            var name     = site.Applications[0].ApplicationPoolName;
            var pool     = ApplicationPools.FirstOrDefault(item => item.Name == name);
            var fileName =
                Path.Combine(
                    Environment.GetFolderPath(
                        pool != null && pool.Enable32BitAppOnWin64
                            ? Environment.SpecialFolder.ProgramFilesX86
                            : Environment.SpecialFolder.ProgramFiles),
                    "IIS Express",
                    "iisexpress.exe");

            if (!File.Exists(fileName))
            {
                fileName = Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
                    "IIS Express",
                    "iisexpress.exe");
            }

            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName               = fileName,
                    Arguments              = site.CommandLine,
                    WindowStyle            = ProcessWindowStyle.Hidden,
                    UseShellExecute        = false,
                    CreateNoWindow         = true,
                    RedirectStandardOutput = true
                }
            };

            try
            {
                process.Start();
                process.WaitForExit(5000);
                if (process.HasExited)
                {
                    throw new InvalidOperationException("process terminated");
                }

                site.State = ObjectState.Started;
            }
            catch (Exception ex)
            {
                throw new COMException(
                          string.Format("cannot start site: {0}, {1}", ex.Message, process.StandardOutput.ReadToEnd()));
            }
            finally
            {
                site.State = process.HasExited ? ObjectState.Stopped : ObjectState.Started;
            }
        }
        private void StartInner(Site site, bool restart)
        {
            var name     = site.Applications[0].ApplicationPoolName;
            var pool     = ApplicationPools.FirstOrDefault(item => item.Name == name);
            var fileName =
                Path.Combine(
                    Environment.GetFolderPath(
                        pool != null && pool.Enable32BitAppOnWin64
                            ? Environment.SpecialFolder.ProgramFilesX86
                            : Environment.SpecialFolder.ProgramFiles),
                    "IIS Express",
                    "iisexpress.exe");

            if (!File.Exists(fileName))
            {
                fileName = Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
                    "IIS Express",
                    "iisexpress.exe");
            }

            var temp = Path.GetTempFileName();

            using (var process = new Process())
            {
                var start = process.StartInfo;
                start.Verb = site.Bindings.ElevationRequired && !PublicNativeMethods.IsProcessElevated
                    ? "runas"
                    : null;
                start.FileName = "cmd";
                var extra = restart ? "/r" : string.Empty;
                start.Arguments =
                    $"/c \"\"{Path.Combine(Environment.CurrentDirectory, "certificateinstaller.exe")}\" /launcher:\"{fileName}\" /config:\"{site.FileContext.FileName}\" /siteId:{site.Id} /resultFile:\"{temp}\"\" {extra}";
                start.CreateNoWindow = true;
                start.WindowStyle    = ProcessWindowStyle.Hidden;
                InjectEnvironmentVariables(site, start);

                try
                {
                    process.Start();
                    process.WaitForExit();
                    if (process.ExitCode == 0)
                    {
                        site.State = ObjectState.Started;
                    }
                    else if (process.ExitCode == 1)
                    {
                        throw new InvalidOperationException("The process has terminated");
                    }
                }
                catch (Win32Exception ex)
                {
                    // elevation is cancelled.
                    if (ex.NativeErrorCode != NativeMethods.ErrorCancelled)
                    {
                        throw new COMException(
                                  $"cannot start site: {ex.Message}, {File.ReadAllText(temp)}");
                    }

                    throw new COMException(
                              $"site start cancelled: {ex.Message}, {File.ReadAllText(temp)}");
                }
                catch (Exception ex)
                {
                    throw new COMException(
                              $"cannot start site: {ex.Message}, {File.ReadAllText(temp)}");
                }
                finally
                {
                    site.State = process.ExitCode == 0 ? ObjectState.Started : ObjectState.Stopped;
                }
            }
        }