Skip to content
This repository was archived by the owner on Jun 20, 2023. It is now read-only.

Commit fe3686a

Browse files
committed
Switch to WebClient sync api
We were using `HttpClient` and calling `.Result` on one of the async methods that uses it which can result in a deadlock and might have been causing random locking in the VS IDE. This switches to using `HttpWebRequest` and all synchronous API's to avoid this possible deadlock. We also control the request timeout and set it to a lower 10 seconds as it's not a critical request we don't want to block the IDE too long.
1 parent bfe34fc commit fe3686a

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

source/buildtasks/support-annotations/NugetPackages.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5-
using System.Net.Http;
5+
using System.Net;
66
using System.Runtime.Serialization.Json;
77
using System.Runtime.Versioning;
88
using System.Text.RegularExpressions;
@@ -16,6 +16,8 @@ namespace Xamarin.Android.Support.BuildTasks
1616
{
1717
public static class NugetPackages
1818
{
19+
const int WEB_REQUEST_TIMEOUT_MS = 10000;
20+
1921
public static readonly Dictionary<int, Version> AndroidApiLevelsAndVersions = new Dictionary<int, Version>
2022
{
2123
{ 23, new Version(6, 0) },
@@ -127,20 +129,18 @@ public static string GetSupportVersion(string packageIdPrefix, Dictionary<string
127129
return supportVersion;
128130
}
129131

130-
static readonly HttpClient http = new HttpClient();
131-
132-
public static async Task<string> GetRecommendedSupportPackageVersion(int apiLevel)
132+
public static string GetRecommendedSupportPackageVersion(int apiLevel)
133133
{
134134
// Default to the apilevel.x since this is displayed as a suggestion in a message
135135
var bestVersion = apiLevel.ToString() + ".x";
136136

137137
try
138138
{
139-
var searchUrl = await GetNuGetSearchUrl();
139+
var searchUrl = GetNuGetSearchUrl();
140140

141141
var queryUrl = searchUrl + "?q=packageid:Xamarin.Android.Support.Annotations&prerelease=false";
142142

143-
var data = await http.GetStringAsync(queryUrl);
143+
var data = DownloadString(queryUrl);
144144

145145
var js = new JavaScriptSerializer();
146146
var json = js.Deserialize<dynamic>(data);
@@ -161,9 +161,9 @@ public static async Task<string> GetRecommendedSupportPackageVersion(int apiLeve
161161
return bestVersion;
162162
}
163163

164-
static async Task<string> GetNuGetSearchUrl()
164+
static string GetNuGetSearchUrl()
165165
{
166-
var data = await http.GetStringAsync("https://api.nuget.org/v3/index.json");
166+
var data = DownloadString("https://api.nuget.org/v3/index.json");
167167

168168
var js = new JavaScriptSerializer();
169169
var json = js.Deserialize<dynamic>(data);
@@ -175,5 +175,22 @@ static async Task<string> GetNuGetSearchUrl()
175175

176176
return null;
177177
}
178+
179+
static string DownloadString(string url)
180+
{
181+
var result = string.Empty;
182+
183+
var request = (HttpWebRequest)WebRequest.Create(url);
184+
request.Timeout = WEB_REQUEST_TIMEOUT_MS;
185+
request.ReadWriteTimeout = WEB_REQUEST_TIMEOUT_MS;
186+
187+
using (var response = (HttpWebResponse)request.GetResponse())
188+
using (var respStream = response.GetResponseStream())
189+
using (var sr = new StreamReader(respStream)) {
190+
result = sr.ReadToEnd();
191+
}
192+
193+
return result;
194+
}
178195
}
179196
}

source/buildtasks/support-annotations/Support-Annotations-BuildTasks.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@
4040
<Reference Include="System.Runtime.Serialization" />
4141
<Reference Include="System.Web.Extensions" />
4242
<Reference Include="Microsoft.CSharp" />
43-
<Reference Include="System.Net.Http">
44-
<HintPath>..\..\..\..\..\..\Library\Frameworks\Mono.framework\Versions\5.16.0\lib\mono\xbuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.Net.Http.dll</HintPath>
45-
</Reference>
4643
</ItemGroup>
4744
<ItemGroup>
4845
<Compile Include="Properties\AssemblyInfo.cs" />

source/buildtasks/support-annotations/VerifyVersionsTask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public override bool Execute()
8383
sb.AppendLine("The following Xamarin.Android.Support.* packages and versions were detected:");
8484
sb.AppendLine();
8585

86-
var recommendedSupportVersion = NugetPackages.GetRecommendedSupportPackageVersion(apiLevel).Result;
86+
var recommendedSupportVersion = NugetPackages.GetRecommendedSupportPackageVersion(apiLevel);
8787

8888
foreach (var pkg in packageVersions)
8989
sb.AppendLine($" {pkg.Key} ({pkg.Value})");

0 commit comments

Comments
 (0)