Skip to content

Commit 54aad64

Browse files
Decode Node's JSON response into arbitrary .NET type. Add VS stuff.
1 parent 52953c5 commit 54aad64

File tree

19 files changed

+213
-26
lines changed

19 files changed

+213
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.vs
22
*.xproj.user
33
project.lock.json
4+
npm-debug.log

Microsoft.AspNet.NodeServices.Angular/AngularRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static AngularRenderer() {
1313
}
1414

1515
public static async Task<string> RenderToString(INodeServices nodeServices, string componentModuleName, string componentExportName, string componentTagName, string requestUrl) {
16-
return await nodeServices.InvokeExport(nodeScript.FileName, "renderToString", new {
16+
return await nodeServices.InvokeExport<string>(nodeScript.FileName, "renderToString", new {
1717
moduleName = componentModuleName,
1818
exportName = componentExportName,
1919
tagName = componentTagName,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
8+
<PropertyGroup Label="Globals">
9+
<ProjectGuid>421807e6-b62c-417b-b901-46c5dedaa8f1</ProjectGuid>
10+
<RootNamespace>Microsoft.AspNet.NodeServices.Angular</RootNamespace>
11+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
13+
</PropertyGroup>
14+
15+
<PropertyGroup>
16+
<SchemaVersion>2.0</SchemaVersion>
17+
</PropertyGroup>
18+
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
19+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
8+
<PropertyGroup Label="Globals">
9+
<ProjectGuid>b04381de-991f-4831-a0b5-fe1bd3ef80c4</ProjectGuid>
10+
<RootNamespace>Microsoft.AspNet.NodeServices.React</RootNamespace>
11+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
13+
</PropertyGroup>
14+
15+
<PropertyGroup>
16+
<SchemaVersion>2.0</SchemaVersion>
17+
</PropertyGroup>
18+
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
19+
</Project>

Microsoft.AspNet.NodeServices.React/ReactRenderer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static ReactRenderer() {
1313
}
1414

1515
public static async Task<string> RenderToString(INodeServices nodeServices, string componentModuleName, string componentExportName, string requestUrl) {
16-
return await nodeServices.InvokeExport(nodeScript.FileName, "renderToString", new {
16+
return await nodeServices.InvokeExport<string>(nodeScript.FileName, "renderToString", new {
1717
moduleName = componentModuleName,
1818
exportName = componentExportName,
1919
requestUrl = requestUrl

Microsoft.AspNet.NodeServices/Content/Node/entrypoint-http.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ var server = http.createServer(function(req, res) {
1414
if (!func) {
1515
throw new Error('The module "' + resolvedPath + '" has no export named "' + bodyJson.exportedFunctionName + '"');
1616
}
17-
17+
1818
var hasSentResult = false;
1919
var callback = function(errorValue, successValue) {
2020
if (!hasSentResult) {
2121
hasSentResult = true;
2222
if (errorValue) {
2323
res.status(500).send(errorValue);
24-
} else if (typeof successValue === 'object') {
25-
// Arbitrary object - JSON-serialize it
24+
} else if (typeof successValue !== 'string') {
25+
// Arbitrary object/number/etc - JSON-serialize it
2626
res.setHeader('Content-Type', 'application/json');
2727
res.end(JSON.stringify(successValue));
2828
} else {

Microsoft.AspNet.NodeServices/Content/Node/entrypoint-stream.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function invocationCallback(errorValue, successValue) {
66
if (errorValue) {
77
throw new Error('InputOutputStreamHost doesn\'t support errors. Got error: ' + errorValue.toString());
88
} else {
9-
var serializedResult = typeof successValue === 'object' ? JSON.stringify(successValue) : successValue;
9+
var serializedResult = JSON.stringify(successValue);
1010
console.log(serializedResult);
1111
}
1212
}

Microsoft.AspNet.NodeServices/HostingModels/HttpNodeInstance.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public HttpNodeInstance(string projectPath, int port = 0)
2020
{
2121
}
2222

23-
public override async Task<string> Invoke(NodeInvocationInfo invocationInfo) {
23+
public override async Task<T> Invoke<T>(NodeInvocationInfo invocationInfo) {
2424
await this.EnsureReady();
2525

2626
using (var client = new HttpClient()) {
@@ -29,7 +29,14 @@ public override async Task<string> Invoke(NodeInvocationInfo invocationInfo) {
2929
var payload = new StringContent(payloadJson, Encoding.UTF8, "application/json");
3030
var response = await client.PostAsync("http://localhost:" + this._portNumber, payload);
3131
var responseString = await response.Content.ReadAsStringAsync();
32-
return responseString;
32+
var responseIsJson = response.Content.Headers.ContentType.MediaType == "application/json";
33+
if (responseIsJson) {
34+
return JsonConvert.DeserializeObject<T>(responseString);
35+
} else if (typeof(T) != typeof(string)) {
36+
throw new System.ArgumentException("Node module responded with non-JSON string. This cannot be converted to the requested generic type: " + typeof(T).FullName);
37+
} else {
38+
return (T)(object)responseString;
39+
}
3340
}
3441
}
3542

Microsoft.AspNet.NodeServices/HostingModels/InputOutputStreamNodeInstance.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public InputOutputStreamNodeInstance(string projectPath)
2929
{
3030
}
3131

32-
public override async Task<string> Invoke(NodeInvocationInfo invocationInfo) {
32+
public override async Task<T> Invoke<T>(NodeInvocationInfo invocationInfo) {
3333
await this._invocationSemaphore.WaitAsync();
3434
try {
3535
await this.EnsureReady();
@@ -39,7 +39,8 @@ public override async Task<string> Invoke(NodeInvocationInfo invocationInfo) {
3939
this._currentInvocationResult = new TaskCompletionSource<string>();
4040
nodeProcess.StandardInput.Write("\ninvoke:");
4141
nodeProcess.StandardInput.WriteLine(payloadJson); // WriteLineAsync isn't supported cross-platform
42-
return await this._currentInvocationResult.Task;
42+
var resultString = await this._currentInvocationResult.Task;
43+
return JsonConvert.DeserializeObject<T>(resultString);
4344
} finally {
4445
this._invocationSemaphore.Release();
4546
this._currentInvocationResult = null;

Microsoft.AspNet.NodeServices/HostingModels/OutOfProcessNodeInstance.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public OutOfProcessNodeInstance(string entryPointScript, string projectPath, str
3333
this._commandLineArguments = commandLineArguments ?? string.Empty;
3434
}
3535

36-
public abstract Task<string> Invoke(NodeInvocationInfo invocationInfo);
36+
public abstract Task<T> Invoke<T>(NodeInvocationInfo invocationInfo);
3737

38-
public Task<string> Invoke(string moduleName, params object[] args) {
39-
return this.InvokeExport(moduleName, null, args);
38+
public Task<T> Invoke<T>(string moduleName, params object[] args) {
39+
return this.InvokeExport<T>(moduleName, null, args);
4040
}
4141

42-
public async Task<string> InvokeExport(string moduleName, string exportedFunctionName, params object[] args) {
43-
return await this.Invoke(new NodeInvocationInfo {
42+
public async Task<T> InvokeExport<T>(string moduleName, string exportedFunctionName, params object[] args) {
43+
return await this.Invoke<T>(new NodeInvocationInfo {
4444
ModuleName = moduleName,
4545
ExportedFunctionName = exportedFunctionName,
4646
Args = args

0 commit comments

Comments
 (0)