You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using the v6.3 Nuget.Packaging and NuGet.PackageManagement libaries, I am unable to install packages like I can when using v.4.0 equivalent.
It seems to be releated to my PluginProject.InstallPackageAsync override, v6 throws a null refernce exception but not with v4. Not calling the base.InstallPackagesAsync fails to install any packages in my folder so im sure its key.
// Class is derived from FolderNuGetProject and method overidden
public override async Task<bool> InstallPackageAsync(PackageIdentity packageIdentity, DownloadResourceResult downloadResourceResult,
INuGetProjectContext nuGetProjectContext, CancellationToken token)
{
var result = await base.InstallPackageAsync(packageIdentity, downloadResourceResult, nuGetProjectContext, token);
await _packagesConfigProject.InstallPackageAsync(packageIdentity, downloadResourceResult, nuGetProjectContext, token);
return result;
}
I am sure this is to do with me using the library as opposed to a bug. Pointers appreciated and full details below.
Even when using the FolderNuGetProject directly, it throws - so I have found the source of the error need to debug the source to understand why... nuGetProjectContext.PackageExtractionContext is null.
Detail
I writing an app which has plugins downloadable from nuget, which can then be dynamically loaded into my application - similair to this interesting talk.
I derive my class from the NuGet.ProjectManagement.FolderNuGetProject, however files are not installed after calling NuGetPackageManager.InstallPackageAsync with my custom folder project, only packages.config is updated per below:
PS C:\ProgramData\unified.msp> dir
Directory: C:\ProgramData\unified.msp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 07/08/2022 11:07 156 packages.config
The Project class is below:
public class PluginProject : FolderNuGetProject
{
private readonly PackagesConfigNuGetProject packagesConfigProject;
public PluginProject(string root) : base(root)
{
this.packagesConfigProject = new PackagesConfigNuGetProject(root, new Dictionary<string, object>()
{
{ "TargetFramework", NuGetFramework.AnyFramework },
{ "Name", "My.Package.Installer" }
});
}
public override Task<IEnumerable<PackageReference>> GetInstalledPackagesAsync(CancellationToken cancellationToken)
{
return this.packagesConfigProject.GetInstalledPackagesAsync(cancellationToken);
}
public override Task<bool> InstallPackageAsync(PackageIdentity packageIdentity, DownloadResourceResult downloadResourceResult,
INuGetProjectContext nuGetProjectContext, CancellationToken token)
{
// THIS LINE THROWS NULL REFERENCE WITH v6, BUT NOT v4.
// I REMOVE THE BASE METHOD CALL IN v6 BUT NOT FILES ARE INSTALLED IN PACKAGES DIRECTORY
var result = await DeletePackage(packageIdentity, nuGetProjectContext, token);
await
packagesConfigProject.UninstallPackageAsync(packageIdentity, nuGetProjectContext, token);
return result;
}
}
And the overall plugin manager is below (which scaffolds everything together into the NuGetPackangeManager method calls. It does list installed packages, and can install (ableit without the actual file contents):
public class NugetPluginManager
{
private readonly IOptions<NugetPluginManagerSettings> settings;
private readonly LoggerAdapter logger;
public NugetPluginManager(IOptions<NugetPluginManagerSettings> settings, ILogger<NugetPluginManager> logger)
{
this.settings = settings;
this.logger = new LoggerAdapter(logger);
}
private static (SourceRepository Repository, List<Lazy<INuGetResourceProvider>> Providers) GetRepository(string repositoryUrl)
{
var providers = new List<Lazy<INuGetResourceProvider>>();
providers.AddRange(Repository.Provider.GetCoreV3());
var packageSource = new PackageSource(repositoryUrl);
var repository = new SourceRepository(packageSource, providers);
if(packageSource.SourceUri.Scheme != Uri.UriSchemeHttps)
throw new ArgumentException($"{repositoryUrl} is not https.", nameof(repositoryUrl));
return (repository, providers);
}
public async Task<IEnumerable<PackageReference>> GetInstalledPackages(CancellationToken cancellationToken = default)
{
var commonApplicationDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
var rootAppPath = Path.Combine(commonApplicationDataPath, "unified.msp");
var packagesPath = Path.Combine(rootAppPath, "packages");
var project = new PluginProject(rootAppPath);
return await project.GetInstalledPackagesAsync(cancellationToken);
}
public async Task Install(string repositoryUrl, string packageName, string version, bool includeDependencies,
bool includePreRelease, CancellationToken cancellationToken = default)
{
var (repository, providers) = GetRepository(repositoryUrl);
var commonApplicationDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
var rootAppPath = Path.Combine(commonApplicationDataPath, "unified.msp");
var packagesPath = Path.Combine(rootAppPath, "packages");
var project = new PluginProject(rootAppPath);
var defaultSettings = Settings.LoadDefaultSettings(rootAppPath, null, new XPlatMachineWideSetting());
// Create the package manager - TODO: https://github.com/NuGet/Home/issues/8479
#pragma warning disable CS0618 // Type or member is obsolete
var repositoryProvider = new SourceRepositoryProvider(defaultSettings, providers);
#pragma warning restore CS0618 // Type or member is obsolete
var packageManager = new NuGetPackageManager(repositoryProvider, defaultSettings, packagesPath)
{
PackagesFolderNuGetProject = project
};
var dependencyBehaviour = includeDependencies ? DependencyBehavior.Lowest : DependencyBehavior.Ignore;
var resolutionContext =
new ResolutionContext(dependencyBehaviour, includePreRelease, false, VersionConstraints.None);
var projectContext = new EmptyNuGetProjectContext();
await packageManager.InstallPackageAsync(
packageManager.PackagesFolderNuGetProject,
new PackageIdentity(packageName, NuGetVersion.Parse(version)),
resolutionContext,
projectContext,
repository,
Array.Empty<SourceRepository>(),
cancellationToken);
}
}
The below packages work:
And the below packages throw Object reference not set to an instance of an object:
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=NuGet.PackageManagement
StackTrace:
at NuGet.ProjectManagement.FolderNuGetProject.<>c__DisplayClass13_0.<<InstallPackageAsync>b__0>d.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NuGet.Common.ConcurrencyUtilities.<ExecuteWithFileLockedAsync>d__5`1.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at NuGet.Common.ConcurrencyUtilities.<ExecuteWithFileLockedAsync>d__5`1.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Unified.Msp.InstallCommon.NuGet.MspPackagesNuGetProject.<InstallPackageAsync>d__3.MoveNext() in
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
When using the v6.3
Nuget.Packaging
andNuGet.PackageManagement
libaries, I am unable to install packages like I can when using v.4.0 equivalent.It seems to be releated to my
PluginProject.InstallPackageAsync
override, v6 throws a null refernce exception but not with v4. Not calling thebase.InstallPackagesAsync
fails to install any packages in my folder so im sure its key.I am sure this is to do with me using the library as opposed to a bug. Pointers appreciated and full details below.
Even when using the
FolderNuGetProject
directly, it throws - so I have found the source of the error need to debug the source to understand why...nuGetProjectContext.PackageExtractionContext
is null.Detail
I writing an app which has plugins downloadable from nuget, which can then be dynamically loaded into my application - similair to this interesting talk.
I derive my class from the NuGet.ProjectManagement.FolderNuGetProject, however files are not installed after calling
NuGetPackageManager.InstallPackageAsync
with my custom folder project, onlypackages.config
is updated per below:But no files in the directory other than that:
The Project class is below:
And the overall plugin manager is below (which scaffolds everything together into the
NuGetPackangeManager
method calls. It does list installed packages, and can install (ableit without the actual file contents):The below packages work:
And the below packages throw
Object reference not set to an instance of an object
:Beta Was this translation helpful? Give feedback.
All reactions