Skip to content

Nuget.Client API How to get download progress? #5095

Closed

Description

@danwalmsley

Hi,

I am using the nugget.client api to download packages like this:

public async Task InstallPackage(string packageId, string version)
        {
            PackageIdentity identity = new PackageIdentity(packageId, new NuGet.Versioning.NuGetVersion(version));

            List<Lazy<INuGetResourceProvider>> providers = new List<Lazy<INuGetResourceProvider>>();

            providers.AddRange(Repository.Provider.GetCoreV3());  // Add v3 API support
            providers.AddRange(Repository.Provider.GetCoreV2());  // Add v2 API support

            var settings = (NuGet.Configuration.Settings)NuGet.Configuration.Settings.LoadDefaultSettings(Platform.ReposDirectory, null, new MachineWideSettings(), false, true);

            ISourceRepositoryProvider sourceRepositoryProvider = new SourceRepositoryProvider(settings, providers);  // See part 2

            using (var installedPackageCache = GetCache())
            {
                var project = new AvalonStudioExtensionsFolderProject(new NuGet.PhysicalFileSystem(Platform.ReposDirectory), GetFramework(), installedPackageCache, Platform.ReposDirectory);

                if (!project.PackageExists(identity))
                {
                    var packageManager = new NuGetPackageManager(sourceRepositoryProvider, settings, Platform.ReposDirectory)
                    {
                        PackagesFolderNuGetProject = project,
                    };

                    bool allowPrereleaseVersions = true;
                    bool allowUnlisted = false;

                    ResolutionContext resolutionContext = new ResolutionContext(
                        DependencyBehavior.Lowest, allowPrereleaseVersions, allowUnlisted, VersionConstraints.None);

                    INuGetProjectContext projectContext = new ProjectContext(_logger);
                    var sourceRepositories = new List<SourceRepository>();
                    sourceRepositories.Add(new SourceRepository(new NuGet.Configuration.PackageSource(DefaultPackageSource), providers));
                    
                    await packageManager.InstallPackageAsync(packageManager.PackagesFolderNuGetProject,
                        identity, resolutionContext, projectContext, sourceRepositories,
                        Array.Empty<SourceRepository>(),  // This is a list of secondary source respositories, probably empty
                        CancellationToken.None);
                }
                else
                {
                    _logger.LogInformation("Package is already installed.");
                }
            }
        }

However I'm not sure how to get the download progress? Is this possible, for large packages it would be nice to give ui feedback on dl progress,

Many Thanks in advance.

Dan

Activity

  1. emgarten commented on Apr 20, 2017

    @emgarten
    Member

    There isn't currently a way to get download progress. But it could be done by injecting a custom HttpMessageHandler.

    To do this you would need to create something like HttpHandlerResourceV3Provider and add it to your provider list. Currently the handler being used comes from this call
    providers.AddRange(Repository.Provider.GetCoreV3());

  2. danwalmsley commented on Apr 21, 2017

    @danwalmsley
    Author

    @emgarten I tried this...

    public class AvalonHttpMessageHandler : DelegatingHandler
        {
            public AvalonHttpMessageHandler(HttpClientHandler handler) : base(handler)
            {
    
            }
    
            protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
            {
                Console.WriteLine("Intercepting requests");
                return base.SendAsync(request, cancellationToken);
            }
        }
    
        public class AvalonHttpHandlerResourceV3Provider : ResourceProvider
        {
            public AvalonHttpHandlerResourceV3Provider()
                : base(typeof(HttpHandlerResource),
                      nameof(AvalonHttpHandlerResourceV3Provider),
                      NuGetResourceProviderPositions.Last)
            {
            }
    
            public override Task<Tuple<bool, INuGetResource>> TryCreate(SourceRepository source, CancellationToken token)
            {
                //Debug.Assert(source.PackageSource.IsHttp, "HTTP handler requested for a non-http source.");
    
                HttpHandlerResourceV3 curResource = null;
    
                if (source.PackageSource.IsHttp)
                {
                    curResource = CreateResource(source.PackageSource);
                }
    
                return Task.FromResult(new Tuple<bool, INuGetResource>(curResource != null, curResource));
            }
    
            private static HttpHandlerResourceV3 CreateResource(PackageSource packageSource)
            {
                var sourceUri = packageSource.SourceUri;
                var proxy = ProxyCache.Instance.GetProxy(sourceUri);
    
                // replace the handler with the proxy aware handler
                var clientHandler = new HttpClientHandler
                {
                    Proxy = proxy,
                    AutomaticDecompression = (DecompressionMethods.GZip | DecompressionMethods.Deflate)
                };
    
                // HTTP handler pipeline can be injected here, around the client handler
                HttpMessageHandler messageHandler = new AvalonHttpMessageHandler(clientHandler);
    
                if (proxy != null)
                {
                    messageHandler = new ProxyAuthenticationHandler(clientHandler, HttpHandlerResourceV3.CredentialService, ProxyCache.Instance);
                }
    
                {
                    var innerHandler = messageHandler;
    
                    messageHandler = new HttpSourceAuthenticationHandler(packageSource, clientHandler, HttpHandlerResourceV3.CredentialService)
                    {
                        InnerHandler = innerHandler
                    };
                }
    
                var resource = new HttpHandlerResourceV3(clientHandler, messageHandler);
    
                return resource;
            }
        }
    

    everything still works, but I only get to intercept messages with the nugget protocol, not when actually downloading the package files, any clues on what I need to do?

  3. mishra14 commented on Oct 17, 2017

    @mishra14
    Contributor

    pinging @emgarten
    @danwalmsley were you able to get this to work?

  4. added this to the Backlog milestone on Oct 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions