Push NuGet package programmatically using NuGet.Core

白昼怎懂夜的黑 提交于 2020-01-22 14:17:32

问题


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

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);

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

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

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


回答1:


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

The code now looks like this

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);

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.)

The PushPackage method signature looks like this:

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



回答2:


In addition to rh072005's answer:

  • Timeout is in milliseconds, be careful.
  • Uri is tricky. For NuGet.Server implementation PushPackage uri should be "http://nugetserveraddress" while for IPackageRepository objects Uri becomes "http://nugetserveraddress/nuget"
  • For large packages you will get (404) Not Found if IIS server is not configured to accept large requests.


来源:https://stackoverflow.com/questions/26549645/push-nuget-package-programmatically-using-nuget-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!