Exemple #1
0
 public void CanExecuteCode()
 {
     using (var repl = new ProcessWrapper())
     {
         Assert.Equal("[(4, 20)]", repl.Execute(@"Tuple.Create(4,20);"));
         repl.Kill();
     }
 }
Exemple #2
0
        public void CantAssertNewPermissions()
        {
            if (File.Exists("c:\\test.txt"))
                File.Delete("c:\\test.txt");

            using (var repl = new ProcessWrapper())
            {
                repl.Execute(@"new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted).Assert(); System.IO.File.WriteAllText(@""c:\test.txt"", ""test"");");
                repl.Kill();
                Assert.False(File.Exists("c:\\test.txt"));
            }
        }
Exemple #3
0
        public void CantWriteFile()
        {
            if (File.Exists("c:\\test.txt"))
                File.Delete("c:\\test.txt");

            using (var repl = new ProcessWrapper())
            {
                repl.Execute(@"System.IO.File.WriteAllText(@""c:\test.txt"", ""test"");");
                repl.Kill();

                Assert.False(File.Exists("c:\\test.txt"));
            }
        }
Exemple #4
0
        public void Stop()
        {
            if (process != null)
            {
                process.OutputStreamChanged -= ProcessOutputStreamChanged;
                process.ErrorStreamChanged  -= ProcessErrorStreamChanged;

                if (!process.HasExited)
                {
                    process.Kill();
                }
                process = null;
            }
        }
Exemple #5
0
        void Disconnect()
        {
            if (process == null)
            {
                return;
            }

            log.WriteConsoleLogText("\nDisconnected\n");

            if (!process.HasExited)
            {
                process.Kill();
            }
            else if (process.ExitCode != 0)
            {
                log.WriteError(string.Format("Unknown error {0}\n", process.ExitCode));
            }

            process.Dispose();

            process = null;
        }
Exemple #6
0
        /// <summary>
        /// Stops the associated service app.
        /// </summary>
        /// <param name="isApplicationExiting">Set to true to prevent the service app from recovering from a dirty
        /// state on stop since the application will be exiting.</param>
        public virtual void Stop(bool isApplicationExiting)
        {
            _canRecreateProcess = !isApplicationExiting;

            try
            {
                _process.StandardInput.WriteLine(JsonConvert.SerializeObject(new { action = "stop" }));

                // we will need to be doubly sure that the process has ended.
                // ---
                // Would have preferred Process.WaitForExit but we also need to synchronize with the other thread
                // to process the "I have stopped" state change from the process. There probably is a better way
                // of doing this...
                int attempts = 5;
                while (attempts > 0)
                {
                    if (IsProcessRunning || ServiceApp.CurrentState != ServiceAppState.NotLoaded)
                    {
                        Thread.Sleep(ProcessWaitLimit / attempts);
                        attempts--;
                    }
                    else
                    {
                        break;
                    }
                }

                if (attempts <= 0)
                {
                    try
                    {
                        _process.Kill();
                    }
                    finally
                    {
                        AddMessage("Service app force stopped.", ServiceAppState.NotLoaded);
                    }
                }

                // stop listening on the client out stream
                _process.StandardOutput.Close();
            }
            catch (InvalidOperationException ioe)
            {
                // the process must have died already but the state of this probably did not update correctly
                _log.Debug(ioe);
            }
            catch (IOException ioex)
            {
                // this means that the "pipes are broken"
                _log.Debug(ioex);
            }

            if (_canRecreateProcess)
            {
                if (!IsProcessRunning)
                {
                    if (ServiceApp.CurrentState != ServiceAppState.NotLoaded)
                    {
                        // getting to this point usually means that the app was stopped before the "stop"
                        // state was emitted and processed.
                        AddMessage("Process exited unexpectedly.", ServiceAppState.NotLoaded);
                    }

                    // This is done in here to ensure that the object is never left in a dirty state.
                    // This is fine because the process is not running yet and just exists as a .NET object
                    // so as to prevent null reference exceptions.
                    CreateProcess();
                }
                else
                {
                    throw new ApplicationException("Cannot re-create the service app's process as it failed to exit.");
                }
            }
        }