Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 56d190a

Browse files
author
Patrick Assuied
committed
Fix Search query for GetParameterNamesAsync() in NuGet v2
- Changed WebRequestFactory to use static HttpClient to avoid connection socket exhaustion
1 parent 0be0dd7 commit 56d190a

File tree

4 files changed

+7
-8
lines changed

4 files changed

+7
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ on the official Visual Studio extension gallery.
1414

1515
## 1.4
1616
- [x] Implement NuGet V2 Feed support
17+
- [x] Change WebRequestFactory to use a static HttpClient to avoid connection socket exhaustion problem
1718

1819
## 1.1
1920

src/ProjectFileTools.NuGetSearch/Feeds/Web/NuGetV2ServiceFeed.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public async Task<IPackageNameSearchResult> GetPackageNamesAsync(string prefix,
8484
IReadOnlyList<string> results = new List<string>();
8585
string frameworkQuery = !string.IsNullOrEmpty(queryConfiguration.CompatibiltyTarget) ? $"&targetFramework={queryConfiguration.CompatibiltyTarget}" : "";
8686
var serviceEndpoints = new[] { $"{_feed}/Search()" };
87-
Func<string, string> queryFunc = x => $"{x}?searchTerm={prefix}{frameworkQuery}&includePrerelease={queryConfiguration.IncludePreRelease}";
87+
Func<string, string> queryFunc = x => $"{x}?searchTerm='{prefix}'{frameworkQuery}&includePrerelease={queryConfiguration.IncludePreRelease}";
8888
XDocument document = await ExecuteAutocompleteServiceQueryAsync(serviceEndpoints, queryFunc, cancellationToken).ConfigureAwait(false);
8989

9090
if (document != null)

src/ProjectFileTools.NuGetSearch/IO/WebRequestFactory.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ namespace ProjectFileTools.NuGetSearch.IO
66
{
77
public class WebRequestFactory : IWebRequestFactory
88
{
9+
private static readonly HttpClient _httpClient = new HttpClient();
910
public async Task<string> GetStringAsync(string endpoint, CancellationToken cancellationToken)
1011
{
1112
try
1213
{
13-
using (HttpClient client = new HttpClient())
14-
{
15-
HttpResponseMessage responseMessage = await client.GetAsync(endpoint, cancellationToken).ConfigureAwait(false);
16-
responseMessage.EnsureSuccessStatusCode();
17-
return await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
18-
}
14+
HttpResponseMessage responseMessage = await _httpClient.GetAsync(endpoint, cancellationToken).ConfigureAwait(false);
15+
responseMessage.EnsureSuccessStatusCode();
16+
return await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
1917
}
2018
catch
2119
{

test/ProjectFileTools.NuGetSearch.Tests/NuGetV2ServiceFeedTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public async Task GivenPackagesFound_ReturnListOfIds(string feed, string testFil
3939
var webRequestFactory = Mock.Of<IWebRequestFactory>();
4040

4141
Mock.Get(webRequestFactory)
42-
.Setup(f => f.GetStringAsync("http://localhost/nuget/Search()?searchTerm=Common.Logging&targetFramework=netcoreapp2.0&includePrerelease=False", It.IsAny<CancellationToken>()))
42+
.Setup(f => f.GetStringAsync("http://localhost/nuget/Search()?searchTerm='Common.Logging'&targetFramework=netcoreapp2.0&includePrerelease=False", It.IsAny<CancellationToken>()))
4343
.Returns(Task.FromResult(GetXmlFromTestFile(testFile)));
4444

4545
var sut = new NuGetV2ServiceFeed(feed, webRequestFactory);

0 commit comments

Comments
 (0)