如何使用 nuget.core 获取包大小? [英] how to get package size with nuget.core?

查看:34
本文介绍了如何使用 nuget.core 获取包大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法在下载之前知道包的大小.我正在使用 nuget 包来部署组件,并且知道更新的大小将是一个不错的功能.

i am wondering if there is a way to know the size of a package before downloading. I'm using nuget packages for deploying components, and knowing the size of an update would be a nice feature.

推荐答案

NuGet 库包源:

 <d:PackageSize m:type="Edm.Int64">223865</d:PackageSize>

很遗憾,您从 NuGet.Core.dll 返回的 Package 对象中没有此 PackageSize.

Unfortunately this PackageSize is not available from the Package object you get back from NuGet.Core.dll.

获取 PackageSize 的一种方法是从 NuGet 的源代码中获取 DataServicePackageRepository 类和 DataServicePackage 类并对其进行修改,以便 PackageSize 属性可用于 DataServicePackage 对象.

To get the PackageSize one way is to take the DataServicePackageRepository class and the DataServicePackage class from NuGet's source code and modify them so the PackageSize property is available on the DataServicePackage object.

public long PackageSize { get; set; }

我将这两个类重命名为 CustomDataServicePackageRepository 和 CustomDataServicePackage.

I took these two classes and renamed them to be CustomDataServicePackageRepository and CustomDataServicePackage.

您可以在应用程序中包含这两个类,并通过将返回的 IPackage 转换为自定义 DataServicePackage 类然后访问 PackageSize 属性来使用它们.

You can include these two classes in your application and use them by casting the IPackage returned to your custom DataServicePackage class and then access the PackageSize property.

    var repo = new CustomDataServicePackageRepository(new Uri("https://www.nuget.org/api/v2/"));
    var packages = repo.Search(null, false)
        .Where(package => package.IsLatestVersion)
        .OrderByDescending(package => package.DownloadCount)
        .AsBufferedEnumerable(30)
        .Where(package => package.IsReleaseVersion())
        .Take(10);

    foreach (IPackage package in packages) {
        var dataServicePackage = package as CustomDataServicePackage;
        long size = dataServicePackage == null ? -1 : dataServicePackage.PackageSize;
        Console.WriteLine("Package: {0}, Size: {1}", package.ToString(), size);
    }

NuGet 本身不使用 PackageSize.相反,它开始下载 NuGet 包并查看 HTTP 响应中返回的 ContentLength 以查找 NuGet 包的大小.然后用于在 Visual Studio 中显示百分比进度.

NuGet itself does not use the PackageSize. Instead it starts the download of the NuGet package and looks at the ContentLength returned in the HTTP response to find the size of the NuGet package. This is then used to show the percentage progress in Visual Studio.

这篇关于如何使用 nuget.core 获取包大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆