diff --git a/src/Paket.Core/NuGetV2.fs b/src/Paket.Core/NuGetV2.fs index 0719a8a6e7..fb10623e6b 100644 --- a/src/Paket.Core/NuGetV2.fs +++ b/src/Paket.Core/NuGetV2.fs @@ -762,7 +762,8 @@ let GetVersions force root (sources, packageName:PackageName) = if not (String.containsIgnoreCase "teamcity" source.Url || String.containsIgnoreCase"feedservice.svc" source.Url ) then yield getVersionsCached "Json" tryGetPackageVersionsViaJson (nugetSource, auth, source.Url, packageName) ] - match NuGetV3.getAllVersionsAPI(source.Authentication,source.Url) with + let apiV3 = NuGetV3.getAllVersionsAPI(source.Authentication,source.Url) |> Async.AwaitTask + match apiV3 |> Async.RunSynchronously with | None -> v2Feeds | Some v3Url -> (getVersionsCached "V3" tryNuGetV3 (nugetSource, auth, v3Url, packageName)) :: v2Feeds | NuGetV3 source -> diff --git a/src/Paket.Core/NuGetV3.fs b/src/Paket.Core/NuGetV3.fs index 4cee0a2b06..ec90b12e11 100644 --- a/src/Paket.Core/NuGetV3.fs +++ b/src/Paket.Core/NuGetV3.fs @@ -11,6 +11,7 @@ open Paket.Utils open Paket.Xml open Paket.PackageSources open Paket.Requirements +open Paket.Logging /// [omit] type JSONResource = @@ -27,10 +28,10 @@ type JSONRootData = { Resources : JSONResource [] } /// [omit] -let private searchDict = new System.Collections.Concurrent.ConcurrentDictionary<_,_>() +let private searchDict = new System.Collections.Concurrent.ConcurrentDictionary<_,System.Threading.Tasks.Task<_>>() /// [omit] -let private allVersionsDict = new System.Collections.Concurrent.ConcurrentDictionary<_,_>() +let private allVersionsDict = new System.Collections.Concurrent.ConcurrentDictionary<_,System.Threading.Tasks.Task<_>>() /// Calculates the NuGet v3 URL from a NuGet v2 URL. let calculateNuGet3Path(nugetUrl:string) = @@ -59,37 +60,38 @@ let calculateNuGet2Path(nugetUrl:string) = /// [omit] let getSearchAPI(auth,nugetUrl) = - match searchDict.TryGetValue nugetUrl with - | true,v -> v - | _ -> - try + searchDict.GetOrAdd(nugetUrl, fun nugetUrl -> + async { match calculateNuGet3Path nugetUrl with - | None -> None + | None -> return None | Some v3Path -> let source = { Url = v3Path; Authentication = auth } - Some (PackageSources.getNuGetV3Resource source AutoComplete |> Async.RunSynchronously) - with - | _ -> None - |> fun result -> - searchDict.[nugetUrl] <- result - result + let! v3res = PackageSources.getNuGetV3Resource source AutoComplete |> Async.Catch + return + match v3res with + | Choice1Of2 s -> Some s + | Choice2Of2 ex -> + if verbose then traceWarnfn "getAllVersionsAPI: %s" (ex.ToString()) + None + } |> Async.StartAsTask) /// [omit] let getAllVersionsAPI(auth,nugetUrl) = - match allVersionsDict.TryGetValue nugetUrl with - | true,v -> v - | _ -> - try + allVersionsDict.GetOrAdd(nugetUrl, fun nugetUrl -> + async { match calculateNuGet3Path nugetUrl with - | None -> None + | None -> return None | Some v3Path -> let source = { Url = v3Path; Authentication = auth } - Some (PackageSources.getNuGetV3Resource source AllVersionsAPI |> Async.RunSynchronously) - with - | _ -> None - |> fun result -> - allVersionsDict.[nugetUrl] <- result - result + let! v3res = PackageSources.getNuGetV3Resource source AllVersionsAPI |> Async.Catch + return + match v3res with + | Choice1Of2 s -> Some s + | Choice2Of2 ex -> + if verbose then traceWarnfn "getAllVersionsAPI: %s" (ex.ToString()) + None + } |> Async.StartAsTask) + /// [omit] let extractAutoCompleteVersions(response:string) = @@ -153,7 +155,8 @@ let extractPackages(response:string) = JsonConvert.DeserializeObject(response).Data let private getPackages(auth, nugetURL, packageNamePrefix, maxResults) = async { - match getSearchAPI(auth,nugetURL) with + let! apiRes = getSearchAPI(auth,nugetURL) |> Async.AwaitTask + match apiRes with | Some url -> let query = sprintf "%s?q=%s&take=%d" url packageNamePrefix maxResults let! response = safeGetFromUrl(auth |> Option.map toBasicAuth,query,acceptJson) diff --git a/src/Paket.Core/PublicAPI.fs b/src/Paket.Core/PublicAPI.fs index 36c4e32751..549224edab 100644 --- a/src/Paket.Core/PublicAPI.fs +++ b/src/Paket.Core/PublicAPI.fs @@ -465,7 +465,8 @@ type Dependencies(dependenciesFileName: string) = |> Seq.choose (fun source -> match source with | NuGetV2 s -> - match NuGetV3.getSearchAPI(s.Authentication,s.Url) with + let res = NuGetV3.getSearchAPI(s.Authentication,s.Url) |> Async.AwaitTask |> Async.RunSynchronously + match res with | Some _ -> Some(NuGetV3.FindPackages(s.Authentication, s.Url, searchTerm, maxResults)) | None -> Some(NuGetV2.FindPackages(s.Authentication, s.Url, searchTerm, maxResults)) | NuGetV3 s -> Some(NuGetV3.FindPackages(s.Authentication, s.Url, searchTerm, maxResults))