mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-23 01:58:29 +00:00
Workaround for a bug in .NET Core. This issue is referenced by #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.
This commit is contained in:
committed by
SteveSandersonMS
parent
341cd4f1cb
commit
50ee405656
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
@@ -19,6 +18,8 @@ namespace Microsoft.AspNetCore.NodeServices
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
|
||||
private HttpClient _client;
|
||||
private bool _disposed;
|
||||
private int _portNumber;
|
||||
|
||||
public HttpNodeInstance(string projectPath, int port = 0, string[] watchFileExtensions = null)
|
||||
@@ -29,6 +30,7 @@ namespace Microsoft.AspNetCore.NodeServices
|
||||
projectPath,
|
||||
MakeCommandLineOptions(port, watchFileExtensions))
|
||||
{
|
||||
_client = new HttpClient();
|
||||
}
|
||||
|
||||
private static string MakeCommandLineOptions(int port, string[] watchFileExtensions)
|
||||
@@ -46,12 +48,10 @@ namespace Microsoft.AspNetCore.NodeServices
|
||||
{
|
||||
await 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:" + _portNumber, payload);
|
||||
var response = await _client.PostAsync("http://localhost:" + _portNumber, payload);
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
@@ -74,7 +74,6 @@ namespace Microsoft.AspNetCore.NodeServices
|
||||
|
||||
return (T)(object)responseString;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnOutputDataReceived(string outputData)
|
||||
{
|
||||
@@ -94,5 +93,19 @@ namespace Microsoft.AspNetCore.NodeServices
|
||||
// Prepare to receive a new port number
|
||||
_portNumber = 0;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing) {
|
||||
base.Dispose(disposing);
|
||||
|
||||
if (!_disposed)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
_client.Dispose();
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user