diff --git a/src/Microsoft.AspNetCore.NodeServices/Configuration/NodeServicesOptions.cs b/src/Microsoft.AspNetCore.NodeServices/Configuration/NodeServicesOptions.cs index fd8b3642..dcbfce59 100644 --- a/src/Microsoft.AspNetCore.NodeServices/Configuration/NodeServicesOptions.cs +++ b/src/Microsoft.AspNetCore.NodeServices/Configuration/NodeServicesOptions.cs @@ -14,7 +14,7 @@ public NodeServicesOptions() HostingModel = DefaultNodeHostingModel; WatchFileExtensions = (string[])DefaultWatchFileExtensions.Clone(); } - + public Action OnBeforeStartExternalProcess { get; set; } public NodeHostingModel HostingModel { get; set; } public Func NodeInstanceFactory { get; set; } public string ProjectPath { get; set; } diff --git a/src/Microsoft.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs b/src/Microsoft.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs index 1b59e873..749118f6 100644 --- a/src/Microsoft.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs +++ b/src/Microsoft.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs @@ -28,21 +28,22 @@ internal class HttpNodeInstance : OutOfProcessNodeInstance ContractResolver = new CamelCasePropertyNamesContractResolver() }; - private readonly HttpClient _client; + private readonly HttpClient _client; private bool _disposed; private int _portNumber; - public HttpNodeInstance(string projectPath, string[] watchFileExtensions, int port = 0) + public HttpNodeInstance(string projectPath, string[] watchFileExtensions, int port = 0, Action onBeforeStartExternalProcess = null) : base( EmbeddedResourceReader.Read( typeof(HttpNodeInstance), "/Content/Node/entrypoint-http.js"), projectPath, watchFileExtensions, - MakeCommandLineOptions(port)) + MakeCommandLineOptions(port), + onBeforeStartExternalProcess) { _client = new HttpClient(); - } + } private static string MakeCommandLineOptions(int port) { @@ -112,18 +113,19 @@ protected override void OnOutputDataReceived(string outputData) } } - protected override void Dispose(bool disposing) { - base.Dispose(disposing); + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); - if (!_disposed) + if (!_disposed) { - if (disposing) + if (disposing) { - _client.Dispose(); - } + _client.Dispose(); + } - _disposed = true; - } - } - } + _disposed = true; + } + } + } } \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs b/src/Microsoft.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs index c45c1004..3690a47b 100644 --- a/src/Microsoft.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs +++ b/src/Microsoft.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs @@ -31,10 +31,11 @@ public OutOfProcessNodeInstance( string entryPointScript, string projectPath, string[] watchFileExtensions, - string commandLineArguments) + string commandLineArguments, + Action onBeforeStartExternalProcess = null) { _entryPointScript = new StringAsTempFile(entryPointScript); - _nodeProcess = LaunchNodeProcess(_entryPointScript.FileName, projectPath, commandLineArguments); + _nodeProcess = LaunchNodeProcess(_entryPointScript.FileName, projectPath, commandLineArguments, onBeforeStartExternalProcess); _watchFileExtensions = watchFileExtensions; _fileSystemWatcher = BeginFileWatcher(projectPath); ConnectToInputOutputStreams(); @@ -111,7 +112,7 @@ private void EnsureFileSystemWatcherIsDisposed() } } - private static Process LaunchNodeProcess(string entryPointFilename, string projectPath, string commandLineArguments) + private static Process LaunchNodeProcess(string entryPointFilename, string projectPath, string commandLineArguments, Action onBeforeStartExternalProcess) { var startInfo = new ProcessStartInfo("node") { @@ -136,6 +137,10 @@ private static Process LaunchNodeProcess(string entryPointFilename, string proje #else startInfo.Environment["NODE_PATH"] = nodePathValue; #endif + if (onBeforeStartExternalProcess != null) + { + onBeforeStartExternalProcess(startInfo); + } var process = Process.Start(startInfo); diff --git a/src/Microsoft.AspNetCore.NodeServices/HostingModels/SocketNodeInstance.cs b/src/Microsoft.AspNetCore.NodeServices/HostingModels/SocketNodeInstance.cs index 3fb25f06..bb2611e4 100644 --- a/src/Microsoft.AspNetCore.NodeServices/HostingModels/SocketNodeInstance.cs +++ b/src/Microsoft.AspNetCore.NodeServices/HostingModels/SocketNodeInstance.cs @@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels /// internal class SocketNodeInstance : OutOfProcessNodeInstance { - private readonly static JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings + private readonly static JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; @@ -36,16 +36,17 @@ internal class SocketNodeInstance : OutOfProcessNodeInstance private string _socketAddress; private VirtualConnectionClient _virtualConnectionClient; - public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress): base( + public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress, Action onBeforeStartExternalProcess = null) : base( EmbeddedResourceReader.Read( typeof(SocketNodeInstance), "/Content/Node/entrypoint-socket.js"), projectPath, watchFileExtensions, - MakeNewCommandLineOptions(socketAddress)) + MakeNewCommandLineOptions(socketAddress), + onBeforeStartExternalProcess) { _socketAddress = socketAddress; - } + } protected override async Task InvokeExportAsync(NodeInvocationInfo invocationInfo) { @@ -170,7 +171,7 @@ private static async Task ReadJsonAsync(Stream stream) private static async Task ReadAllBytesAsync(Stream input) { - byte[] buffer = new byte[16*1024]; + byte[] buffer = new byte[16 * 1024]; using (var ms = new MemoryStream()) {