|
1 | 1 | using System; |
| 2 | +using System.IO; |
2 | 3 | using System.Net.Http; |
3 | 4 | using System.Text; |
4 | 5 | using System.Text.RegularExpressions; |
@@ -52,27 +53,46 @@ public override async Task<T> Invoke<T>(NodeInvocationInfo invocationInfo) |
52 | 53 | var payloadJson = JsonConvert.SerializeObject(invocationInfo, JsonSerializerSettings); |
53 | 54 | var payload = new StringContent(payloadJson, Encoding.UTF8, "application/json"); |
54 | 55 | var response = await _client.PostAsync("http://localhost:" + _portNumber, payload); |
55 | | - var responseString = await response.Content.ReadAsStringAsync(); |
56 | 56 |
|
57 | 57 | if (!response.IsSuccessStatusCode) |
58 | 58 | { |
59 | | - throw new Exception("Call to Node module failed with error: " + responseString); |
| 59 | + var responseErrorString = await response.Content.ReadAsStringAsync(); |
| 60 | + throw new Exception("Call to Node module failed with error: " + responseErrorString); |
60 | 61 | } |
61 | 62 |
|
62 | | - var responseIsJson = response.Content.Headers.ContentType.MediaType == "application/json"; |
63 | | - if (responseIsJson) |
| 63 | + var responseContentType = response.Content.Headers.ContentType; |
| 64 | + switch (responseContentType.MediaType) |
64 | 65 | { |
65 | | - return JsonConvert.DeserializeObject<T>(responseString); |
| 66 | + case "text/plain": |
| 67 | + // String responses can skip JSON encoding/decoding |
| 68 | + if (typeof(T) != typeof(string)) |
| 69 | + { |
| 70 | + throw new ArgumentException( |
| 71 | + "Node module responded with non-JSON string. This cannot be converted to the requested generic type: " + |
| 72 | + typeof(T).FullName); |
| 73 | + } |
| 74 | + |
| 75 | + var responseString = await response.Content.ReadAsStringAsync(); |
| 76 | + return (T)(object)responseString; |
| 77 | + |
| 78 | + case "application/json": |
| 79 | + var responseJson = await response.Content.ReadAsStringAsync(); |
| 80 | + return JsonConvert.DeserializeObject<T>(responseJson); |
| 81 | + |
| 82 | + case "application/octet-stream": |
| 83 | + // Streamed responses have to be received as System.IO.Stream instances |
| 84 | + if (typeof(T) != typeof(Stream)) |
| 85 | + { |
| 86 | + throw new ArgumentException( |
| 87 | + "Node module responded with binary stream. This cannot be converted to the requested generic type: " + |
| 88 | + typeof(T).FullName + ". Instead you must use the generic type System.IO.Stream."); |
| 89 | + } |
| 90 | + |
| 91 | + return (T)(object)(await response.Content.ReadAsStreamAsync()); |
| 92 | + |
| 93 | + default: |
| 94 | + throw new InvalidOperationException("Unexpected response content type: " + responseContentType.MediaType); |
66 | 95 | } |
67 | | - |
68 | | - if (typeof(T) != typeof(string)) |
69 | | - { |
70 | | - throw new ArgumentException( |
71 | | - "Node module responded with non-JSON string. This cannot be converted to the requested generic type: " + |
72 | | - typeof(T).FullName); |
73 | | - } |
74 | | - |
75 | | - return (T)(object)responseString; |
76 | 96 | } |
77 | 97 |
|
78 | 98 | protected override void OnOutputDataReceived(string outputData) |
|
0 commit comments