@@ -15,36 +15,36 @@ public abstract class OutOfProcessNodeInstance : INodeServices {
1515 private string _commandLineArguments ;
1616 private Process _nodeProcess ;
1717 private TaskCompletionSource < bool > _nodeProcessIsReadySource ;
18-
18+
1919 protected Process NodeProcess {
2020 get {
2121 // This is only exposed to support the unreliable OutOfProcessNodeRunner, which is just to verify that
2222 // other hosting/transport mechanisms are possible. This shouldn't really be exposed.
2323 return this . _nodeProcess ;
2424 }
2525 }
26-
26+
2727 public OutOfProcessNodeInstance ( string entryPointScript , string commandLineArguments = null )
2828 {
2929 this . _childProcessLauncherLock = new object ( ) ;
3030 this . _entryPointScript = new StringAsTempFile ( entryPointScript ) ;
3131 this . _commandLineArguments = commandLineArguments ?? string . Empty ;
3232 }
33-
33+
3434 public abstract Task < string > Invoke ( NodeInvocationInfo invocationInfo ) ;
35-
35+
3636 public Task < string > Invoke ( string moduleName , params object [ ] args ) {
3737 return this . InvokeExport ( moduleName , null , args ) ;
3838 }
39-
39+
4040 public async Task < string > InvokeExport ( string moduleName , string exportedFunctionName , params object [ ] args ) {
4141 return await this . Invoke ( new NodeInvocationInfo {
4242 ModuleName = moduleName ,
4343 ExportedFunctionName = exportedFunctionName ,
4444 Args = args
4545 } ) ;
4646 }
47-
47+
4848 protected async Task EnsureReady ( ) {
4949 lock ( this . _childProcessLauncherLock ) {
5050 if ( this . _nodeProcess == null || this . _nodeProcess . HasExited ) {
@@ -55,36 +55,44 @@ protected async Task EnsureReady() {
5555 RedirectStandardOutput = true ,
5656 RedirectStandardError = true
5757 } ;
58-
58+
5959 // Append current directory to NODE_PATH so it can locate node_modules
6060 var existingNodePath = Environment . GetEnvironmentVariable ( "NODE_PATH" ) ?? string . Empty ;
6161 if ( existingNodePath != string . Empty ) {
6262 existingNodePath += ":" ;
6363 }
64-
64+
6565 var nodePathValue = existingNodePath + Path . Combine ( Directory . GetCurrentDirectory ( ) , "node_modules" ) ;
6666 #if DNX451
6767 startInfo . EnvironmentVariables . Add ( "NODE_PATH" , nodePathValue ) ;
6868 #else
6969 startInfo . Environment . Add ( "NODE_PATH" , nodePathValue ) ;
7070 #endif
71-
71+
7272 this . OnBeforeLaunchProcess ( ) ;
7373 this . _nodeProcess = Process . Start ( startInfo ) ;
7474 this . ConnectToInputOutputStreams ( ) ;
7575 }
7676 }
77-
78- var initializationSucceeded = await this . _nodeProcessIsReadySource . Task ;
77+
78+ var task = this . _nodeProcessIsReadySource . Task ;
79+
80+ var initializationSucceeded = task
81+ . GetAwaiter ( )
82+ . GetResult ( ) ;
83+
7984 if ( ! initializationSucceeded ) {
80- throw new InvalidOperationException ( "The Node.js process failed to initialize" ) ;
85+ throw new InvalidOperationException ( "The Node.js process failed to initialize" , task . Exception ) ;
86+ }
87+ else {
88+ Console . WriteLine ( "Started" ) ;
8189 }
8290 }
83-
91+
8492 private void ConnectToInputOutputStreams ( ) {
8593 var initializationIsCompleted = false ; // TODO: Make this thread-safe? (Interlocked.Exchange etc.)
8694 this . _nodeProcessIsReadySource = new TaskCompletionSource < bool > ( ) ;
87-
95+
8896 this . _nodeProcess . OutputDataReceived += ( sender , evt ) => {
8997 if ( evt . Data == "[Microsoft.AspNet.NodeServices:Listening]" && ! initializationIsCompleted ) {
9098 this . _nodeProcessIsReadySource . SetResult ( true ) ;
@@ -103,18 +111,18 @@ private void ConnectToInputOutputStreams() {
103111 }
104112 }
105113 } ;
106-
114+
107115 this . _nodeProcess . BeginOutputReadLine ( ) ;
108- this . _nodeProcess . BeginErrorReadLine ( ) ;
116+ this . _nodeProcess . BeginErrorReadLine ( ) ;
109117 }
110-
118+
111119 protected virtual void OnBeforeLaunchProcess ( ) {
112120 }
113121
114122 protected virtual void OnOutputDataReceived ( string outputData ) {
115123 Console . WriteLine ( "[Node] " + outputData ) ;
116124 }
117-
125+
118126 protected virtual void OnErrorDataReceived ( string errorData ) {
119127 Console . WriteLine ( "[Node] " + errorData ) ;
120128 }
@@ -124,18 +132,19 @@ public void Dispose()
124132 Dispose ( true ) ;
125133 GC . SuppressFinalize ( this ) ;
126134 }
127-
135+
128136 protected virtual void Dispose ( bool disposing )
129137 {
130138 if ( ! disposed ) {
131139 if ( disposing ) {
132140 this . _entryPointScript . Dispose ( ) ;
133141 }
134-
142+
135143 if ( this . _nodeProcess != null && ! this . _nodeProcess . HasExited ) {
136- this . _nodeProcess . Kill ( ) ; // TODO: Is there a more graceful way to end it? Or does this still let it perform any cleanup? System.Console.WriteLine("Killed");
144+ this . _nodeProcess . Kill ( ) ; // TODO: Is there a more graceful way to end it? Or does this still let it perform any cleanup?
145+ System . Console . WriteLine ( "Killed" ) ;
137146 }
138-
147+
139148 disposed = true ;
140149 }
141150 }
0 commit comments