Exemplo n.º 1
0
        /// <summary>
        /// Helper function used for opening a runspace
        /// </summary>
        private void DoOpenHelper()
        {
            // NTRAID#Windows Out Of Band Releases-915851-2005/09/13
            if (_disposed)
            {
                throw PSTraceSource.NewObjectDisposedException("runspace");
            }

            bool startLifeCycleEventWritten = false;
            s_runspaceInitTracer.WriteLine("begin open runspace");
            try
            {
                _transcriptionData = new TranscriptionData();

                if (InitialSessionState != null)
                {
                    // All ISS-based configuration of the engine itself is done by AutomationEngine,
                    // which calls InitialSessionState.Bind(). Anything that doesn't
                    // require an active and open runspace should be done in ISS.Bind()
                    _engine = new AutomationEngine(Host, null, InitialSessionState);
                }
                else
                {
                    _engine = new AutomationEngine(Host, RunspaceConfiguration, null);
                }
                _engine.Context.CurrentRunspace = this;

                //Log engine for start of engine life
                MshLog.LogEngineLifecycleEvent(_engine.Context, EngineState.Available);
                startLifeCycleEventWritten = true;

                _commandFactory = new CommandFactory(_engine.Context);
                _history = new History(_engine.Context);
                _jobRepository = new JobRepository();
                _jobManager = new JobManager();
                _runspaceRepository = new RunspaceRepository();

                s_runspaceInitTracer.WriteLine("initializing built-in aliases and variable information");
                InitializeDefaults();
            }
            catch (Exception exception)
            {
                CommandProcessorBase.CheckForSevereException(exception);
                s_runspaceInitTracer.WriteLine("Runspace open failed");

                //Log engine health event
                LogEngineHealthEvent(exception);

                //Log engine for end of engine life
                if (startLifeCycleEventWritten)
                {
                    Dbg.Assert(_engine.Context != null, "if startLifeCycleEventWritten is true, ExecutionContext must be present");
                    MshLog.LogEngineLifecycleEvent(_engine.Context, EngineState.Stopped);
                }

                //Open failed. Set the RunspaceState to Broken.
                SetRunspaceState(RunspaceState.Broken, exception);

                //Raise the event
                RaiseRunspaceStateEvents();

                //Rethrow the exception. For asynchronous execution, 
                //OpenThreadProc will catch it. For synchronous execution
                //caller of open will catch it.
                throw;
            }

            SetRunspaceState(RunspaceState.Opened);
            RunspaceOpening.Set();

            //Raise the event
            RaiseRunspaceStateEvents();
            s_runspaceInitTracer.WriteLine("runspace opened successfully");

            // Now do initial state configuration that requires an active runspace
            if (InitialSessionState != null)
            {
                Exception initError = InitialSessionState.BindRunspace(this, s_runspaceInitTracer);
                if (initError != null)
                {
                    // Log engine health event
                    LogEngineHealthEvent(initError);

                    // Log engine for end of engine life
                    Debug.Assert(_engine.Context != null,
                                "if startLifeCycleEventWritten is true, ExecutionContext must be present");
                    MshLog.LogEngineLifecycleEvent(_engine.Context, EngineState.Stopped);

                    // Open failed. Set the RunspaceState to Broken.
                    SetRunspaceState(RunspaceState.Broken, initError);

                    // Raise the event
                    RaiseRunspaceStateEvents();

                    // Throw the exception. For asynchronous execution, 
                    // OpenThreadProc will catch it. For synchronous execution
                    // caller of open will catch it.
                    throw initError;
                }
            }

            TelemetryAPI.ReportLocalSessionCreated(InitialSessionState, TranscriptionData);
        }
Exemplo n.º 2
0
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (_disposed)
                {
                    return;
                }
                lock (SyncRoot)
                {
                    if (_disposed)
                    {
                        return;
                    }

                    _disposed = true;
                }

                if (disposing)
                {
                    Close();
                    _engine = null;
                    _history = null;
                    _transcriptionData = null;
                    _jobManager = null;
                    _jobRepository = null;
                    _runspaceRepository = null;
                    if (RunspaceOpening != null)
                    {
                        RunspaceOpening.Dispose();
                        RunspaceOpening = null;
                    }

                    // Dispose the event manager
                    if (this.ExecutionContext != null && this.ExecutionContext.Events != null)
                    {
                        try
                        {
                            this.ExecutionContext.Events.Dispose();
                        }
                        catch (ObjectDisposedException)
                        {
                            ;
                        }
                    }
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }