5

Currently I have a requirement where we have separate assemblies for contract and implementation. After creating a nuget package and attempting to consume nuget package, it fails because Package manager is unable to find dependent (contract) assembly.

This seems to be an open issue in .net core.

https://github.com/dotnet/cli/issues/3959

Unable to understand why such simple thing will not work in .net core. Looking for workaround for this issue.

Signcodeindie
  • 776
  • 1
  • 10
  • 28

3 Answers3

7

It is simple to solve. You have 2 options:

A) Pack and Publish all your projects as Nuget packages:

You just add your dependencies as ProjectReference into your main projects. And continue development using project references. Also must pack all dependency projects as well. When you want to publish your packages using the same version just run:

dotnet pack -p:PackageVersion=2.1.0 also can add any other pack arguments.

Since during pack all ProjectReference will be transformed to Package dependencies. And version number is cascading into all package.

In this case your original main project and all of its dependencies will be Nuget packaged. Now you have to publish ALL. And when you want to install your Nuget package it will install all of its dependencies as well with the same version specified.

B) Package all output DLLs into a single Nuget package:

You can Publish only one Project as Nuget package and pack all other DLL into that package. First suppress pack to transform dependency from Project to Package. Find your ProjectReference and add PrivateAssets="All" to it. Should look like this:

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

And add the following section to your .csproj file (to the project which should be packaged) to package dependency DLLs, change the DLL name and Framework version in <PackagePath>.

<ItemGroup>
  <_PackageFiles Include="$(OutputPath)\yourproj.dll">
    <BuildAction>None</BuildAction>
    <PackagePath>lib\net5.0</PackagePath>
  </_PackageFiles>
</ItemGroup>
Major
  • 5,948
  • 2
  • 45
  • 60
  • B) and tags are not recognized by IDE – Roman Prykhodko Oct 16 '20 at 07:04
  • Do you have .Net Core project? It works fine for me with .Net Core and .Net5 – Major Oct 16 '20 at 07:06
  • I have core 3.1 – Roman Prykhodko Oct 16 '20 at 07:26
  • As for method A - what the key point of this method? I might not understand proterly, but dotnet pack doesnt include reference (( – Roman Prykhodko Oct 16 '20 at 08:32
  • I have tried it out with a .NET Core 3.1 project using VS 2019 and option B) was perfectly working. Do you have VS 2019? Also you might added duplicate ProjectReference or copied the XML nodes into wrong places... I think you left out the part! – Major Oct 16 '20 at 09:17
  • Yes 'dotnet pack' won't include project references into the package. But it will AUTOMATICALLY transform ProjectReference to PackageReference. So instead of using a project output your package will depend on another Nuget package(s). So you have to PACKAGE all referenced projects and PUBLISH all. And when you want to install your original Nuget package it will download all dependent packages. Locally those are project references. So option A) is a totally different approach! My solution will help with the versioning. Since package reference depends on specific version as well. – Major Oct 16 '20 at 09:23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/223162/discussion-between-major-and-roman-prykhodko). – Major Oct 16 '20 at 14:14
  • Huh, this didn't work for me. My project is targeting netstandard2.1 – Steve Boniface Apr 08 '22 at 20:21
  • 1
    idk - why packaging has to be so annoying and convoluted to deal with but thank you dude - solution B solved my issue – vpetkovic Aug 28 '23 at 21:28
  • @SteveBoniface netstandard2.1 is listed here so should be fine: https://learn.microsoft.com/en-us/nuget/reference/target-frameworks#supported-frameworks – Major Aug 29 '23 at 11:51
1

After reading documentation I understood .net core discourages project reference instead advises to use package reference. This is mentioned in description heading in following doc.

https://github.com/dotnet/docs/blob/master/docs/core/tools/dotnet-pack.md

I published my contract assembly to nuget package and consumed it in implementation as nuget package.

Signcodeindie
  • 776
  • 1
  • 10
  • 28
0

In my case I worked around that by adding the following section to the csproj file

  <ItemGroup>
    <BuildOutputInPackage Include="$(OutputPath)settings.json" />
    <BuildOutputInPackage Include="$(OutputPath)First.dll" />
    <BuildOutputInPackage Include="$(OutputPath)Second.dll" />
    <BuildOutputInPackage Include="$(OutputPath)Some.ico" />
    <BuildOutputInPackage Include="$(OutputPath)Even.exe" />
  </ItemGroup>

and then using for example

dotnet build
dotnet pack MySolution/MyProject.csproj-c Release

Further Reading: dotnet does not pack exe by default

weshouman
  • 632
  • 9
  • 17