14

I don't know much about .NET yet, so I guess I'm missing something obvious.

I created a library (targeted as a DLL file, set for .NET standard 2.0), packaged it both as a DLL file and as a NuGet package. Now I want to use the library in another project, on ASP.NET Core 2.0. How should I do it?

I am currently on a Linux VM, so I use Visual Studio Code, and therefore I would prefer some solution without using the full Visual Studio. I tried some solutions using the full Visual Studio, but that didn't work for me, because I haven't found a reference explorer anywhere.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
MrMid
  • 301
  • 1
  • 3
  • 11

5 Answers5

15

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>
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
areller
  • 3,476
  • 5
  • 25
  • 41
  • Thank you! This seems to be the way to go. I will try it once I'll find how to use relative paths. – MrMid Aug 20 '17 at 11:16
12

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)

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Doug
  • 26,182
  • 26
  • 140
  • 197
1

Given that the DLL file you want to reference in the new ASP.NET Core 2.0 project is relatively fresh, I suspect you will need to make changes to this original DLL file as you develop the ASP.NET project.

In this situation I would add the original DLL project as part of the ASP.NET solution so you can work on both sets of source code, including setting of breakpoints within the same solution workspace.

NuGet packaging of the original DLL project can be delayed until the first release of your whole combined solution has stabilised and you want to make that DLL file available to a larger developer audience beyond the scope of your ASP.NET application.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
camelCase
  • 1,198
  • 2
  • 13
  • 26
  • Yeah, this saved me a lot of time probably. That library was indeed still buggy. Thanks! – MrMid Aug 20 '17 at 13:57
1

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

<ItemGroup>

 <Reference Include="MyAssembly">

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

 </Reference>
</ItemGroup>
simon w
  • 21
  • 3
0

A good solution will be to add the library (.dll file) that you want to use to the Project's References of your project in which you want to use the library:

Right Click on the project → AddReferenceProjectBrowse → Path_to_your_generated_library (.dll)

This will automatically generate the following node in the .csproj file:

<ItemGroup>
   <Reference Include="DotNetCoreClassLibraryCodeParser">
      <HintPath>..\..\DotNetCoreClassLibrary\DotNetCoreClassLibrary\bin\Debug\netcoreapp2.1\DotNetCoreClassLibrary.dll</HintPath>
   </Reference>
 </ItemGroup>
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
cristian
  • 384
  • 4
  • 17