/// <summary>
 /// Creates a new PackageCatalog.
 /// </summary>
 /// <param name="catalog">The address of the NuGet catalog endpoint.</param>
 /// <param name="storage">The Storage object responsible for loading and saving the file.</param>
 /// <param name="address">The storage resource URI to save the package to.</param>
 /// <param name="nugetServiceUrls">The set of NuGet endpoints to use when determining if a package is the latest stable version.</param>
 public PackageCatalog(Uri catalog, IStorage storage, Uri address, NugetServiceEndpoints nugetServiceUrls) : base(storage, address)
 {
     this.Catalog = catalog;
     this.Packages = new SortedList<string, PackageInfo>();
     this.NugetServiceUrls = nugetServiceUrls;
 }
        /// <summary>
        /// Gets the latest stable version of this package
        /// </summary>
        /// <returns>The registration for the latest stable version of this package.</returns>
        internal RegistrationIndexPackage GetLatestStableVersion(NugetServiceEndpoints nugetServiceUrls)
        {
            RegistrationIndex registrationIndex;

            // Download the registration index for the package
            using (WebClient client = new WebClient())
            {
                Uri registrationUrl = nugetServiceUrls.ComposeRegistrationUrl(this.PackageId);

                string registrationIndexJson;
                try
                {
                    registrationIndexJson = client.DownloadString(registrationUrl);
                }
                catch (WebException we)
                {
                    HttpWebResponse response = we.Response as HttpWebResponse;
                    if (response != null && response.StatusCode == HttpStatusCode.NotFound)
                    {
                        // If the page can't be found, there's not much we can do. Log the error and   
                        // return null, indicating that we can't get the latest stable version for this package.  
                        Trace.TraceError($"Could not download registration file for the package URL {registrationUrl}.");
                        return null;
                    }
                    else
                    {
                        // Any other error is likely an intermittent network issue, so we should rethrow.  
                        throw;
                    }
                }

                registrationIndex = RegistrationIndex.Deserialize(registrationIndexJson);
            }

            return registrationIndex.GetLatestStableVersion();
        }