Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PackageManager.InstallPackageAsync() throws NullReferenceException #9926

Open
ericanastas opened this issue Aug 20, 2020 · 1 comment
Open
Assignees
Labels
Functionality:SDK The NuGet client packages published to nuget.org Priority:3 Issues under consideration. With enough upvotes, will be reconsidered to be added to the backlog. Resolution:Question This issues appears to be a question, not a product defect Type:DeveloperDocs

Comments

@ericanastas
Copy link

ericanastas commented Aug 20, 2020

NuGet product used: Nuget Libraries
NuGet version (5.7.0:
VS version: VS 2017
OS version: Win 10 (1909)

Details about Problem

I'm working on a plug-in management system built on top of Nuget similar to Chocolatey, PowerShell Gallery etc. I wanted to use the newer V3 libraries but the amount of abstraction and the lack of documentation is becoming a serious challenge for me.

The responses to issue #6161 have been helpful, as well as the blog posts mentioned there. However, I'm still failing to get simple prototype working that will download and extract a package.

Here's my test code
https://github.com/ericanastas/nuget-packagemanagment-test

       private static void Main(string[] args)
        {
            try
            {
                var curDir = System.IO.Directory.GetCurrentDirectory();
                ISettings settings = Settings.LoadSpecificSettings(curDir, Settings.DefaultSettingsFileName);

                List<Lazy<INuGetResourceProvider>> resourceProviders = new List<Lazy<INuGetResourceProvider>>();
                resourceProviders.AddRange(NuGet.Protocol.Core.Types.Repository.Provider.GetCoreV3());  // Add v3 API support
                IPackageSourceProvider packageSourceProvider = new PackageSourceProvider(settings);
                SourceRepositoryProvider sourceRepositoryProvider = new SourceRepositoryProvider(packageSourceProvider, resourceProviders);

                string projectRoot = System.IO.Path.GetFullPath("ProjectRoot");
                if (!System.IO.Directory.Exists(projectRoot)) System.IO.Directory.CreateDirectory(projectRoot);

                FolderNuGetProject nuGetProject = new FolderNuGetProject(projectRoot);

                string packagesFolderPath = System.IO.Path.GetFullPath("Packages");
                if (!System.IO.Directory.Exists(packagesFolderPath)) System.IO.Directory.CreateDirectory(packagesFolderPath);

                NuGetPackageManager packageManager = new NuGetPackageManager(sourceRepositoryProvider, settings, packagesFolderPath)
                {
                    PackagesFolderNuGetProject = nuGetProject
                };

                bool allowPrereleaseVersions = true;
                bool allowUnlisted = true;
                DependencyBehavior dependencyBehavior = DependencyBehavior.Lowest;

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

                INuGetProjectContext projectContext = new ConsoleProjectContext(NullLogger.Instance);

                IEnumerable<SourceRepository> primarySourceRepositories = sourceRepositoryProvider.GetRepositories();

                //packageIdentity
                string packageId = "Newtonsoft.Json";
                string packageVersion = "12.0.3";
                PackageIdentity packageIdentity = new PackageIdentity(packageId, new NuGet.Versioning.NuGetVersion(packageVersion));

                var installPackageAsyncTask = packageManager.InstallPackageAsync(packageManager.PackagesFolderNuGetProject,
                    packageIdentity, resolutionContext, projectContext,
                    primarySourceRepositories, Array.Empty<SourceRepository>(), CancellationToken.None);

                installPackageAsyncTask.GetAwaiter().GetResult();
            }
            catch (Exception exp)
            {
                using (var sw = new System.IO.StreamWriter("error.txt"))
                {
                    sw.Write(exp.ToString());
                }

                Console.WriteLine(exp.ToString());
                throw;
            }
        }

And here's the NullReferenceException that is thrown at the end when I try to call InstallPackageAsync()

System.NullReferenceException: Object reference not set to an instance of an object.
   at NuGet.ProjectManagement.FolderNuGetProject.<>c__DisplayClass13_0.<<InstallPackageAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.Common.ConcurrencyUtilities.<ExecuteWithFileLockedAsync>d__3`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at NuGet.Common.ConcurrencyUtilities.<ExecuteWithFileLockedAsync>d__3`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.PackageManagement.NuGetPackageManager.<ExecuteInstallAsync>d__85.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at NuGet.PackageManagement.NuGetPackageManager.<ExecuteNuGetProjectActionsAsync>d__76.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at NuGet.PackageManagement.NuGetPackageManager.<ExecuteNuGetProjectActionsAsync>d__76.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at NuGet.PackageManagement.NuGetPackageManager.<ExecuteNuGetProjectActionsAsync>d__76.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.PackageManagement.NuGetPackageManager.<InstallPackageAsync>d__48.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.PackageManagement.NuGetPackageManager.<InstallPackageAsync>d__47.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at NugetTest.Program.Main(String[] args) in C:\Git\5ec64809061464dc19a1d4e39961c565\NugetTest\Program.cs:line 66

What am I missing here?

@donnie-msft donnie-msft added Functionality:SDK The NuGet client packages published to nuget.org Type:DeveloperDocs labels Aug 22, 2020
@donnie-msft
Copy link
Contributor

FYI, there's been some recent work to add more examples of using the SDK. Here's a pointer to one of those:
https://docs.microsoft.com/en-us/nuget/reference/nuget-client-sdk#download-a-package

@zkat zkat added the Resolution:Question This issues appears to be a question, not a product defect label Aug 27, 2020
@zivkan zivkan self-assigned this Nov 17, 2020
@jeffkl jeffkl added Priority:3 Issues under consideration. With enough upvotes, will be reconsidered to be added to the backlog. and removed Pipeline:Icebox labels Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Functionality:SDK The NuGet client packages published to nuget.org Priority:3 Issues under consideration. With enough upvotes, will be reconsidered to be added to the backlog. Resolution:Question This issues appears to be a question, not a product defect Type:DeveloperDocs
Projects
None yet
Development

No branches or pull requests

6 participants