Exemplo n.º 1
0
        public override async Task <T> Invoke <T>(NodeInvocationInfo invocationInfo)
        {
            await this.EnsureReady();

            using (var client = new HttpClient()) {
                // TODO: Use System.Net.Http.Formatting (PostAsJsonAsync etc.)
                var payloadJson = JsonConvert.SerializeObject(invocationInfo, jsonSerializerSettings);
                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();

                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception("Call to Node module failed with error: " + 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);
                }
            }
        }
Exemplo n.º 2
0
 public override async Task<string> Invoke(NodeInvocationInfo invocationInfo) {
     await this.EnsureReady();
     
     using (var client = new HttpClient()) {
         // TODO: Use System.Net.Http.Formatting (PostAsJsonAsync etc.)
         var payloadJson = JsonConvert.SerializeObject(invocationInfo, jsonSerializerSettings);
         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;
     }
 }
 public override async Task<string> Invoke(NodeInvocationInfo invocationInfo) {
     await this._invocationSemaphore.WaitAsync();
     try {
         await this.EnsureReady();
         
         var payloadJson = JsonConvert.SerializeObject(invocationInfo, jsonSerializerSettings);
         var nodeProcess = this.NodeProcess;
         this._currentInvocationResult = new TaskCompletionSource<string>();
         nodeProcess.StandardInput.Write("\ninvoke:");
         nodeProcess.StandardInput.WriteLine(payloadJson); // WriteLineAsync isn't supported cross-platform
         return await this._currentInvocationResult.Task;
     } finally {
         this._invocationSemaphore.Release();
         this._currentInvocationResult = null;
     }
 }
Exemplo n.º 4
0
        public override async Task <T> Invoke <T>(NodeInvocationInfo invocationInfo)
        {
            await this._invocationSemaphore.WaitAsync();

            try {
                await this.EnsureReady();

                var payloadJson = JsonConvert.SerializeObject(invocationInfo, jsonSerializerSettings);
                var nodeProcess = this.NodeProcess;
                this._currentInvocationResult = new TaskCompletionSource <string>();
                nodeProcess.StandardInput.Write("\ninvoke:");
                nodeProcess.StandardInput.WriteLine(payloadJson); // WriteLineAsync isn't supported cross-platform
                var resultString = await this._currentInvocationResult.Task;
                return(JsonConvert.DeserializeObject <T>(resultString));
            } finally {
                this._invocationSemaphore.Release();
                this._currentInvocationResult = null;
            }
        }
Exemplo n.º 5
0
 public abstract Task <T> Invoke <T>(NodeInvocationInfo invocationInfo);
 public abstract Task<string> Invoke(NodeInvocationInfo invocationInfo);