Esempio 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);
                }
            }
        }
Esempio n. 2
0
        public override async Task <T> Invoke <T>(NodeInvocationInfo invocationInfo)
        {
            await EnsureReady();

            // 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:" + _portNumber, payload);

            if (!response.IsSuccessStatusCode)
            {
                var responseErrorString = await response.Content.ReadAsStringAsync();

                throw new Exception("Call to Node module failed with error: " + responseErrorString);
            }

            var responseContentType = response.Content.Headers.ContentType;

            switch (responseContentType.MediaType)
            {
            case "text/plain":
                // String responses can skip JSON encoding/decoding
                if (typeof(T) != typeof(string))
                {
                    throw new ArgumentException(
                              "Node module responded with non-JSON string. This cannot be converted to the requested generic type: " +
                              typeof(T).FullName);
                }

                var responseString = await response.Content.ReadAsStringAsync();

                return((T)(object)responseString);

            case "application/json":
                var responseJson = await response.Content.ReadAsStringAsync();

                return(JsonConvert.DeserializeObject <T>(responseJson));

            case "application/octet-stream":
                // Streamed responses have to be received as System.IO.Stream instances
                if (typeof(T) != typeof(Stream))
                {
                    throw new ArgumentException(
                              "Node module responded with binary stream. This cannot be converted to the requested generic type: " +
                              typeof(T).FullName + ". Instead you must use the generic type System.IO.Stream.");
                }

                return((T)(object)(await response.Content.ReadAsStreamAsync()));

            default:
                throw new InvalidOperationException("Unexpected response content type: " + responseContentType.MediaType);
            }
        }
        public override async Task <T> Invoke <T>(NodeInvocationInfo invocationInfo)
        {
            await EnsureReady();

            var virtualConnectionClient = await GetOrCreateVirtualConnectionClientAsync();

            bool   shouldDisposeVirtualConnection = true;
            Stream virtualConnection = null;

            try
            {
                virtualConnection = _currentVirtualConnectionClient.OpenVirtualConnection();

                // Send request
                await WriteJsonLineAsync(virtualConnection, invocationInfo);

                // Determine what kind of response format is expected
                if (typeof(T) == typeof(Stream))
                {
                    // Pass through streamed binary response
                    // It is up to the consumer to dispose this stream, so don't do so here
                    shouldDisposeVirtualConnection = false;
                    return((T)(object)virtualConnection);
                }
                else
                {
                    // Parse and return non-streamed JSON response
                    var response = await ReadJsonAsync <RpcJsonResponse <T> >(virtualConnection);

                    if (response.ErrorMessage != null)
                    {
                        throw new NodeInvocationException(response.ErrorMessage, response.ErrorDetails);
                    }

                    return(response.Result);
                }
            }
            finally
            {
                if (shouldDisposeVirtualConnection)
                {
                    virtualConnection.Dispose();
                }
            }
        }
        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;
            }
        }
Esempio n. 5
0
        public override async Task <T> Invoke <T>(NodeInvocationInfo invocationInfo)
        {
            await EnsureReady();

            var virtualConnectionClient = await GetOrCreateVirtualConnectionClientAsync();

            using (var virtualConnection = _currentVirtualConnectionClient.OpenVirtualConnection())
            {
                // Send request
                await WriteJsonLineAsync(virtualConnection, invocationInfo);

                // Receive response
                var response = await ReadJsonAsync <RpcResponse <T> >(virtualConnection);

                if (response.ErrorMessage != null)
                {
                    throw new NodeInvocationException(response.ErrorMessage, response.ErrorDetails);
                }

                return(response.Result);
            }
        }
 public abstract Task <T> Invoke <T>(NodeInvocationInfo invocationInfo);