在 nuget 包中包含引用的项目 DLL [.Net Core RC3 *.csproj 文件]

问题描述:

我有一个包含两个项目的解决方案.第一个项目称为 Library1,它引用名为 Referencelibrary 的项目 2.我正在尝试将 ReferenceLibrary 的 DLL 嵌入 Library1 的 nuget 包中,这样我就不必发布 2 个单独的 nuget 包.通过将以下条目添加到我的 csproj 文件中,我已经能够将 ReferenceLibrary 的 DLL 嵌入到 nuget 包中(看起来如此):

I have a solution with two projects in it. First project is called Library1, which references project two called Referencelibrary. I am trying to embed the DLLs for ReferenceLibrary inside Library1's nuget package so that I don't have to publish 2 separate nuget packages. I've been able to embed ReferenceLibrary's DLL into the nuget package (so it seems) by adding the entries below into my csproj file:

  <ItemGroup>
    <ProjectReference Include="..ReferenceLibraryReferenceLibrary.csproj">
        <ReferenceOutputAssembly>true</ReferenceOutputAssembly>
        <IncludeAssets>ReferenceLibrary.dll</IncludeAssets>
        <IncludeAssets>ReferenceLibrary.pdp</IncludeAssets>
    </ProjectReference>
  </ItemGroup>

但是当我导入 nuget 包并尝试运行我的测试应用程序时,出现以下异常:

But when I import the nuget package and try to run my test app, I get the following exception:

我认为 DLL 已被嵌入,因为在将IncludeAssets"添加到 csproj 之前,我无法导入 nuget 包,因为它试图引用 ReferenceLibrary nuget 包.但是在添加这些条目后,它允许我导入它.但现在它在运行时爆炸.任何帮助将不胜感激.谢谢!

I assumed that the DLLs had been embedded because prior to adding the "IncludeAssets" to the csproj, I wasn't able to import the nuget package because it was trying to reference the ReferenceLibrary nuget package. But after adding those entries, it allowed me to import it. But now it bombs at run-time. Any help would be greatly appreciated. Thanks!

;)

现在描述了一个更新的解决方法 此处.只需将 TargetsForTfmSpecificBuildOutputTarget 节点添加到您的 .csproj 文件中,如下所示.

There is now a more up-to-date workaround described here. Simply add the TargetsForTfmSpecificBuildOutput and Target nodes to your .csproj file as shown below.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
  </PropertyGroup>
  <Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
    <ItemGroup>
      <BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
    </ItemGroup>
  </Target>
</Project>

可以在包目标中找到此扩展点的官方文档这里.

Official documentation for this extension point in the pack target can be found here.

然后,您可能还想将属性 PrivateAssets=All" 添加到 ProjectReference 元素,以禁止该项目在生成的包中显示为 NuGet 依赖项,例如:

You might also then want to add the attribute PrivateAssets="All" to the ProjectReference element to suppress that project from appearing as a NuGet dependency in the generated package, e.g.:

<ProjectReference Include="MyNonNugetDependentProject.csproj" PrivateAssets="All" />