c# - 如何强制msbuild和nuget将引用的Lib打包为dll?

标签 c# .net visual-studio nuget

我有 2 个项目:
项目A是.Net Standard 2.0
项目B是.Net Framework 4.6.1

在 B 中,我像这样引用 A (csproj):

 <ItemGroup>
<ProjectReference Include="A.csproj">
  <Project>{60DEB41B-8670-4199-95C2-FE68EB06B099}</Project>
  <Name>A</Name>
</ProjectReference>

现在,当我打包 B 的 nuget 时,我无法访问 A,因为它无法解决依赖关系。

以下是由 gitlab ci 运行的命令,用于创建项目 B:

nuget 恢复
msbuild B.csproj
nuget pack -版本 $VERSION
dotnet nuget 推送 **/*.nupkg ...

如何将A的lib作为DLL包含在B中?

最佳答案

实际上,在项目级别添加引用并不会在打包过程中自动添加对应的引用dll。您需要手动向 nuget 包中添加一个额外的 dll

我有两个解决方案:

解决方案一

正如Cem.S所说,使用-IncludeReferencedProjects来打包您的项目。

此外,由于您打包了一个 net Framework nuget 项目,因此您只需 use nuget.exe cli to pack the project with the created nuspec file :

使用此命令打包您的项目B:

nuget pack xxx\B.csproj -IncludeReferencedProjects

解决方案二

或者只需在B.nuspec文件中添加这样的节点:

<?xml version="1.0" encoding="utf-8"?>
<package >
  <metadata>
    <id>xxx</id>
    <version>1.0.0</version>
    <title>xxx</title>
    <authors>xxx</authors>
    .....
  </metadata>
  <files>
   <file src="xxx\xxx\A.dll" target="lib\net461" />
  <files>
</package>

然后,运行nuget pack xxx\B.csproj生成新版本。

在安装新版本之前,您应该first clean nuget caches first .

关于c# - 如何强制msbuild和nuget将引用的Lib打包为dll?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64500666/

相关文章:

c# - 仅刷新 MVC4 中的部分 View

c# - Tendermint、GRPC 和 C# - 流被 RST_STREAM 终止,错误代码为 : PROTOCOL_ERROR

c# - 无法在 ASP.NET MVC 中捕获异常

c# - 如果使用了特定的枚举值,如何生成警告

C# 远程调试器 - 无法找到或打开 PDB 文件

c# - 如何获取控件的屏幕截图? DrawToBitmap 不工作

.net - 在 10 亿次迭代中获得相同 GUID 的机会是多少?

c# - 跨线程操作无效

asp.net-mvc - Visual Studio : How to override the default "Build Action" for certain extension types per project or solution?

c++ - 从 VS6 移植到 VS2008 时在不寻常的体系结构中使用单元测试?