Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify dependencies in a .NET Standard Class Library?

A .NET Standard Class Library doesn't use a .nuspec file; there is a "Package" tab in the project settings where you enter all the metadata for the NuGet package. These go in the .csproj file.

I want to create a solution with projects A and B, where project B depends on project A. Both need to go on NuGet as separate packages. But if someone installs the NuGet package for B, it should also install the package for A.

How do you specify this simple dependency for a .NET Standard Class Library? I'm not assuming that adding a reference will be enough to do this.

like image 809
Gigi Avatar asked Mar 29 '17 19:03

Gigi


People also ask

How do I add dependencies to a library?

The primary way of adding dependencies to a .NET library is referencing NuGet packages. NuGet package references allow you to quickly reuse and leverage already written functionality, but they're a common source of friction for .NET developers.

How do I add a dependency to a class in NET Core?

For most common .NET Core applications, you don't need to do this. Add a dependency by editing the project file. To add a dependency, add a <PackageReference> element inside an <ItemGroup> element. You can add to an existing <ItemGroup> or create a new one.

What is a standard class library?

.NET Standard class libraries are a merger of the platform-specific and portable library concept into a single model that provides the best of both. Platform-specific libraries are bound to a single .NET platform (for example, .NET Framework on Windows) and can therefore take significant dependencies on a known execution environment.

How to create a configuration file for the class library?

The class library does not have a configuration file by default. Dependency injection is already part of .Net Core, you will have to register your dependencies in the ConfigureService method of Startup.cs file. Create new application by clicking File -> New -> Project -> Installed -> C# -> Web -> ASP.Net Web Core application.


2 Answers

NET Standard .csproj files use PackageReference and ProjectReference MSBuild items to control dependencies on packages loaded from NuGet or your solution, respectively. They both support metadata tags PrivateAssets, IncludeAssets, ExcludeAssets, which control what exactly and how your project depends on in those dependencies. By default, all dependencies are auto-generated by the Visual Studio with PrivateAssets, which means those dependencies are consumed, but they're not marked as dependencies for consumers of your project (which is gonna be a package in NuGet). So, you just need to replace the PrivateAssets metadata with the IncludeAssets (maybe with proper values). For example, my SuperPackage.csproj file looks like below:

..................
  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.6.0">
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Newtonsoft.Json" Version="11.0.2">
      <IncludeAssets>compile</IncludeAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj">
      <IncludeAssets>compile</IncludeAssets>
    </ProjectReference>
  </ItemGroup>
</Project>

So, I have the PackageReference to the 'Newtonsoft.Json' package from NuGet marked as a dependency, and the ProjectReference to the ClassLibrary1 project within the single solution. When the SuperPackage project is built with checked 'Generate NuGet package on build', I get the following SuperPackage.nupkg:

enter image description here

Check this doc for more details: https://learn.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files

like image 104
Hennadii Lutsyshyn Avatar answered Oct 16 '22 09:10

Hennadii Lutsyshyn


From what I have found it is not yet supported in .csproj files and you should use .nuspec to specify dependencies. Have a look how they do it in xUnit, for example.

like image 38
Andrii Litvinov Avatar answered Oct 16 '22 10:10

Andrii Litvinov