使用 NuGet.Core 以编程方式推送 NuGet 包 [英] Push NuGet package programmatically using NuGet.Core

查看:37
本文介绍了使用 NuGet.Core 以编程方式推送 NuGet 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在打包一些文件,并使用命令行工具将它们推送到我们其中一台服务器上的 NuGet 提要.我没有使用命令行工具,而是使用 Nuget.Core 设置了一个项目并成功创建了一个包.我现在正在尝试通过 NuGet.Core 将该包从我的机器推送到 NuGet 提要.使用看起来像这样的命令行工具(我也可以使用):

I'm currently packaging some files and pushing them on to a NuGet feed on one of our servers using the command line tool. Rather than using the command line tool I've set up a project using Nuget.Core and successfully managed to create a package. I'm now trying to push that package from my machine on to the NuGet feed via NuGet.Core. Using the command line tool that looks like this (and I got this working too):

nuget.exe push package.nupkg -ApiKey MYAPIKEY -Source http://nugetpackagefeedaddress

我想要做的是使用 NuGet.Core 复制推送功能.到目前为止,我设法获得的最接近的是从 PackageRepositoryFactory 获取两个存储库,一个用于本地机器路径,一个用于包源,然后从本地检索包并尝试添加它像这样的提要:

What I want to do is replicate the push function using NuGet.Core. The closest I've managed to get so far is getting two repositories from the PackageRepositoryFactory, one for the local machine path and one for the package feed and then retrieve the package from the local one and try and add it to the feed like this:

var remoteRepo = PackageRepositoryFactory.Default.CreateRepository("myNugetPackagefeedUrl");
var localRepo = PackageRepositoryFactory.Default.CreateRepository(@"locationOfLocalPackage");
var package = localRepo.FindPackagesById("packageId").First();
remoteRepo.AddPackage(package);

此代码导致 NotSupportedException 声明不支持指定的方法"

This code results in a NotSupportedException stating the 'Specified method is not supported'

是否可以使用 NuGet.Core 推送包?用上面的代码我是否接近它?

Is it possible to push packages using NuGet.Core? and am I anywhere close to it with the above code?

注意:我知道我可以包装对 nuget.exe 的调用并从 .NET 调用它,但我要么想从 NuGet.Core 打包和推送,要么通过包装对 nuget.exe 的调用来实现两者一半以上

Note: I'm aware I could wrap the call to nuget.exe and call that from .NET but I'd either want to package and push from NuGet.Core or do both by wrapping the calls to nuget.exe rather than half and half

推荐答案

所以事实证明我完全找错了地方.我想要的方法是 PackageServer

So it turns out I was looking in the wrong place entirely. The method I wanted was PushPackage on PackageServer

代码现在看起来像这样

var localRepo = PackageRepositoryFactory.Default.CreateRepository(@"locationOfLocalPackage");
var package = localRepo.FindPackagesById("packageId").First();
var packageFile = new FileInfo(@"packagePath");
var size = packageFile .Length;
var ps = new PackageServer("http://nugetpackagefeedaddress", "userAgent");
ps.PushPackage("MYAPIKEY", package, size, 1800, false);

我不确定在更新 PackageServer 时 userAgent 参数的最佳值是多少.同样,如果有人对 timeout 或 disableBuffering 参数有任何建议,请告诉我(例如以毫秒、秒等为单位的超时)

I'm not sure what the best values for the userAgent parameter when newing up the PackageServer would be. Similarly if anyone has any advice on what the timeout or disableBuffering parameters want to be, let me know (for example is the timeout in ms, seconds etc.)

PushPackage 方法签名如下所示:

The PushPackage method signature looks like this:

void PackageServer.PushPackage(string apiKey, IPackage package, long packageSize, int timeout, bool disableBuffering)

这篇关于使用 NuGet.Core 以编程方式推送 NuGet 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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