Quantcast
Channel: Active questions tagged nuget-package - Stack Overflow
Viewing all articles
Browse latest Browse all 3067

NuGet PackageManager.InstallPackageAsync() throws NullReferenceException

$
0
0

I'm working on a plug-in management system built on top of Nuget similar to Chocolatey, PowerShell Gallery etc.

I want to use the newer V3 libraries rather then the deprecated V2 Nuget.Core library used by Chocolatey. However, the amount of abstraction and the lack of documentation in the V3 libraries is becoming a serious challenge for me. 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?


Viewing all articles
Browse latest Browse all 3067

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>