user7437257
user7437257

Reputation: 11

How do I publish a package using NuGet API?

I am trying to publish NuGet packages to internal Nexus NuGet repository using NuGet API in a C# Console application. Code snippet:

List<Lazy<INuGetResourceProvider>> providers = new List<Lazy<INuGetResourceProvider>>();
providers.AddRange(Repository.Provider.GetCoreV3());  
PackageSource packageSource = new PackageSource("http://nexus:8081/nexus/nuget-repo");
SourceRepository sourceRepository = new SourceRepository(packageSource, providers);
ISettings settings = NuGet.Configuration.Settings.LoadDefaultSettings(@"C:\Users\<user>\AppData\Roaming\NuGet", null, new MachineWideSettings());
var packageSourceProvider = new PackageSourceProvider(settings);
packageSourceProvider.SaveActivePackageSource(packageSource);
                
 
try
{
    await PushRunner.Run(
        settings,
        packageSourceProvider,
        filetouse,
        "http://nexus:8081/nexus/nuget-repo",
        "MY API KEY", "", "", 10, false, true,
        new Logger());
}
catch(Exception e) 
{
}

The problem I am getting is that the call to PushRunner completes immediately with no exceptions and the package is not pushed. I see a message in the console that NuGet is going to push the package, but no other logging is emitted after that. I have tried pushing the same package using nuget.exe and it works fine. The Nexus server requires username, password, and API key to publish NuGet artifacts.

NuGet API is lacking documentation so I am not sure what the issue is and whether I am doing this the right way or not.

Upvotes: 1

Views: 1713

Answers (2)

Ivan Uthus
Ivan Uthus

Reputation: 21

It has been some time now, but I have successfully just done this by using the NuGet.CommandLine.XPlat NuGet package. This is the package that implements the NuGet part of the dotnet.exe. The way I got it to work in a really easy way was to execute the Program.Main method as you would call the exe itself. The documentation for dotnet nuget push is described here: https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-nuget-push?tabs=netcore21.

So then the code becomes:

Program.Main(new[] { "push", packagePath, "-k", apiKey, "-s", serverUrl });

You only get a return code back, but currently (in version 4.8.0 of the package), there is another version of Main (MainInternal), where you can intercept and override the logging of the operation as well.

Upvotes: 2

Justin Emgarten
Justin Emgarten

Reputation: 2368

To handle this in code you can add a credential provider. Here is the code used by nuget.exe push:

https://github.com/NuGet/NuGet.Client/blob/f55f41629b0016072dc24f6effcc72e430609b6f/src/NuGet.Clients/NuGet.CommandLine/Commands/Command.cs#L156-L173

Alternatively you can add the credentials to your nuget.config, then read the settings from that file. If you can get nuget.exe push working first with nuget.config so that there are no prompts, then it should easier to load the same settings and run the code you already have.

Upvotes: 0

Related Questions