From 9952f2fd687c53e1f8efe9e9553d93d688af3176 Mon Sep 17 00:00:00 2001 From: Livar Cunha Date: Wed, 17 May 2017 21:46:21 -0700 Subject: [PATCH] Addressing code review comments by using Streams instead of ing the contents into a string. --- .../RuntimeConfig.cs | 20 +++++++++++-------- .../dotnet-add-package/AddPackageParser.cs | 17 ++++++++-------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.DotNet.Cli.Utils/RuntimeConfig.cs b/src/Microsoft.DotNet.Cli.Utils/RuntimeConfig.cs index 751f2548e..61691e4a8 100644 --- a/src/Microsoft.DotNet.Cli.Utils/RuntimeConfig.cs +++ b/src/Microsoft.DotNet.Cli.Utils/RuntimeConfig.cs @@ -13,12 +13,16 @@ namespace Microsoft.DotNet.Cli.Utils public RuntimeConfigFramework Framework { get; } public RuntimeConfig(string runtimeConfigPath) - { - var runtimeConfigJson = OpenRuntimeConfig(runtimeConfigPath); - - Framework = ParseFramework(runtimeConfigJson); - - IsPortable = Framework != null; + { + JObject runtimeConfigJson; + using (var streamReader = new StreamReader(new FileStream(runtimeConfigPath, FileMode.Open))) + { + runtimeConfigJson = OpenRuntimeConfig(streamReader); + } + + Framework = ParseFramework(runtimeConfigJson); + + IsPortable = Framework != null; } public static bool IsApplicationPortable(string entryAssemblyPath) @@ -32,9 +36,9 @@ namespace Microsoft.DotNet.Cli.Utils return false; } - private JObject OpenRuntimeConfig(string runtimeConfigPath) + private JObject OpenRuntimeConfig(StreamReader streamReader) { - var reader = new JsonTextReader(new StringReader(File.ReadAllText(runtimeConfigPath))); + var reader = new JsonTextReader(streamReader); return JObject.Load(reader); } diff --git a/src/dotnet/commands/dotnet-add/dotnet-add-package/AddPackageParser.cs b/src/dotnet/commands/dotnet-add/dotnet-add-package/AddPackageParser.cs index e0b4a8e90..5ee3fd10c 100644 --- a/src/dotnet/commands/dotnet-add/dotnet-add-package/AddPackageParser.cs +++ b/src/dotnet/commands/dotnet-add/dotnet-add-package/AddPackageParser.cs @@ -54,7 +54,7 @@ namespace Microsoft.DotNet.Cli { var httpClient = new HttpClient(); - string result; + Stream result; try { @@ -62,21 +62,22 @@ namespace Microsoft.DotNet.Cli var response = httpClient.GetAsync($"https://api-v2v3search-0.nuget.org/query?q={match}&skip=0&take=100&prerelease=true", cancellation.Token) .Result; - result = response.Content.ReadAsStringAsync().Result; + result = response.Content.ReadAsStreamAsync().Result; } catch (Exception) { yield break; } - using (var reader = new JsonTextReader(new StringReader(result))) + JObject json; + using (var reader = new JsonTextReader(new StreamReader(result))) { - var json = JObject.Load(reader); + json = JObject.Load(reader); + } - foreach (var id in json["data"]) - { - yield return id["id"].Value(); - } + foreach (var id in json["data"]) + { + yield return id["id"].Value(); } } }