我正在编写一个控制台应用程序来创建NuGet包(使用Nuget.Core库),它将托管在我们的内部服务器上以部署到我们的应用程序中。当我们使用NuGet Package Explorer手动创建包时,这一切都正常,但我们现在需要自动执行此过程。
我将以下代码拼凑在一起以自动构建包,但我在行builder.Populate(packageMetadata);
错误是:
Value cannot be null.
Parameter name: source
这是堆栈跟踪:
at System.Linq.Enumerable.Select[TSource,TResult](IEnumerable`1 source, Func`2 selector)
at NuGet.PackageBuilder.Populate(ManifestMetadata manifestMetadata)
at BuildPackage.Create(String packageName, String path, String description) in c:\Source Code\Visual Studio Projects\Tools\Console Applications\NuGet Package Builder\BuildPackage.cs:line 134
我已经看过NuGet的源代码(请参阅here),但我无法弄清楚导致它的原因。
关于以下代码的一些注释:
以下是代码:
class BuildPackage
{
public static void Create(string packageName, string path, string description)
{
try
{
#region Get the current package version
int major = 0;
int minor = 0;
int packageNo = 0;
List<string> files = Directory.EnumerateFiles(ReferencePaths.NuGetPackages, packageName + "*").ToList();
if (files.Count > 0)
{
foreach (string file in files)
{
string[] versions = file.Replace(ReferencePaths.NuGetPackages, "").Replace(packageName + ".", "").Replace(".nupkg", "").Split('.');
int newMajor = Convert.ToInt32(versions[0]);
int newMinor = Convert.ToInt32(versions[1]);
int newPackageNo = Convert.ToInt32(versions[2]);
// Figure out if this is the latest package
if (newMajor > major ||
(newMajor == major && newMinor > minor) ||
(newMajor == major && newMinor == minor && newPackageNo > packageNo))
{
major = newMajor;
minor = newMinor;
packageNo = newPackageNo;
}
}
}
#endregion Get the current package version
#region Get the new assembly version
FileVersionInfo version = FileVersionInfo.GetVersionInfo(path + packageName + ".dll");
if (version.FileMajorPart > major ||
(version.FileMajorPart == major && version.FileMinorPart > minor))
{
major = version.FileMajorPart;
minor = version.FileMinorPart;
packageNo = 0;
}
else
{
while (File.Exists(ReferencePaths.NuGetPackages + packageName + "." + major.ToString() + "." + minor.ToString() + "." + packageNo.ToString() + ".nupkg"))
{
packageNo++;
}
}
#endregion Get the new assembly version
#region Create the package
string packageVersion = major.ToString() + "." + minor.ToString() + "." + packageNo.ToString();
string newPackageName = packageName + "." + packageVersion + ".nupkg";
ManifestMetadata packageMetadata = new ManifestMetadata();
packageMetadata.Id = packageName;
packageMetadata.Version = packageVersion;
packageMetadata.Authors = "Test";
packageMetadata.Description = description;
List<ManifestFile> manifestFiles = new List<ManifestFile>();
ManifestFile dllFile = new ManifestFile();
dllFile.Source = packageName + ".dll";
dllFile.Target = @"lib\" + packageName + ".dll";
manifestFiles.Add(dllFile);
ManifestFile pdbFile = new ManifestFile();
pdbFile.Source = packageName + ".pdb";
pdbFile.Target = @"lib\" + packageName + ".pdb";
manifestFiles.Add(pdbFile);
PackageBuilder builder = new PackageBuilder();
builder.PopulateFiles(path, manifestFiles);
builder.Populate(packageMetadata);
using (FileStream stream = File.Open(ReferencePaths.NuGetPackages + newPackageName, FileMode.OpenOrCreate))
{
builder.Save(stream);
}
#endregion Create the package
Console.WriteLine("New package created: " + newPackageName);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
有谁知道什么可能是错的?我是否需要ManifestMetadata中的更多信息?我试着填写我能想到的所有属性,但它没有帮助。
答案 0 :(得分:0)
好吧,我尝试了几种不同的方法来做同样的事情,但没有任何效果。所以我注意到http://nuget.codeplex.com/SourceControl上的代码版本都是v2.0向上,并决定检查我引用的DLL(这是.NET 4.0或4.5版本),我注意到我的NuGet.Core参考是对于版本1.6.3。
所以,我使用NuGet包管理器来获取最新版本的NuGet.Core(在编写本文时为v2.8.5),现在上面的确切代码工作得很好! *
Alanis Morissette可能会说具有讽刺意味。
File.Open
,因为我错过了文件名。