Skip to content

Commit de991b9

Browse files
Switch to using DI to acquire Node instances. Bump versions to alpha2.
1 parent 301657a commit de991b9

File tree

23 files changed

+103
-92
lines changed

23 files changed

+103
-92
lines changed

Microsoft.AspNet.NodeServices.Angular/AngularPrerenderTagHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,24 @@ static AngularRunAtServerTagHelper() {
2020
const string PrerenderModuleAttributeName = "aspnet-ng2-prerender-module";
2121
const string PrerenderExportAttributeName = "aspnet-ng2-prerender-export";
2222

23-
private static NodeInstance nodeInstance = new NodeInstance();
24-
2523
[HtmlAttributeName(PrerenderModuleAttributeName)]
2624
public string ModuleName { get; set; }
2725

2826
[HtmlAttributeName(PrerenderExportAttributeName)]
2927
public string ExportName { get; set; }
3028

3129
private IHttpContextAccessor contextAccessor;
30+
private INodeServices nodeServices;
3231

33-
public AngularRunAtServerTagHelper(IHttpContextAccessor contextAccessor)
32+
public AngularRunAtServerTagHelper(INodeServices nodeServices, IHttpContextAccessor contextAccessor)
3433
{
3534
this.contextAccessor = contextAccessor;
35+
this.nodeServices = nodeServices;
3636
}
3737

3838
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
3939
{
40-
var result = await nodeInstance.InvokeExport(nodeScript.FileName, "renderComponent", new {
40+
var result = await this.nodeServices.InvokeExport(nodeScript.FileName, "renderComponent", new {
4141
componentModule = this.ModuleName,
4242
componentExport = this.ExportName,
4343
tagName = output.TagName,

Microsoft.AspNet.NodeServices.Angular/project.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.0.0-alpha1",
2+
"version": "1.0.0-alpha2",
33
"description": "Microsoft.AspNet.NodeServices.Angular Class Library",
44
"authors": [
55
"Microsoft"
@@ -25,7 +25,7 @@
2525
}
2626
},
2727
"dependencies": {
28-
"Microsoft.AspNet.NodeServices": "1.0.0-alpha1",
28+
"Microsoft.AspNet.NodeServices": "1.0.0-alpha2",
2929
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-beta8"
3030
},
3131
"resource": [

Microsoft.AspNet.NodeServices.React/ReactRenderer.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ namespace Microsoft.AspNet.NodeServices.React
55
public static class ReactRenderer
66
{
77
private static StringAsTempFile nodeScript;
8-
private static NodeInstance nodeInstance = new NodeInstance();
98

109
static ReactRenderer() {
1110
// Consider populating this lazily
1211
var script = EmbeddedResourceReader.Read(typeof (ReactRenderer), "/Content/Node/react-rendering.js");
1312
nodeScript = new StringAsTempFile(script); // Will be cleaned up on process exit
1413
}
1514

16-
public static async Task<string> RenderToString(string moduleName, string exportName, string baseUrl) {
17-
return await nodeInstance.InvokeExport(nodeScript.FileName, "renderToString", new {
15+
public static async Task<string> RenderToString(INodeServices nodeServices, string moduleName, string exportName, string baseUrl) {
16+
return await nodeServices.InvokeExport(nodeScript.FileName, "renderToString", new {
1817
moduleName,
1918
exportName,
2019
baseUrl

Microsoft.AspNet.NodeServices.React/project.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.0.0-alpha1",
2+
"version": "1.0.0-alpha2",
33
"description": "Microsoft.AspNet.NodeServices.React Class Library",
44
"authors": [
55
"Microsoft"
@@ -25,7 +25,7 @@
2525
}
2626
},
2727
"dependencies": {
28-
"Microsoft.AspNet.NodeServices": "1.0.0-alpha1"
28+
"Microsoft.AspNet.NodeServices": "1.0.0-alpha2"
2929
},
3030
"resource": [
3131
"Content/**/*"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.Framework.DependencyInjection;
2+
3+
namespace Microsoft.AspNet.NodeServices {
4+
public static class Configuration {
5+
public static void AddNodeServices(this IServiceCollection serviceCollection, NodeHostingModel hostingModel = NodeHostingModel.Http) {
6+
serviceCollection.AddSingleton(typeof(INodeServices), (serviceProvider) => {
7+
return CreateNodeServices(hostingModel);
8+
});
9+
}
10+
11+
private static INodeServices CreateNodeServices(NodeHostingModel hostingModel)
12+
{
13+
switch (hostingModel)
14+
{
15+
case NodeHostingModel.Http:
16+
return new HttpNodeInstance();
17+
case NodeHostingModel.InputOutputStream:
18+
return new InputOutputStreamNodeInstance();
19+
default:
20+
throw new System.ArgumentException("Unknown hosting model: " + hostingModel.ToString());
21+
}
22+
}
23+
}
24+
}

Microsoft.AspNet.NodeServices/HostingModels/HttpNodeHost.cs renamed to Microsoft.AspNet.NodeServices/HostingModels/HttpNodeInstance.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using Newtonsoft.Json.Serialization;
77

88
namespace Microsoft.AspNet.NodeServices {
9-
internal class HttpNodeHost : OutOfProcessNodeRunner {
9+
internal class HttpNodeInstance : OutOfProcessNodeInstance {
1010
private readonly static Regex PortMessageRegex = new Regex(@"^\[Microsoft.AspNet.NodeServices.HttpNodeHost:Listening on port (\d+)\]$");
1111

1212
private readonly static JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings {
@@ -15,8 +15,8 @@ internal class HttpNodeHost : OutOfProcessNodeRunner {
1515

1616
private int _portNumber;
1717

18-
public HttpNodeHost(int port = 0)
19-
: base(EmbeddedResourceReader.Read(typeof(HttpNodeHost), "/Content/Node/entrypoint-http.js"), port.ToString())
18+
public HttpNodeInstance(int port = 0)
19+
: base(EmbeddedResourceReader.Read(typeof(HttpNodeInstance), "/Content/Node/entrypoint-http.js"), port.ToString())
2020
{
2121
}
2222

Microsoft.AspNet.NodeServices/HostingModels/InputOutputStreamNodeHost.cs renamed to Microsoft.AspNet.NodeServices/HostingModels/InputOutputStreamNodeInstance.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Microsoft.AspNet.NodeServices {
1515
// Instead of directly using stdin/stdout, we could use either regular sockets (TCP) or use named pipes
1616
// on Windows and domain sockets on Linux / OS X, but either way would need a system for framing the
1717
// requests, associating them with responses, and scheduling use of the comms channel.
18-
internal class InputOutputStreamNodeHost : OutOfProcessNodeRunner
18+
internal class InputOutputStreamNodeInstance : OutOfProcessNodeInstance
1919
{
2020
private SemaphoreSlim _invocationSemaphore = new SemaphoreSlim(1);
2121
private TaskCompletionSource<string> _currentInvocationResult;
@@ -24,8 +24,8 @@ internal class InputOutputStreamNodeHost : OutOfProcessNodeRunner
2424
ContractResolver = new CamelCasePropertyNamesContractResolver()
2525
};
2626

27-
public InputOutputStreamNodeHost()
28-
: base(EmbeddedResourceReader.Read(typeof(InputOutputStreamNodeHost), "/Content/Node/entrypoint-stream.js"))
27+
public InputOutputStreamNodeInstance()
28+
: base(EmbeddedResourceReader.Read(typeof(InputOutputStreamNodeInstance), "/Content/Node/entrypoint-stream.js"))
2929
{
3030
}
3131

Microsoft.AspNet.NodeServices/HostingModels/NodeHost.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

Microsoft.AspNet.NodeServices/HostingModels/OutOfProcessNodeRunner.cs renamed to Microsoft.AspNet.NodeServices/HostingModels/OutOfProcessNodeInstance.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Microsoft.AspNet.NodeServices {
88
* Class responsible for launching the Node child process, determining when it is ready to accept invocations,
99
* and finally killing it when the parent process exits. Also it restarts the child process if it dies.
1010
*/
11-
internal abstract class OutOfProcessNodeRunner : NodeHost {
11+
public abstract class OutOfProcessNodeInstance : INodeServices {
1212
private object _childProcessLauncherLock;
1313
private bool disposed;
1414
private StringAsTempFile _entryPointScript;
@@ -18,19 +18,33 @@ internal abstract class OutOfProcessNodeRunner : NodeHost {
1818

1919
protected Process NodeProcess {
2020
get {
21-
// This is only exposed to support the UnreliableStreamNodeHost, which is just to verify that
21+
// 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
}
2626

27-
public OutOfProcessNodeRunner(string entryPointScript, string commandLineArguments = null)
27+
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
}
3333

34+
public abstract Task<string> Invoke(NodeInvocationInfo invocationInfo);
35+
36+
public Task<string> Invoke(string moduleName, params object[] args) {
37+
return this.InvokeExport(moduleName, null, args);
38+
}
39+
40+
public async Task<string> InvokeExport(string moduleName, string exportedFunctionName, params object[] args) {
41+
return await this.Invoke(new NodeInvocationInfo {
42+
ModuleName = moduleName,
43+
ExportedFunctionName = exportedFunctionName,
44+
Args = args
45+
});
46+
}
47+
3448
protected async Task EnsureReady() {
3549
lock (this._childProcessLauncherLock) {
3650
if (this._nodeProcess == null || this._nodeProcess.HasExited) {
@@ -104,8 +118,8 @@ protected virtual void OnOutputDataReceived(string outputData) {
104118
protected virtual void OnErrorDataReceived(string errorData) {
105119
Console.WriteLine("[Node] " + errorData);
106120
}
107-
108-
public override void Dispose()
121+
122+
public void Dispose()
109123
{
110124
Dispose(true);
111125
GC.SuppressFinalize(this);
@@ -125,8 +139,8 @@ protected virtual void Dispose(bool disposing)
125139
disposed = true;
126140
}
127141
}
128-
129-
~OutOfProcessNodeRunner() {
142+
143+
~OutOfProcessNodeInstance() {
130144
Dispose (false);
131145
}
132146
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace Microsoft.AspNet.NodeServices {
5+
public interface INodeServices : IDisposable {
6+
Task<string> Invoke(string moduleName, params object[] args);
7+
8+
Task<string> InvokeExport(string moduleName, string exportedFunctionName, params object[] args);
9+
}
10+
}

0 commit comments

Comments
 (0)