mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-25 11:07:29 +00:00
Decode Node's JSON response into arbitrary .NET type. Add VS stuff.
This commit is contained in:
@@ -14,15 +14,15 @@ var server = http.createServer(function(req, res) {
|
||||
if (!func) {
|
||||
throw new Error('The module "' + resolvedPath + '" has no export named "' + bodyJson.exportedFunctionName + '"');
|
||||
}
|
||||
|
||||
|
||||
var hasSentResult = false;
|
||||
var callback = function(errorValue, successValue) {
|
||||
if (!hasSentResult) {
|
||||
hasSentResult = true;
|
||||
if (errorValue) {
|
||||
res.status(500).send(errorValue);
|
||||
} else if (typeof successValue === 'object') {
|
||||
// Arbitrary object - JSON-serialize it
|
||||
} else if (typeof successValue !== 'string') {
|
||||
// Arbitrary object/number/etc - JSON-serialize it
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(JSON.stringify(successValue));
|
||||
} else {
|
||||
|
||||
@@ -6,7 +6,7 @@ function invocationCallback(errorValue, successValue) {
|
||||
if (errorValue) {
|
||||
throw new Error('InputOutputStreamHost doesn\'t support errors. Got error: ' + errorValue.toString());
|
||||
} else {
|
||||
var serializedResult = typeof successValue === 'object' ? JSON.stringify(successValue) : successValue;
|
||||
var serializedResult = JSON.stringify(successValue);
|
||||
console.log(serializedResult);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
{
|
||||
}
|
||||
|
||||
public override async Task<string> Invoke(NodeInvocationInfo invocationInfo) {
|
||||
public override async Task<T> Invoke<T>(NodeInvocationInfo invocationInfo) {
|
||||
await this.EnsureReady();
|
||||
|
||||
using (var client = new HttpClient()) {
|
||||
@@ -29,7 +29,14 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
var payload = new StringContent(payloadJson, Encoding.UTF8, "application/json");
|
||||
var response = await client.PostAsync("http://localhost:" + this._portNumber, payload);
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
return responseString;
|
||||
var responseIsJson = response.Content.Headers.ContentType.MediaType == "application/json";
|
||||
if (responseIsJson) {
|
||||
return JsonConvert.DeserializeObject<T>(responseString);
|
||||
} else if (typeof(T) != typeof(string)) {
|
||||
throw new System.ArgumentException("Node module responded with non-JSON string. This cannot be converted to the requested generic type: " + typeof(T).FullName);
|
||||
} else {
|
||||
return (T)(object)responseString;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
{
|
||||
}
|
||||
|
||||
public override async Task<string> Invoke(NodeInvocationInfo invocationInfo) {
|
||||
public override async Task<T> Invoke<T>(NodeInvocationInfo invocationInfo) {
|
||||
await this._invocationSemaphore.WaitAsync();
|
||||
try {
|
||||
await this.EnsureReady();
|
||||
@@ -39,7 +39,8 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
this._currentInvocationResult = new TaskCompletionSource<string>();
|
||||
nodeProcess.StandardInput.Write("\ninvoke:");
|
||||
nodeProcess.StandardInput.WriteLine(payloadJson); // WriteLineAsync isn't supported cross-platform
|
||||
return await this._currentInvocationResult.Task;
|
||||
var resultString = await this._currentInvocationResult.Task;
|
||||
return JsonConvert.DeserializeObject<T>(resultString);
|
||||
} finally {
|
||||
this._invocationSemaphore.Release();
|
||||
this._currentInvocationResult = null;
|
||||
|
||||
@@ -33,14 +33,14 @@ namespace Microsoft.AspNet.NodeServices {
|
||||
this._commandLineArguments = commandLineArguments ?? string.Empty;
|
||||
}
|
||||
|
||||
public abstract Task<string> Invoke(NodeInvocationInfo invocationInfo);
|
||||
public abstract Task<T> Invoke<T>(NodeInvocationInfo invocationInfo);
|
||||
|
||||
public Task<string> Invoke(string moduleName, params object[] args) {
|
||||
return this.InvokeExport(moduleName, null, args);
|
||||
public Task<T> Invoke<T>(string moduleName, params object[] args) {
|
||||
return this.InvokeExport<T>(moduleName, null, args);
|
||||
}
|
||||
|
||||
public async Task<string> InvokeExport(string moduleName, string exportedFunctionName, params object[] args) {
|
||||
return await this.Invoke(new NodeInvocationInfo {
|
||||
public async Task<T> InvokeExport<T>(string moduleName, string exportedFunctionName, params object[] args) {
|
||||
return await this.Invoke<T>(new NodeInvocationInfo {
|
||||
ModuleName = moduleName,
|
||||
ExportedFunctionName = exportedFunctionName,
|
||||
Args = args
|
||||
|
||||
@@ -3,8 +3,8 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.AspNet.NodeServices {
|
||||
public interface INodeServices : IDisposable {
|
||||
Task<string> Invoke(string moduleName, params object[] args);
|
||||
Task<T> Invoke<T>(string moduleName, params object[] args);
|
||||
|
||||
Task<string> InvokeExport(string moduleName, string exportedFunctionName, params object[] args);
|
||||
Task<T> InvokeExport<T>(string moduleName, string exportedFunctionName, params object[] args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>b0fa4175-8b29-4904-9780-28b3c24b0567</ProjectGuid>
|
||||
<RootNamespace>Microsoft.AspNet.NodeServices</RootNamespace>
|
||||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\NodeServices.sln\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
|
||||
<OutputPath Condition="'$(OutputPath)'=='' ">..\NodeServices.sln\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
||||
Reference in New Issue
Block a user