From 3fe8222318fa959cef295feec378ec20dad5dbc9 Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Mon, 26 May 2014 16:32:52 +0200 Subject: [PATCH] JsonRPC4NET: close HttpClient once we have finished using it and not before!!!! --- jsonrpc4net/jsonrpc4net/JsonRpcHttpAsyncClient.cs | 67 ++++++++--------------- 1 file changed, 23 insertions(+), 44 deletions(-) diff --git a/jsonrpc4net/jsonrpc4net/JsonRpcHttpAsyncClient.cs b/jsonrpc4net/jsonrpc4net/JsonRpcHttpAsyncClient.cs index 9802343..8864290 100644 --- a/jsonrpc4net/jsonrpc4net/JsonRpcHttpAsyncClient.cs +++ b/jsonrpc4net/jsonrpc4net/JsonRpcHttpAsyncClient.cs @@ -110,24 +110,38 @@ namespace GumartinM.JsonRPC4NET // see: http://stackoverflow.com/questions/5895879/when-do-we-need-to-call-dispose-in-dot-net-c //TODO: Am I really sure I have to call the Dispose method of HttpContent content? In this case, shouldn't it be stupid? // For HttpResponseMessage response I am sure I have to do it but I am not for HttpContent content. - using (HttpContent content = new StringContent(jsonData, System.Text.Encoding.UTF8, "application/json-rpc")) - using (HttpResponseMessage response = await this.PostAsync(uri, content, cancellation)) + using (HttpClient client = new HttpClient { Timeout = TimeSpan.FromSeconds(5) }) + using (HttpContent contentPOST = new StringContent(jsonData, System.Text.Encoding.UTF8, "application/json-rpc")) + using (HttpResponseMessage response = await client.PostAsync(uri, contentPOST, cancellation)) + using (HttpContent contentRESULT = response.Content) { - if (response.StatusCode == HttpStatusCode.OK) { - //byte[] jsonBytes = await response.Content.ReadAsByteArrayAsync(); - Stream stream = await response.Content.ReadAsStreamAsync(); - - //return this.ReadResponse(jsonBytes); - return await this.ReadResponseAsync(stream); - + return await this.ReadResponseAsync(contentRESULT); } throw new Exception("Unexpected response code: " + response.StatusCode); } } + async private Task> ReadResponseAsync(HttpContent content) + { + // Option a) with bytes + //byte[] jsonBytes = await contentRESULT.ReadAsByteArrayAsync(); + //return this.ReadResponse(jsonBytes); + + // Option b) with stream + using (Stream stream = await content.ReadAsStreamAsync ()) + using (StreamReader streamReader = new StreamReader (stream, System.Text.Encoding.UTF8)) + { + // This line makes this method useless (IMHO it is the same as the one working with bytes) + // How could I work with JSON saving memory? + string json = await streamReader.ReadToEndAsync(); + + return this.ReadResponse(json); + } + } + /// /// Reads the response. /// @@ -141,24 +155,6 @@ namespace GumartinM.JsonRPC4NET return this.ReadResponse(json); } - /// - /// Reads the response. - /// - /// The response. - /// Stream. - /// The 1st type parameter. - async private Task> ReadResponseAsync(Stream stream) - { - using (StreamReader streamReader = new StreamReader (stream, System.Text.Encoding.UTF8)) - { - // This line makes this method useless (IMHO it is the same as the one working with bytes) - // How could I work with JSON saving memory? - string json = await streamReader.ReadToEndAsync(); - - return this.ReadResponse(json); - } - } - private POSTResult ReadResponse(string json) { JObject jsonObjects = JObject.Parse(json); @@ -178,23 +174,6 @@ namespace GumartinM.JsonRPC4NET throw new JsonRpcClientException(0, "There is not error nor result in JSON response data.", jsonObjects); } - /// - /// Send a POST request to the specified Uri as an asynchronous operation. - /// - /// The Uri the request is sent to. - /// The HTTP request content sent to the server. - /// Cancellation token. - /// When some error. - /// System.Threading.Tasks.Task]]>.The task object representing the asynchronous operation. - async private Task PostAsync(string uri, HttpContent content, CancellationToken cancellation) - { - using (HttpClient client = new HttpClient { Timeout = TimeSpan.FromSeconds(5) }) - { - // TODO: cancellation - return await client.PostAsync(uri, content, cancellation); - } - } - private class POST { public string id { get; set; } -- 2.1.4