How to include a library in .NET Core 2.0

A lot of people recommend one of two solutions:

  1. Copy the library into your solution folder.

    cp -r foo/foo ./foo dotnet sln add foo/foo.csproj cd bar dotnet add reference ../foo/foo.csproj

This is a terrible solution.

Don't do this (i.e., copy and paste your library code every time you want to use it. It is bad for obvious reasons).

  1. Setup a local NuGet repository, copy your library into the local repository, and then add it.

    nuget add -name "Local" -source /home/doug/packages nuget add ~/foo/foo.nupkg -source /home/doug/packages

Then install the package:

cd bar
dotnet add package foo

This is an acceptable solution, but the workflow is quite irritating if you are actively working on your library (foo), because the -source path must be absolute.

--

I recommend you look at dotnet add package with local package file, which explains how you can have a local cache of any custom .nupkg files you want to work with.

Basically, just drop this into your solution folder:

File NuGet.Config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <packageSources>
    <add key="local" value="./packages" />
 </packageSources>
</configuration>

(Notice that ./packages is a relative path, that will work even when you check your project out on an entirely different machine or OS.)

Now if you call dotnet add package X it will also look for any file called x.nupkg in your ./packages/ folder.

Now if you want to use any custom local library, all you need to do is:

cp ~/foo/foo.nupkg ./packages
cd bar
dotnet add package foo

(Note: by default NuGet caches your .nupkg files in ~/.nuget and will restore packages from that folder if you call dotnet add package X, even if you have a different X.nupkg in your local ./packages folder. You may find the command dotnet nuget locals all --clear useful if you encounter strange behaviour to ensure you're getting the exact version of the .nupkg file you want, not some arbitrary cached version)


Another way to reference the local package in the .csproj file:

<ItemGroup>

 <Reference Include="MyAssembly">

   <HintPath>path\to\MyAssembly.dll</HintPath>

 </Reference>
</ItemGroup>

You would have to reference your library in the .csproj file:

Enter image description here

An empty .csproj file would look like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

</Project>

Now, you can have two types of references:

Project Reference - You have a project that serves as a class library in your solution and you want to reference it directly:

<ProjectReference Include="..\..\src\mylib.csproj" />

Package Reference - You have a link to a NuGet package:

<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="1.1.2" />

Inside your .csproj file, the references should be inside an "ItemGroup" block, and each reference type should have its own "ItemGroup".

Here's an example of a .csproj file with some package references and some project references:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.1.0" />
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.1" />
    <PackageReference Include="xunit" Version="2.2.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\..\src\mylib.csproj" />
    <ProjectReference Include="..\..\src\mylib2.csproj" />
  </ItemGroup>
</Project>