Skip to content

Commit 50ee405

Browse files
pauldotknopfSteveSandersonMS
authored andcommitted
Workaround for a bug in .NET Core. This issue is referenced by aspnet#92. It is has been reported to the dotnet corefx team here: dotnet/corefx#8809 The issue won't be resolved in 1.0.0, so @stephentoub recommended that we reuse the HttpClient.
1 parent 341cd4f commit 50ee405

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed

src/Microsoft.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Net;
32
using System.Net.Http;
43
using System.Text;
54
using System.Text.RegularExpressions;
@@ -19,6 +18,8 @@ internal class HttpNodeInstance : OutOfProcessNodeInstance
1918
ContractResolver = new CamelCasePropertyNamesContractResolver()
2019
};
2120

21+
private HttpClient _client;
22+
private bool _disposed;
2223
private int _portNumber;
2324

2425
public HttpNodeInstance(string projectPath, int port = 0, string[] watchFileExtensions = null)
@@ -29,7 +30,8 @@ public HttpNodeInstance(string projectPath, int port = 0, string[] watchFileExte
2930
projectPath,
3031
MakeCommandLineOptions(port, watchFileExtensions))
3132
{
32-
}
33+
_client = new HttpClient();
34+
}
3335

3436
private static string MakeCommandLineOptions(int port, string[] watchFileExtensions)
3537
{
@@ -46,34 +48,31 @@ public override async Task<T> Invoke<T>(NodeInvocationInfo invocationInfo)
4648
{
4749
await EnsureReady();
4850

49-
using (var client = new HttpClient())
50-
{
51-
// TODO: Use System.Net.Http.Formatting (PostAsJsonAsync etc.)
52-
var payloadJson = JsonConvert.SerializeObject(invocationInfo, JsonSerializerSettings);
53-
var payload = new StringContent(payloadJson, Encoding.UTF8, "application/json");
54-
var response = await client.PostAsync("http://localhost:" + _portNumber, payload);
55-
var responseString = await response.Content.ReadAsStringAsync();
51+
// TODO: Use System.Net.Http.Formatting (PostAsJsonAsync etc.)
52+
var payloadJson = JsonConvert.SerializeObject(invocationInfo, JsonSerializerSettings);
53+
var payload = new StringContent(payloadJson, Encoding.UTF8, "application/json");
54+
var response = await _client.PostAsync("http://localhost:" + _portNumber, payload);
55+
var responseString = await response.Content.ReadAsStringAsync();
5656

57-
if (!response.IsSuccessStatusCode)
58-
{
59-
throw new Exception("Call to Node module failed with error: " + responseString);
60-
}
61-
62-
var responseIsJson = response.Content.Headers.ContentType.MediaType == "application/json";
63-
if (responseIsJson)
64-
{
65-
return JsonConvert.DeserializeObject<T>(responseString);
66-
}
57+
if (!response.IsSuccessStatusCode)
58+
{
59+
throw new Exception("Call to Node module failed with error: " + responseString);
60+
}
6761

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-
}
62+
var responseIsJson = response.Content.Headers.ContentType.MediaType == "application/json";
63+
if (responseIsJson)
64+
{
65+
return JsonConvert.DeserializeObject<T>(responseString);
66+
}
7467

75-
return (T)(object)responseString;
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);
7673
}
74+
75+
return (T)(object)responseString;
7776
}
7877

7978
protected override void OnOutputDataReceived(string outputData)
@@ -94,5 +93,19 @@ protected override void OnBeforeLaunchProcess()
9493
// Prepare to receive a new port number
9594
_portNumber = 0;
9695
}
97-
}
96+
97+
protected override void Dispose(bool disposing) {
98+
base.Dispose(disposing);
99+
100+
if (!_disposed)
101+
{
102+
if (disposing)
103+
{
104+
_client.Dispose();
105+
}
106+
107+
_disposed = true;
108+
}
109+
}
110+
}
98111
}

0 commit comments

Comments
 (0)