@@ -38,9 +38,7 @@ public class PowerShellContext : IDisposable, IHostSupportsInteractiveSession
3838 private RunspaceDetails initialRunspace ;
3939 private SessionDetails mostRecentSessionDetails ;
4040
41- private IConsoleHost consoleHost ;
4241 private ProfilePaths profilePaths ;
43- private ConsoleServicePSHost psHost ;
4442
4543 private IVersionSpecificOperations versionSpecificOperations ;
4644
@@ -94,15 +92,7 @@ public PowerShellVersionDetails LocalPowerShellVersion
9492 /// Gets or sets an IConsoleHost implementation for use in
9593 /// writing output to the console.
9694 /// </summary>
97- internal IConsoleHost ConsoleHost
98- {
99- get { return this . consoleHost ; }
100- set
101- {
102- this . consoleHost = value ;
103- this . psHost . ConsoleHost = value ;
104- }
105- }
95+ private IConsoleHost ConsoleHost { get ; set ; }
10696
10797 /// <summary>
10898 /// Gets details pertaining to the current runspace.
@@ -125,42 +115,18 @@ public string PromptString
125115
126116 #region Constructors
127117
128- /// <summary>
129- /// Initializes a new instance of the PowerShellContext class and
130- /// opens a runspace to be used for the session.
131- /// </summary>
132- public PowerShellContext ( ) : this ( ( HostDetails ) null , null )
133- {
134- }
135-
136- /// <summary>
137- /// Initializes a new instance of the PowerShellContext class and
138- /// opens a runspace to be used for the session.
139- /// </summary>
140- /// <param name="hostDetails">Provides details about the host application.</param>
141- /// <param name="profilePaths">An object containing the profile paths for the session.</param>
142- public PowerShellContext ( HostDetails hostDetails , ProfilePaths profilePaths )
143- : this ( hostDetails , profilePaths , false )
144- {
145- }
146-
147- /// <summary>
148- /// Initializes a new instance of the PowerShellContext class and
149- /// opens a runspace to be used for the session.
150- /// </summary>
151- /// <param name="hostDetails">Provides details about the host application.</param>
152- /// <param name="profilePaths">An object containing the profile paths for the session.</param>
153- /// <param name="enableConsoleRepl">
154- /// Enables a terminal-based REPL for this session.
155- /// </param>
156- public PowerShellContext (
118+ public static Runspace CreateRunspace (
157119 HostDetails hostDetails ,
158- ProfilePaths profilePaths ,
120+ PowerShellContext powerShellContext ,
159121 bool enableConsoleRepl )
160122 {
161- hostDetails = hostDetails ?? HostDetails . Default ;
123+ var psHost = new ConsoleServicePSHost ( powerShellContext , hostDetails , enableConsoleRepl ) ;
124+ powerShellContext . ConsoleHost = psHost . ConsoleService ;
125+ return CreateRunspace ( psHost ) ;
126+ }
162127
163- this . psHost = new ConsoleServicePSHost ( hostDetails , this , enableConsoleRepl ) ;
128+ public static Runspace CreateRunspace ( PSHost psHost )
129+ {
164130 var initialSessionState = InitialSessionState . CreateDefault2 ( ) ;
165131
166132 Runspace runspace = RunspaceFactory . CreateRunspace ( psHost , initialSessionState ) ;
@@ -170,9 +136,7 @@ public PowerShellContext(
170136 runspace . ThreadOptions = PSThreadOptions . ReuseThread ;
171137 runspace . Open ( ) ;
172138
173- this . ownsInitialRunspace = true ;
174-
175- this . Initialize ( profilePaths , runspace ) ;
139+ return runspace ;
176140 }
177141
178142 /// <summary>
@@ -181,16 +145,34 @@ public PowerShellContext(
181145 /// </summary>
182146 /// <param name="profilePaths">An object containing the profile paths for the session.</param>
183147 /// <param name="initialRunspace">The initial runspace to use for this instance.</param>
184- public PowerShellContext ( ProfilePaths profilePaths , Runspace initialRunspace )
148+ /// <param name="ownsInitialRunspace">If true, the PowerShellContext owns this runspace.</param>
149+ public void Initialize (
150+ ProfilePaths profilePaths ,
151+ Runspace initialRunspace ,
152+ bool ownsInitialRunspace )
185153 {
186- this . Initialize ( profilePaths , initialRunspace ) ;
154+ this . Initialize ( profilePaths , initialRunspace , ownsInitialRunspace , null ) ;
187155 }
188156
189- private void Initialize ( ProfilePaths profilePaths , Runspace initialRunspace )
157+ /// <summary>
158+ /// Initializes a new instance of the PowerShellContext class using
159+ /// an existing runspace for the session.
160+ /// </summary>
161+ /// <param name="profilePaths">An object containing the profile paths for the session.</param>
162+ /// <param name="initialRunspace">The initial runspace to use for this instance.</param>
163+ /// <param name="ownsInitialRunspace">If true, the PowerShellContext owns this runspace.</param>
164+ /// <param name="consoleHost">An IConsoleHost implementation. Optional.</param>
165+ public void Initialize (
166+ ProfilePaths profilePaths ,
167+ Runspace initialRunspace ,
168+ bool ownsInitialRunspace ,
169+ IConsoleHost consoleHost )
190170 {
191171 Validate . IsNotNull ( "initialRunspace" , initialRunspace ) ;
192172
173+ this . ownsInitialRunspace = ownsInitialRunspace ;
193174 this . SessionState = PowerShellContextState . NotStarted ;
175+ this . ConsoleHost = consoleHost ;
194176
195177 // Get the PowerShell runtime version
196178 this . LocalPowerShellVersion =
0 commit comments