mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-25 02:57:31 +00:00
Decode Node's JSON response into arbitrary .NET type. Add VS stuff.
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user