将编译的库添加到 NuGet 包

· 1 分钟阅读

就在前几天,我在缺失的库上发现了一个关于 NullReferenceException 的错误,该库应该存在,因为它来自我们上传的 nuget 包。

基本上,我编译了一个引用了几个项目的库类。在构建阶段,它会将 dll 文件添加到 bin 文件夹中,但是当 packaging 将其作为 nuget 包并将其安装在另一个解决方案中时,我引用并应复制到 bin 文件夹中的 dll 文件不在那里。

怎么解决

解决这个问题非常简单,您必须更新 nuspec 文件,并对于要复制的每个库,在 files 部分添加 file

<?xml version="1.0" encoding="utf-8"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <license type="expression">MIT</license>
    <projectUrl>$projectUrl$</projectUrl>
    <iconUrl>$projectIconUrl$</iconUrl>
    <description>$description$</description>
    <releaseNotes></releaseNotes>
    <copyright>$copyright$</copyright>
    <tags></tags>
  </metadata>
  <files>
        <file src="bin\Release\MyFile1.dll" target="lib\net47" />
        <file src="bin\Release\MyFile2.dll" target="lib\net47" />
        <file src="bin\Release\MyFile3.dll" target="lib\net47" />
        <file src="bin\Release\MyFile4.dll" target="lib\net47" />
   </files>
</package>