Signcodeindie
Signcodeindie

Reputation: 796

Packaging project reference in nuget : .net core

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.

Upvotes: 5

Views: 3711

Answers (3)

weshouman
weshouman

Reputation: 933

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

Upvotes: 0

Major
Major

Reputation: 6658

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>

Upvotes: 7

Signcodeindie
Signcodeindie

Reputation: 796

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.

Upvotes: 1

Related Questions