Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including referenced project DLLs in nuget package [.Net Core RC3 *.csproj file]

Tags:

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="..\ReferenceLibrary\ReferenceLibrary.csproj">         <ReferenceOutputAssembly>true</ReferenceOutputAssembly>         <IncludeAssets>ReferenceLibrary.dll</IncludeAssets>         <IncludeAssets>ReferenceLibrary.pdp</IncludeAssets>     </ProjectReference>   </ItemGroup> 

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

Exception Screenshot 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!

;)

like image 894
Zorthgo Avatar asked Feb 01 '17 12:02

Zorthgo


1 Answers

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.

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" /> 
like image 143
Neo Avatar answered Sep 21 '22 15:09

Neo